Permalink
Browse files

buffer: split long lines up

When lines are longer then the window they are on, they get wrapped onto
the next line on the window, but not on the buffer, resulting in
overwriting of wrapped text.

This code checks each added line, and if it's too long, it will split
that line up in the buffer.

For improvements, splits could be made on whitespace only
  • Loading branch information...
1 parent d7b6a8d commit 2f0600a686746f238bc50d5d47009b0ad3083f84 @Ikke committed Mar 14, 2012
Showing with 21 additions and 2 deletions.
  1. +20 −1 ledger/buffer.py
  2. +1 −1 ledger/ledger.py
View
@@ -1,14 +1,19 @@
class Buffer(object):
- def __init__(self, window, lines):
+ def __init__(self, window, lines, columns=0):
self.window = window
self.lines = lines
self.buffer = [""]
+ self.columns = columns
def write(self, text):
lines = text.split("\n")
self.buffer[-1] += lines[0]
self.buffer.extend(lines[1:])
+
+ for i in range(len(self.buffer) - len(lines) - 1, len(self.buffer)):
+ self._fix_long_line(i)
+
self.refresh()
def writeln(self, text = ""):
@@ -36,3 +41,17 @@ def refresh(self):
self.window.addstr(nr, 0, line)
self.window.refresh()
+ def _fix_long_line(self, line):
+ if not self.columns: return
+
+ if len(self.buffer[line]) > self.columns:
+ part = self.buffer[line]
+ parts = []
+ while len(part):
+ parts.append(part[:self.columns])
+ part = part[self.columns:]
+
+ parts.reverse()
+ del(self.buffer[line])
+ for part in parts:
+ self.buffer.insert(line, part)
View
@@ -21,7 +21,7 @@ def setup_ncurses():
left_window = curses.newwin(lines, half_width)
right_window = curses.newwin(lines, half_width, 0, half_width + 2)
- left_buffer = Buffer(left_window, lines)
+ left_buffer = Buffer(left_window, lines, half_width)
right_buffer = Buffer(right_window, lines)
return left_buffer, right_buffer

0 comments on commit 2f0600a

Please sign in to comment.