From 0926ec5396a835f7996dea868becb9ae1e5defc5 Mon Sep 17 00:00:00 2001 From: hut Date: Wed, 30 Dec 2009 14:37:04 +0100 Subject: different way of trimming indentation of command docstrings --- TODO | 2 +- ranger/actions.py | 4 +++- ranger/ext/trim.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 ranger/ext/trim.py diff --git a/TODO b/TODO index a5561573..f0024ac5 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,7 @@ Console (X) #1 09/12/06 quick find (X) #2 09/12/06 open with (X) #4 09/12/06 history for console - ( ) #13 09/12/27 display docstring of a command + (X) #13 09/12/27 display docstring of a command General diff --git a/ranger/actions.py b/ranger/actions.py index d85184a3..9a9b3636 100644 --- a/ranger/actions.py +++ b/ranger/actions.py @@ -3,6 +3,7 @@ import shutil from ranger.shared import EnvironmentAware, SettingsAware from ranger import fsobject +from ranger.ext.trim import trimmed_lines_of_docstring class Actions(EnvironmentAware, SettingsAware): search_method = 'ctime' @@ -172,7 +173,8 @@ class Actions(EnvironmentAware, SettingsAware): return pager = self.ui.open_pager() - pager.set_source(command.__doc__.strip(), strip=True) + lines = trimmed_lines_of_docstring(command.__doc__) + pager.set_source(lines) def display_file(self): if not hasattr(self.ui, 'open_embedded_pager'): diff --git a/ranger/ext/trim.py b/ranger/ext/trim.py new file mode 100644 index 00000000..d469435d --- /dev/null +++ b/ranger/ext/trim.py @@ -0,0 +1,31 @@ +import sys + +def trim_docstring(docstring): + if not docstring: + return '' + return '\n'.join(trimmed_lines_of_docstring(docstring)) + +def trimmed_lines_of_docstring(docstring): + if not docstring: + return [] + # Convert tabs to spaces (following the normal Python rules) + # and split into a list of lines: + lines = docstring.expandtabs().splitlines() + # Determine minimum indentation (first line doesn't count): + indent = sys.maxint + for line in lines[1:]: + stripped = line.lstrip() + if stripped: + indent = min(indent, len(line) - len(stripped)) + # Remove indentation (first line is special): + trimmed = [lines[0].strip()] + if indent < sys.maxint: + for line in lines[1:]: + trimmed.append(line[indent:].rstrip()) + # Strip off trailing and leading blank lines: + while trimmed and not trimmed[-1]: + trimmed.pop() + while trimmed and not trimmed[0]: + trimmed.pop(0) + # Return a single string: + return trimmed -- cgit 1.4.1-2-gfad0