This commit is contained in:
Felix Pankratz 2023-09-04 22:04:43 +02:00
parent bf575bf881
commit e7effa34ef

30
hn.py
View File

@ -23,6 +23,7 @@ class Client:
curses.noecho()
curses.cbreak()
self.screen.keypad(True)
curses.curs_set(0)
self.topstories = api.get_topstories()
self.loadedstories = {}
@ -94,9 +95,11 @@ class Client:
async def handle_input(self):
c = self.screen.getch()
story = self.loadedstories[self.topstories[self.story_pos + self.cursor_pos]]
if c == ord('q'): # Quit
await self.exit()
elif c == curses.KEY_UP:
elif c == curses.KEY_UP or c == ord('k'):
self.cursor_pos -= 1
if self.cursor_pos < 0:
self.cursor_pos = self.stories_in_a_site-1
@ -104,7 +107,7 @@ class Client:
self.story_pos -= self.stories_in_a_site
self.story_pos = 0 if self.story_pos < 0 else self.story_pos
elif c == curses.KEY_DOWN:
elif c == curses.KEY_DOWN or c == ord('j'):
self.cursor_pos += 1
if self.cursor_pos >= self.stories_in_a_site:
self.cursor_pos = 0
@ -133,6 +136,29 @@ class Client:
self.stories_in_a_site = self.lines - 3
await self.load_more_if_needed()
elif c == ord('?'):
self.help()
def help(self):
def __helpwin_addstr__(win, string, newline=True):
c_pos_y, c_pos_x = win.getyx()
win.move(c_pos_y, 1)
win.addstr(string)
if newline:
win.move(c_pos_y +1, 1)
helpwin = curses.newwin(self.lines//2, self.cols//2, self.lines//4, self.cols//4)
helpwin.box()
__helpwin_addstr__(helpwin, 'Help')
__helpwin_addstr__(helpwin, '')
__helpwin_addstr__(helpwin, 'hjkl or arrows - move')
__helpwin_addstr__(helpwin, 'enter - open link')
__helpwin_addstr__(helpwin, 'c - open comments')
__helpwin_addstr__(helpwin, 'r - reload')
__helpwin_addstr__(helpwin, '')
helpwin.getch()
async def load_more_if_needed(self):