From d238d1e9628b937e3324effadd55c85a5635581a Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Tue, 29 Aug 2023 21:25:59 +0200 Subject: [PATCH] load more stories on increasing size --- hn.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/hn.py b/hn.py index b3a067a..717e4c4 100755 --- a/hn.py +++ b/hn.py @@ -26,6 +26,7 @@ class Client: self.loadedstories = [] self.story_pos = 0 self.cursor_pos = 0 + self.lines = curses.LINES self.cols = curses.COLS self.stories_in_a_site = self.lines - 3 @@ -33,7 +34,7 @@ class Client: def load_stories(self, from_pos, to_pos): for idx, i in enumerate(self.topstories[from_pos:to_pos]): #stdscr.clear() - self.set_footer(f'[{spinner_states[idx%4]}] Loading stories...') + self.set_footer(f'[{spinner_states[idx%4]}] Loading { to_pos - from_pos } stories...') self.screen.refresh() self.loadedstories.append(api.get_story(i)) @@ -41,7 +42,6 @@ class Client: self.screen.addstr(curses.LINES - 1, 0, footer, curses.A_REVERSE) def draw(self): - self.screen.clear() self.lines = curses.LINES self.cols = curses.COLS @@ -49,6 +49,7 @@ class Client: self.stories_in_a_site = self.lines - 3 self.screen.addstr('Hacker News Top Stories:\n') + for i, story in enumerate(self.loadedstories[self.story_pos:self.story_pos + self.stories_in_a_site]): prefix = '>>> ' if i == self.cursor_pos else ' ' # calculate length of line @@ -66,7 +67,7 @@ class Client: if i == self.cursor_pos: detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n' self.screen.addstr(detail) - self.set_footer(f'Loaded {self.stories_in_a_site} stories.') + self.set_footer(f'Loaded {len(self.loadedstories)} stories.') def handle_input(self): c = self.screen.getch() @@ -88,6 +89,9 @@ class Client: curses.resize_term(*self.screen.getmaxyx()) self.lines, self.cols = self.screen.getmaxyx() self.stories_in_a_site = self.lines - 3 + if len(self.loadedstories) < self.story_pos + self.stories_in_a_site: + # load more + self.load_stories(len(self.loadedstories), self.story_pos + self.stories_in_a_site) def run(self): self.load_stories(0, self.stories_in_a_site) @@ -106,7 +110,7 @@ def main(): client.run() except Exception as e: curses.endwin() - print(e) + raise e if __name__ == '__main__': main()