summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2014-12-03 18:12:40 +0100
committerhut <hut@lepus.uberspace.de>2014-12-03 18:12:40 +0100
commit6196b9b089808ed085492860cc89e5ad6013897f (patch)
tree103a4e84f556476f2e63cf8e7f7305317ea7fe88 /ranger
parent60c5722e81c75da152efcaeef84e54fd83b8c78d (diff)
downloadranger-6196b9b089808ed085492860cc89e5ad6013897f.tar.gz
implement a paper manager
Diffstat (limited to 'ranger')
-rw-r--r--ranger/core/fm.py2
-rw-r--r--ranger/ext/papermanager.py55
-rw-r--r--ranger/gui/widgets/browsercolumn.py19
3 files changed, 73 insertions, 3 deletions
diff --git a/ranger/core/fm.py b/ranger/core/fm.py
index d567bf24..885068e5 100644
--- a/ranger/core/fm.py
+++ b/ranger/core/fm.py
@@ -20,6 +20,7 @@ from ranger.gui.ui import UI
 from ranger.container.bookmarks import Bookmarks
 from ranger.core.runner import Runner
 from ranger.ext.img_display import ImageDisplayer
+from ranger.ext.papermanager import PaperManager
 from ranger.ext.rifle import Rifle
 from ranger.container.directory import Directory
 from ranger.ext.signals import SignalDispatcher
@@ -59,6 +60,7 @@ class FM(Actions, SignalDispatcher):
         self.loader = Loader()
         self.copy_buffer = set()
         self.do_cut = False
+        self.papermanager = PaperManager()
 
         try:
             self.username = pwd.getpwuid(os.geteuid()).pw_name
diff --git a/ranger/ext/papermanager.py b/ranger/ext/papermanager.py
new file mode 100644
index 00000000..a013585f
--- /dev/null
+++ b/ranger/ext/papermanager.py
@@ -0,0 +1,55 @@
+# Copyright (C) 2014  Roman Zimbelmann <hut@lepus.uberspace.de>
+# This software is distributed under the terms of the GNU GPL version 3.
+
+import csv
+from os.path import join, dirname, exists, basename
+
+from ranger.ext.openstruct import OpenStruct
+
+class PaperManager(object):
+    def __init__(self):
+        self.metadata_cache = dict()
+        self.metafile_cache = dict()
+
+    def reset(self):
+        self.metadata_cache.clear()
+        self.metafile_cache.clear()
+
+    def get_paper_info(self, filename):
+        try:
+            return self.metadata_cache[filename]
+        except KeyError:
+            result = OpenStruct(title=None, year=None, filename=filename)
+            metafile = join(dirname(filename), ".paperinfo")
+
+            # get entries of the metadata file
+            if metafile in self.metafile_cache:
+                entries = self.metafile_cache[metafile]
+            else:
+                if exists(metafile):
+                    reader = csv.reader(open(metafile, "r"),
+                            skipinitialspace=True)
+
+                    entries = list(entry for entry in reader if len(entry) == 3)
+                    self.metafile_cache[metafile] = entries
+                else:
+                    # No metadata file
+                    entries = []
+
+            # Find the relevant entry in the metadata file
+            valid = (filename, basename(filename))
+            for entry in entries:
+                if entry[0] in valid:
+                    self._fill_ostruct_with_data(result, entry)
+                    break
+
+            # Cache the value
+            self.metadata_cache[filename] = result
+            return result
+
+    def _fill_ostruct_with_data(self, ostruct, dataset):
+        filename, year, title = dataset
+        if year:
+            ostruct.year = year
+        if title:
+            ostruct.title = title
diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py
index 9e177a52..c96a2d73 100644
--- a/ranger/gui/widgets/browsercolumn.py
+++ b/ranger/gui/widgets/browsercolumn.py
@@ -258,12 +258,25 @@ class BrowserColumn(Pager):
                 self.color_reset()
                 continue
 
-            if drawn._linemode == "filename":
+
+            # Deal with the line mode
+            use_linemode = drawn._linemode
+            if use_linemode == "title":
+                paperinfo = self.fm.papermanager.get_paper_info(drawn.path)
+                if paperinfo.title:
+                    if paperinfo.year:
+                        text = "%s - %s" % (paperinfo.year, paperinfo.title)
+                    else:
+                        text = paperinfo.title
+                else:
+                    use_linemode = "filename"
+            if use_linemode == "filename":
                 text = drawn.basename
-            elif drawn._linemode == "permissions":
+            elif use_linemode == "permissions":
                 text = "%s %s %s %s" % (drawn.get_permission_string(),
                         drawn.user, drawn.group, drawn.basename)
 
+
             if drawn.marked and (self.main_column or \
                     self.settings.display_tags_in_all_columns):
                 text = " " + text
@@ -291,7 +304,7 @@ class BrowserColumn(Pager):
                 space -= vcsstringlen
 
             # info string
-            if drawn._linemode == "filename":
+            if use_linemode == "filename":
                 infostring = self._draw_infostring_display(drawn, space)
                 infostringlen = self._total_len(infostring)
                 if space - infostringlen > 2: