summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/gui/ansi.py37
1 files changed, 34 insertions, 3 deletions
diff --git a/ranger/gui/ansi.py b/ranger/gui/ansi.py
index ea024977..2a4c4461 100644
--- a/ranger/gui/ansi.py
+++ b/ranger/gui/ansi.py
@@ -14,18 +14,22 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+"""
+A library to help to convert ANSI codes to curses instructions.
+"""
+
 from ranger.gui import color
 import re
 
-ansi_re = re.compile('(\033' + r'\[\d*(?:;\d+)*?[a-zA-Z])')
-reset = '\033[0m'
+ansi_re = re.compile('(\x1b' + r'\[\d*(?:;\d+)*?[a-zA-Z])')
+reset = '\x1b[0m'
 
 def split_ansi_from_text(ansi_text):
 	return ansi_re.split(ansi_text)
 
 def text_with_fg_bg_attr(ansi_text):
 	for chunk in split_ansi_from_text(ansi_text):
-		if chunk and chunk[0] == '\033':
+		if chunk and chunk[0] == '\x1b':
 			if chunk[-1] != 'm':
 				continue
 			match = re.match(r'^.\[(.*).$', chunk)
@@ -66,9 +70,32 @@ def text_with_fg_bg_attr(ansi_text):
 			yield chunk
 
 def char_len(ansi_text):
+	"""
+	Count the number of visible characters.
+
+	>>> char_len("\x1b[0;30;40mX\x1b[0m")
+	1
+	>>> char_len("\x1b[0;30;40mXY\x1b[0m")
+	2
+	>>> char_len("\x1b[0;30;40mX\x1b[0mY")
+	2
+	>>> char_len("hello")
+	5
+	>>> char_len("")
+	0
+	"""
 	return len(ansi_re.sub('', ansi_text))
 
 def char_slice(ansi_text, start, end):
+	"""
+	>>> ansi_string = "\x1b[0;30;40mX\x1b[0;31;41mY\x1b[0m"
+	>>> char_slice(ansi_string, 0, 1)
+	'\\x1b[0;30;40mX'
+
+	# XXX: Does not work as expected:
+	# >>> char_slice(ansi_string, 1, 2)
+	# '\\x1b[0;31;41mY'
+	"""
 	slice_chunks = []
 	# skip to start
 	last_color = None
@@ -94,3 +121,7 @@ def char_slice(ansi_text, start, end):
 				if len_left == 0:
 					break
 	return ''.join(slice_chunks)
+
+if __name__ == '__main__':
+	import doctest
+	doctest.testmod()
:34:02 +0100 committer hut <hut@lavabit.com> 2010-01-01 22:34:02 +0100 readme: updated' href='/akspecs/ranger/commit/README?h=v1.5.1&id=36e4e71ee5643fc0b2c501956086434389ec5384'>36e4e71e ^
4ea0f69a ^

36e4e71e ^




78a7d762 ^
36e4e71e ^

78a7d762 ^
36e4e71e ^


4ea0f69a ^

36e4e71e ^
4ea0f69a ^
36e4e71e ^



f6ae504c ^
4ea0f69a ^














36e4e71e ^





4ea0f69a ^


36e4e71e ^




4ea0f69a ^

36e4e71e ^



f6ae504c ^
4ea0f69a ^
36e4e71e ^

4ea0f69a ^

36e4e71e ^






4ea0f69a ^

306c76d8 ^






b6aff4c3 ^

306c76d8 ^
4ea0f69a ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112