From 14dd317baca71470598027f9a985d4d521a4b910 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Tue, 29 Aug 2023 10:09:53 +0200 Subject: [PATCH] add a spinner during loading --- hn.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/hn.py b/hn.py index 4106381..d6af7c5 100755 --- a/hn.py +++ b/hn.py @@ -6,6 +6,8 @@ import webbrowser from dataclasses import dataclass +spinner_states = ['-', '\\', '|', '/'] + @dataclass class Story: id: int @@ -25,7 +27,10 @@ def main(stdscr): raise Exception('Error fetching data from Hacker News API') ids = r.json()[:15] # Show only the first ten stories stories = [] - for i in ids: + for idx, i in enumerate(ids): + stdscr.clear() + stdscr.addstr(f'[{spinner_states[idx%4]}] Getting stories...') + stdscr.refresh() story_url = f'https://hacker-news.firebaseio.com/v0/item/{i}.json' s = requests.get(story_url).json() stories.append(Story(s['id'], @@ -38,7 +43,7 @@ def main(stdscr): current_pos = 0 while True: stdscr.clear() - stdscr.addstr('\n\nHacker News Top Stories:\n') + stdscr.addstr('Hacker News Top Stories:\n') for i, story in enumerate(stories): prefix = '>>> ' if i == current_pos else ' ' text = f'{prefix}{story.title} ({story.link})\n'