summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2011-10-11 21:21:52 +0200
committerhut <hut@lavabit.com>2011-10-11 21:21:52 +0200
commit9b05f8d7279266c1b1ed2224d9a33f67935f2586 (patch)
treea2920aef11ab5d9a93597caedd767e2ef2db77c5
parentf67f859d8b07418d11ffe2975dc1c907ffe35a9f (diff)
downloadranger-9b05f8d7279266c1b1ed2224d9a33f67935f2586.tar.gz
gui.ansi: Fix test case in char_slice
-rw-r--r--ranger/gui/ansi.py65
-rw-r--r--ranger/gui/widgets/pager.py3
2 files changed, 34 insertions, 34 deletions
diff --git a/ranger/gui/ansi.py b/ranger/gui/ansi.py
index e2fd581d..802df04d 100644
--- a/ranger/gui/ansi.py
+++ b/ranger/gui/ansi.py
@@ -86,41 +86,42 @@ def char_len(ansi_text):
 	"""
 	return len(ansi_re.sub('', ansi_text))
 
-def char_slice(ansi_text, start, end):
+def char_slice(ansi_text, start, length):
 	"""
-	>>> 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'
+	>>> test_string = "abcde\x1b[30mfoo\x1b[31mbar\x1b[0mnormal"
+	>>> split_ansi_from_text(test_string)
+	['abcde', '\\x1b[30m', 'foo', '\\x1b[31m', 'bar', '\\x1b[0m', 'normal']
+	>>> char_slice(test_string, 1, 3)
+	'bcd'
+	>>> char_slice(test_string, 0, 8)
+	'abcde\\x1b[30mfoo'
+	>>> char_slice(test_string, 4, 4)
+	'e\\x1b[30mfoo'
+	>>> char_slice(test_string, 11, 100)
+	'\\x1b[0mnormal'
+	>>> char_slice(test_string, 9, 100)
+	'\\x1b[31mar\\x1b[0mnormal'
 	"""
-	slice_chunks = []
-	# skip to start
-	last_color = None
-	skip_len_left = start
-	len_left = end - start
-	for chunk in split_ansi_from_text(ansi_text):
-		m = ansi_re.match(chunk)
-		if m:
-			if chunk[-1] == 'm':
-				last_color = chunk
+	chunks = []
+	last_color = ""
+	pos = 0
+	for i, chunk in enumerate(split_ansi_from_text(ansi_text)):
+		if i % 2 == 1:
+			last_color = chunk
+		elif pos + len(chunk) < start:
+			pos += len(chunk)  #seek
+		elif pos < start and pos + len(chunk) >= start:
+			if chunk[start-pos:start-pos+length]:
+				chunks.append(last_color)
+				chunks.append(chunk[start-pos:start-pos+length])
+			pos += len(chunk)
 		else:
-			if skip_len_left > len(chunk):
-				skip_len_left -= len(chunk)
-			else:		# finished skipping to start
-				if skip_len_left > 0:
-					chunk = chunk[skip_len_left:]
-				chunk_left = chunk[:len_left]
-				if len(chunk_left):
-					if last_color is not None:
-						slice_chunks.append(last_color)
-					slice_chunks.append(chunk_left)
-					len_left -= len(chunk_left)
-				if len_left == 0:
-					break
-	return ''.join(slice_chunks)
+			chunks.append(last_color)
+			chunks.append(chunk)
+			pos += len(chunk)
+		if pos - start >= length:
+			break
+	return ''.join(chunks)
 
 if __name__ == '__main__':
 	import doctest
diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py
index c9b67736..38451781 100644
--- a/ranger/gui/widgets/pager.py
+++ b/ranger/gui/widgets/pager.py
@@ -175,8 +175,7 @@ class Pager(Widget):
 			try:
 				line = self._get_line(i).expandtabs(4)
 				if self.markup is 'ansi':
-					line = ansi.char_slice(line, startx, self.wid + startx) \
-							+ ansi.reset
+					line = ansi.char_slice(line, startx, self.wid) + ansi.reset
 				else:
 					line = line[startx:self.wid + startx]
 				yield line.rstrip()
/commit/html/linux/402time.mu.html?h=main&id=3350c34a74844e21ea69077e01efff3bae64bdcd'>3350c34a ^
1c349ac7 ^
7fa80570 ^
1c349ac7 ^



3d1c4216 ^

ec32c11d ^
3d1c4216 ^
ec32c11d ^

3350c34a ^
ec32c11d ^

3d1c4216 ^



ec32c11d ^
3d1c4216 ^
ec32c11d ^

3350c34a ^
ec32c11d ^

3d1c4216 ^






ec32c11d ^

3d1c4216 ^
3350c34a ^

3d1c4216 ^

ec32c11d ^

3350c34a ^
3d1c4216 ^
1c349ac7 ^



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