Petra Lamborn 4 years ago
parent
commit
211cfda12c
1 changed files with 26 additions and 6 deletions
  1. 26
    6
      fedicurses.py

+ 26
- 6
fedicurses.py View File

@@ -2,17 +2,22 @@ import curses
2 2
 from html.parser import HTMLParser
3 3
 from mastodon import Mastodon
4 4
 
5
+def initColours():
6
+                       # 0: White on black
7
+    curses.init_pair(1, 4, 0) # 1: Blue on black
8
+    curses.init_pair(2, 1, 0) # 2: Red on black
9
+    curses.init_pair(3, 6, 0) # 2: Cyan on black
10
+
5 11
 def newLine(ncwin, lines=1):
6 12
     cy, cx = ncwin.getyx()
7 13
     ncwin.move(cy + lines, 0)
8 14
 
9 15
 def printPost(win, post, parser):
16
+    win.addstr(post["account"]["acct"], curses.A_BOLD)
10 17
     if (post["reblog"] is not None):
11
-        win.addstr("{} boosted {}:".format(post["account"]["acct"],
12
-                                           post["reblog"]["account"]["acct"]),
13
-                   curses.A_BOLD)
14
-    else:
15
-        win.addstr("{}:".format(post["account"]["acct"]), curses.A_BOLD)
18
+        win.addstr(" boosted ")
19
+        win.addstr(post["reblog"]["account"]["acct"], curses.A_BOLD)
20
+    win.addstr(":")
16 21
     newLine(win)
17 22
     if (post["spoiler_text"] != ""):
18 23
         win.addstr(post["spoiler_text"], curses.A_UNDERLINE)
@@ -55,10 +60,14 @@ class PostParser(HTMLParser):
55 60
         self.defncatt = defncatt
56 61
         self.curatt = defncatt
57 62
         self.openp = False
63
+        self.colour = 0
64
+        self.colstack = []
58 65
 
59 66
     def handle_starttag(self, tag, attrs):
60 67
         if tag == 'p':
61 68
             self.curatt = self.defncatt
69
+            self.colstack = []
70
+            self.colour = 0
62 71
             # newLine(self.win)
63 72
         elif tag == 'strong' or tag == 'b':
64 73
             self.curatt = self.curatt ^ curses.A_REVERSE
@@ -66,6 +75,12 @@ class PostParser(HTMLParser):
66 75
             self.curatt = self.curatt ^ curses.A_BOLD
67 76
         elif tag == 'br':
68 77
             newLine(self.win)
78
+        elif tag == 'a':
79
+            self.colstack.append(self.colour)
80
+            self.colour = 1
81
+        elif tag == 'code':
82
+            self.colstack.append(self.colour)
83
+            self.colour = 3
69 84
 
70 85
     def handle_endtag(self, tag):
71 86
         if tag == 'p':
@@ -75,10 +90,14 @@ class PostParser(HTMLParser):
75 90
             self.curatt = self.curatt ^ curses.A_REVERSE
76 91
         elif tag == 'em' or tag == 'i':
77 92
             self.curatt = self.curatt ^ curses.A_BOLD
93
+        elif tag == 'a':
94
+            self.colour = self.colstack.pop()
95
+        elif tag == 'code':
96
+            self.colour = self.colstack.pop()
78 97
 
79 98
     def handle_data(self, data):
80 99
         try:
81
-            self.win.addstr(data, self.curatt)
100
+            self.win.addstr(data, self.curatt | curses.color_pair(self.colour))
82 101
         except curses.error:
83 102
             pass
84 103
         self.openp = True
@@ -88,6 +107,7 @@ class PostParser(HTMLParser):
88 107
 
89 108
 
90 109
 def main(stdscr):
110
+    initColours()
91 111
     mastodon = Mastodon(
92 112
             access_token = 'd100.club_usercred.secret',
93 113
             api_base_url = 'd100.club'