From e7effa34efee405af2aae1a8cd36c256fe26b8d9 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Mon, 4 Sep 2023 22:04:43 +0200 Subject: [PATCH] add help --- hn.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/hn.py b/hn.py index ff4103e..c0c76f7 100755 --- a/hn.py +++ b/hn.py @@ -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):