diff options
author | hut <hut@lavabit.com> | 2011-10-11 22:02:00 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2011-10-11 22:05:37 +0200 |
commit | 6f695ffeafd1d2f1cb7c0c201b395582a1f7ce4e (patch) | |
tree | c44a71f9502dd08e5f1917ceb0f0f33fe3efe57f /ranger | |
parent | 138126641ff06a4528ca433328f765d9e0e6de6d (diff) | |
download | ranger-6f695ffeafd1d2f1cb7c0c201b395582a1f7ce4e.tar.gz |
gui.ansi: simplified more + another test case
Diffstat (limited to 'ranger')
-rw-r--r-- | ranger/gui/ansi.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/ranger/gui/ansi.py b/ranger/gui/ansi.py index 5f14f84d..65038120 100644 --- a/ranger/gui/ansi.py +++ b/ranger/gui/ansi.py @@ -88,11 +88,18 @@ def char_len(ansi_text): def char_slice(ansi_text, start, length): """ + Slices a string with respect to ansi code sequences + + Acts as if the ansi codes aren't there, slices the text from the + given start point to the given length and adds the codes back in. + >>> 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, 5, 6) + '\\x1b[30mfoo\\x1b[31mbar' >>> char_slice(test_string, 0, 8) 'abcde\\x1b[30mfoo' >>> char_slice(test_string, 4, 4) @@ -114,16 +121,14 @@ def char_slice(ansi_text, start, length): old_pos = pos pos += len(chunk) - if pos < start: + if pos <= start: pass # seek elif old_pos < start and pos >= start: - if chunk[start-old_pos:start-old_pos+length]: - chunks.append(last_color) - chunks.append(chunk[start-old_pos:start-old_pos+length]) + chunks.append(last_color) + chunks.append(chunk[start-old_pos:start-old_pos+length]) elif pos > length + start: - if chunk[:start-old_pos+length]: - chunks.append(last_color) - chunks.append(chunk[:start-old_pos+length]) + chunks.append(last_color) + chunks.append(chunk[:start-old_pos+length]) else: chunks.append(last_color) chunks.append(chunk) |