add a spinner during loading

master
Felix Pankratz 1 year ago
parent a7c3b845d9
commit 14dd317bac

@ -6,6 +6,8 @@ import webbrowser
from dataclasses import dataclass from dataclasses import dataclass
spinner_states = ['-', '\\', '|', '/']
@dataclass @dataclass
class Story: class Story:
id: int id: int
@ -25,7 +27,10 @@ def main(stdscr):
raise Exception('Error fetching data from Hacker News API') raise Exception('Error fetching data from Hacker News API')
ids = r.json()[:15] # Show only the first ten stories ids = r.json()[:15] # Show only the first ten stories
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' story_url = f'https://hacker-news.firebaseio.com/v0/item/{i}.json'
s = requests.get(story_url).json() s = requests.get(story_url).json()
stories.append(Story(s['id'], stories.append(Story(s['id'],
@ -38,7 +43,7 @@ def main(stdscr):
current_pos = 0 current_pos = 0
while True: while True:
stdscr.clear() stdscr.clear()
stdscr.addstr('\n\nHacker News Top Stories:\n') stdscr.addstr('Hacker News Top Stories:\n')
for i, story in enumerate(stories): for i, story in enumerate(stories):
prefix = '>>> ' if i == current_pos else ' ' prefix = '>>> ' if i == current_pos else ' '
text = f'{prefix}{story.title} ({story.link})\n' text = f'{prefix}{story.title} ({story.link})\n'

Loading…
Cancel
Save