about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--doc/ranger.19
-rw-r--r--doc/ranger.pod9
-rw-r--r--ranger/config/rc.conf4
-rw-r--r--ranger/container/settings.py2
-rw-r--r--ranger/gui/widgets/browsercolumn.py48
5 files changed, 70 insertions, 2 deletions
diff --git a/doc/ranger.1 b/doc/ranger.1
index 7e57307e..ecc4f7f1 100644
--- a/doc/ranger.1
+++ b/doc/ranger.1
@@ -133,7 +133,7 @@
 .\" ========================================================================
 .\"
 .IX Title "RANGER 1"
-.TH RANGER 1 "ranger-1.7.2" "05/24/2016" "ranger manual"
+.TH RANGER 1 "ranger-1.7.2" "16/05/16" "ranger manual"
 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
 .\" way too many mistakes in technical documents.
 .if n .ad l
@@ -735,6 +735,9 @@ this pattern will hide all files that start with a dot or end with a tilde.
 The delay that ranger idly waits for user input, in milliseconds, with a
 resolution of 100ms.  Lower delay reduces lag between directory updates but
 increases \s-1CPU\s0 load.
+.IP "line_numbers [bool]" 4
+.IX Item "line_numbers [bool]"
+Show line numbers in main column.
 .IP "max_console_history_size [integer, none]" 4
 .IX Item "max_console_history_size [integer, none]"
 How many console commands should be kept in history?  \*(L"none\*(R" will disable the
@@ -773,6 +776,10 @@ to disable this feature.
 Which script should handle generating previews?  If the file doesn't exist, or
 use_preview_script is off, ranger will handle previews itself by just printing
 the content.
+.IP "relative_line_numbers [bool]" 4
+.IX Item "relative_line_numbers [bool]"
+Show relative line numbers in main column.  Requires \fBline_numbers\fR set to
+\&\fBtrue\fR.
 .IP "save_console_history [bool]" 4
 .IX Item "save_console_history [bool]"
 Should the console history be saved on exit?  If disabled, the console history
diff --git a/doc/ranger.pod b/doc/ranger.pod
index a21bcde0..00c26f12 100644
--- a/doc/ranger.pod
+++ b/doc/ranger.pod
@@ -720,6 +720,10 @@ The delay that ranger idly waits for user input, in milliseconds, with a
 resolution of 100ms.  Lower delay reduces lag between directory updates but
 increases CPU load.
 
+=item line_numbers [bool]
+
+Show line numbers in main column.
+
 =item max_console_history_size [integer, none]
 
 How many console commands should be kept in history?  "none" will disable the
@@ -768,6 +772,11 @@ Which script should handle generating previews?  If the file doesn't exist, or
 use_preview_script is off, ranger will handle previews itself by just printing
 the content.
 
+=item relative_line_numbers [bool]
+
+Show relative line numbers in main column.  Requires B<line_numbers> set to
+B<true>.
+
 =item save_console_history [bool]
 
 Should the console history be saved on exit?  If disabled, the console history
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)