Browse Source

Broken typesetting

Petra Lamborn 4 years ago
parent
commit
a6da44a6c5
1 changed files with 33 additions and 1 deletions
  1. 33
    1
      fedicurses.py

+ 33
- 1
fedicurses.py View File

@@ -12,6 +12,37 @@ def newLine(ncwin, lines=1):
12 12
     cy, cx = ncwin.getyx()
13 13
     ncwin.move(cy + lines, 0)
14 14
 
15
+def bump(win):
16
+    my, mx = win.getmaxyx()
17
+    cy, cx = win.getyx()
18
+    if cx != mx:
19
+        win.move(cy, cx + 1)
20
+
21
+
22
+def typeset(text, win, attr, colour):
23
+    my, mx = win.getmaxyx()
24
+    line = 0
25
+    position = 0
26
+    strlen = len(text)
27
+    while position < (strlen - 1):
28
+        cy, cx = win.getyx()
29
+        if cx == mx:
30
+            if cy == my:
31
+                raise curses.error("Out of space")
32
+        rem = (mx - cx) + 1
33
+        nsp = text.find(' ', position)
34
+        if nsp == -1 or nsp - position > rem:
35
+            if cx == 0:
36
+                win.addnstr(text[position:], rem, attr |
37
+                            curses.color_pair(colour))
38
+                position = position + rem
39
+            newLine(win)
40
+        else:
41
+            win.addnstr(text[position:], nsp - position,
42
+                        attr | curses.color_pair(colour))
43
+            position = nsp + 1
44
+            bump(win)
45
+
15 46
 def printPost(win, post, parser):
16 47
     win.addstr(post["account"]["acct"], curses.A_BOLD)
17 48
     if (post["reblog"] is not None):
@@ -97,7 +128,8 @@ class PostParser(HTMLParser):
97 128
 
98 129
     def handle_data(self, data):
99 130
         try:
100
-            self.win.addstr(data, self.curatt | curses.color_pair(self.colour))
131
+            # self.win.addstr(data, self.curatt | curses.color_pair(self.colour))
132
+            typeset(data, self.win, self.curatt, self.colour)
101 133
         except curses.error:
102 134
             pass
103 135
         self.openp = True