add a spinner during loading

This commit is contained in:
Felix Pankratz 2023-08-29 10:09:53 +02:00
parent a7c3b845d9
commit 14dd317bac

9
hn.py
View File

@ -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'