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