summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2016-05-24 15:32:08 +0200
committerhut <hut@lepus.uberspace.de>2016-05-24 15:32:08 +0200
commit94dc91e291ef666d091966ed664407bb0911e06e (patch)
treee179e0c1b2a957bc6a75d8fe807e058261e0d6c6 /ranger
parentfe56c9c8494f961505c6f63d9814f961ec070ae3 (diff)
parentb5adbbb7c27b2d8bfd42b67132a93cb71ed656ab (diff)
downloadranger-94dc91e291ef666d091966ed664407bb0911e06e.tar.gz
Merge branch 'linum' of https://github.com/mrogalski/ranger
Diffstat (limited to 'ranger')
-rw-r--r--ranger/config/rc.conf4
-rw-r--r--ranger/container/settings.py2
-rw-r--r--ranger/gui/widgets/browsercolumn.py48
3 files changed, 53 insertions, 1 deletions
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index 914b1d0f..cf07b232 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -201,6 +201,10 @@ set metadata_deep_search false
 # Clear all existing filters when leaving a directory
 set clear_filters_on_dir_change false
 
+# Disable displaying line numbers in main column
+set line_numbers false
+set relative_line_numbers false
+
 # ===================================================================
 # == Local Options
 # ===================================================================
diff --git a/ranger/container/settings.py b/ranger/container/settings.py
index 14ff9bca..be468c21 100644
--- a/ranger/container/settings.py
+++ b/ranger/container/settings.py
@@ -26,6 +26,7 @@ ALLOWED_SETTINGS = {
     'flushinput': bool,
     'hidden_filter': str,
     'idle_delay': int,
+    'line_numbers': bool,
     'max_console_history_size': (int, type(None)),
     'max_history_size': (int, type(None)),
     'metadata_deep_search': bool,
@@ -38,6 +39,7 @@ ALLOWED_SETTINGS = {
     'preview_images_method': str,
     'preview_max_size': int,
     'preview_script': (str, type(None)),
+    'relative_line_numbers': bool,
     'save_console_history': bool,
     'scroll_offset': int,
     'shorten_title': int,
diff --git a/ranger/gui/widgets/browsercolumn.py b/ranger/gui/widgets/browsercolumn.py
index 129d8486..964f000a 100644
--- a/ranger/gui/widgets/browsercolumn.py
+++ b/ranger/gui/widgets/browsercolumn.py
@@ -191,6 +191,15 @@ class BrowserColumn(Pager):
                 self.set_source(f)
             Pager.draw(self)
 
+    def _format_line_number(self, linum_format, i, selected_i):
+        line_number = i
+        if self.settings.relative_line_numbers:
+            line_number = abs(selected_i - i)
+            if line_number == 0:
+                line_number = selected_i
+
+        return linum_format.format(line_number)
+
     def _draw_directory(self):
         """Draw the contents of a directory"""
         if self.image:
@@ -239,6 +248,13 @@ class BrowserColumn(Pager):
 
         copied = [f.path for f in self.fm.copy_buffer]
 
+        # Set the size of the linum text field to the number of digits in the
+        # visible files in directory.
+        linum_text_len = len(str(self.scroll_begin + self.hei))
+        linum_format = "{0:>" + str(linum_text_len) + "}"
+        # add separator between line number and tag
+        linum_format += " "
+
         selected_i = self._get_index_of_selected_file()
         for line in range(self.hei):
             i = line + self.scroll_begin
@@ -269,9 +285,26 @@ class BrowserColumn(Pager):
             key = (self.wid, selected_i == i, drawn.marked, self.main_column,
                    drawn.path in copied, tagged_marker, drawn.infostring,
                    drawn.vcsstatus, drawn.vcsremotestatus, self.target.has_vcschild,
-                   self.fm.do_cut, current_linemode.name, metakey, active_pane)
+                   self.fm.do_cut, current_linemode.name, metakey, active_pane,
+                   self.settings.line_numbers)
 
+            # Check if current line has not already computed and cached
             if key in drawn.display_data:
+                # This conditional is necessary for two things:
+                #  1) computing relative line numbers,
+                #  2) switching between relative line numbers and normal.
+                #
+                # (1) is necessary because drawn.display_data cache cannot be
+                # trusted when it comes to line numbers.
+                # For (2) we could add self.settings.relative_line_numbers to
+                # key, but we still require a conditional check here for (1),
+                # and it solves both problems at the same time.
+                if self.main_column and self.settings.line_numbers:
+                    line_number_text = self._format_line_number(linum_format,
+                                                                i,
+                                                                selected_i)
+                    drawn.display_data[key][0][0] = line_number_text
+
                 self.execute_curses_batch(line, drawn.display_data[key])
                 self.color_reset()
                 continue
@@ -290,6 +323,19 @@ class BrowserColumn(Pager):
             predisplay_right = []
             space = self.wid
 
+            # line number field
+            if self.settings.line_numbers:
+                if self.main_column and space - linum_text_len > 2:
+                    line_number_text = self._format_line_number(linum_format,
+                                                                i,
+                                                                selected_i)
+                    predisplay_left.append([line_number_text, []])
+                    space -= linum_text_len
+
+                    # Delete one additional character for space separator
+                    # between the line number and the tag
+                    space -= 1
+
             # selection mark
             tagmark = self._draw_tagged_display(tagged, tagged_marker)
             tagmarklen = self._total_len(tagmark)