load more stories on increasing size

master
Felix Pankratz 1 year ago
parent 4bd8d61d7c
commit d238d1e962

12
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()

Loading…
Cancel
Save