about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-30 14:37:04 +0100
committerhut <hut@lavabit.com>2009-12-30 14:37:04 +0100
commit0926ec5396a835f7996dea868becb9ae1e5defc5 (patch)
treecfe327f93ecee3fbbbfe65ece783f6875ce7f6f2
parent524d95e19c05830848567b586c0df6dba5c28e75 (diff)
downloadranger-0926ec5396a835f7996dea868becb9ae1e5defc5.tar.gz
different way of trimming indentation of command docstrings
-rw-r--r--TODO2
-rw-r--r--ranger/actions.py4
-rw-r--r--ranger/ext/trim.py31
3 files changed, 35 insertions, 2 deletions
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