Compare commits
2 Commits
bf575bf881
...
1fc2d55983
Author | SHA1 | Date | |
---|---|---|---|
1fc2d55983 | |||
e7effa34ef |
42
hn.py
42
hn.py
@ -23,6 +23,7 @@ class Client:
|
||||
curses.noecho()
|
||||
curses.cbreak()
|
||||
self.screen.keypad(True)
|
||||
curses.curs_set(0)
|
||||
|
||||
self.topstories = api.get_topstories()
|
||||
self.loadedstories = {}
|
||||
@ -88,15 +89,19 @@ class Client:
|
||||
|
||||
page = int(self.story_pos / self.stories_in_a_site + 1)
|
||||
total_pages = math.ceil(500/len(self.loadedstories))
|
||||
self.set_footer(f'Page {page}/{total_pages}, loaded {len(self.loadedstories)} stories.')
|
||||
#self.set_footer(f'{self.loadedstories}')
|
||||
footer_text = f'Page {page}/{total_pages}, loaded {len(self.loadedstories)} stories. [?] for help.'
|
||||
if len(footer_text) >= self.cols:
|
||||
footer_text = footer_text[:self.cols - 1]
|
||||
self.set_footer(footer_text)
|
||||
|
||||
async def handle_input(self):
|
||||
c = self.screen.getch()
|
||||
story = self.loadedstories[self.topstories[self.story_pos + self.cursor_pos]]
|
||||
|
||||
if c == ord('q'): # Quit
|
||||
await self.exit()
|
||||
elif c == curses.KEY_UP:
|
||||
|
||||
elif c == curses.KEY_UP or c == ord('k'):
|
||||
self.cursor_pos -= 1
|
||||
if self.cursor_pos < 0:
|
||||
self.cursor_pos = self.stories_in_a_site-1
|
||||
@ -104,7 +109,7 @@ class Client:
|
||||
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:
|
||||
elif c == curses.KEY_DOWN or c == ord('j'):
|
||||
self.cursor_pos += 1
|
||||
if self.cursor_pos >= self.stories_in_a_site:
|
||||
self.cursor_pos = 0
|
||||
@ -133,6 +138,29 @@ class Client:
|
||||
self.stories_in_a_site = self.lines - 3
|
||||
await self.load_more_if_needed()
|
||||
|
||||
elif c == ord('?'):
|
||||
self.help()
|
||||
|
||||
def help(self):
|
||||
def __helpwin_addstr__(win, string, newline=True):
|
||||
c_pos_y, _ = win.getyx()
|
||||
win.move(c_pos_y, 1)
|
||||
win.addstr(string)
|
||||
if newline:
|
||||
win.move(c_pos_y +1, 1)
|
||||
|
||||
helpwin = curses.newwin(self.lines//2, self.cols//2, self.lines//4, self.cols//4)
|
||||
helpwin.box()
|
||||
__helpwin_addstr__(helpwin, 'Help')
|
||||
__helpwin_addstr__(helpwin, '')
|
||||
__helpwin_addstr__(helpwin, 'hjkl or arrows - move')
|
||||
__helpwin_addstr__(helpwin, 'enter - open link')
|
||||
__helpwin_addstr__(helpwin, 'c - open comments')
|
||||
__helpwin_addstr__(helpwin, 'r - reload')
|
||||
__helpwin_addstr__(helpwin, '')
|
||||
|
||||
|
||||
helpwin.getch()
|
||||
|
||||
async def load_more_if_needed(self):
|
||||
|
||||
@ -141,14 +169,12 @@ class Client:
|
||||
await self.load_stories(self.story_pos, self.story_pos + self.stories_in_a_site)
|
||||
|
||||
async def load_stories(self, from_pos, to_pos):
|
||||
#self.set_footer(f'[{spinner_states[idx%4]}] Loading { to_pos - from_pos } stories...')
|
||||
self.set_footer(f'Loading stories...')
|
||||
self.screen.refresh()
|
||||
story_list = []
|
||||
tasks = []
|
||||
#async with self.session as session:
|
||||
session = self.session
|
||||
for idx, i in enumerate(self.topstories[from_pos:to_pos]):
|
||||
for i in self.topstories[from_pos:to_pos]:
|
||||
tasks.append(asyncio.ensure_future(api.get_story(session, i)))
|
||||
story_list = await asyncio.gather(*tasks)
|
||||
for story in story_list:
|
||||
@ -178,7 +204,6 @@ class Client:
|
||||
self.set_footer("Reloading...")
|
||||
self.screen.refresh()
|
||||
self.topstories = api.get_topstories()
|
||||
#self.loadedstories = []
|
||||
self.story_pos = 0
|
||||
self.cursor_pos = 0
|
||||
await self.load_stories(self.cursor_pos, self.cursor_pos + self.stories_in_a_site)
|
||||
@ -186,7 +211,6 @@ class Client:
|
||||
def main():
|
||||
try:
|
||||
client = Client()
|
||||
#async with client.run():
|
||||
asyncio.run(client.run())
|
||||
except Exception as e:
|
||||
curses.endwin()
|
||||
|
Loading…
Reference in New Issue
Block a user