From 41d3e07a3f87717bbb35e6fc3e12273f122f5445 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Tue, 29 Aug 2023 22:24:54 +0200 Subject: [PATCH] add paging --- hn.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/hn.py b/hn.py index f03d9fd..9a9a958 100755 --- a/hn.py +++ b/hn.py @@ -89,10 +89,18 @@ class Client: self.cursor_pos -= 1 if self.cursor_pos < 0: self.cursor_pos = self.stories_in_a_site-1 + # scroll up a page (: + 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: self.cursor_pos += 1 if self.cursor_pos >= self.stories_in_a_site: self.cursor_pos = 0 + # scroll up down a page :) + self.story_pos += self.stories_in_a_site + self.load_more_if_needed() + elif c == ord('c'): webbrowser.open(f'https://news.ycombinator.com/item?id={self.loadedstories[self.story_pos + self.cursor_pos].id}') elif c == curses.KEY_ENTER or c == 10: @@ -101,9 +109,12 @@ 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) + self.load_more_if_needed() + + def load_more_if_needed(self): + 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)