Compare commits
No commits in common. "1fc2d55983da0dcc196ecde61966dffdc3232676" and "bf575bf881ad222bec2d5c7880c4a5b34b301672" have entirely different histories.
1fc2d55983
...
bf575bf881
42
hn.py
42
hn.py
@ -23,7 +23,6 @@ class Client:
|
|||||||
curses.noecho()
|
curses.noecho()
|
||||||
curses.cbreak()
|
curses.cbreak()
|
||||||
self.screen.keypad(True)
|
self.screen.keypad(True)
|
||||||
curses.curs_set(0)
|
|
||||||
|
|
||||||
self.topstories = api.get_topstories()
|
self.topstories = api.get_topstories()
|
||||||
self.loadedstories = {}
|
self.loadedstories = {}
|
||||||
@ -89,19 +88,15 @@ class Client:
|
|||||||
|
|
||||||
page = int(self.story_pos / self.stories_in_a_site + 1)
|
page = int(self.story_pos / self.stories_in_a_site + 1)
|
||||||
total_pages = math.ceil(500/len(self.loadedstories))
|
total_pages = math.ceil(500/len(self.loadedstories))
|
||||||
footer_text = f'Page {page}/{total_pages}, loaded {len(self.loadedstories)} stories. [?] for help.'
|
self.set_footer(f'Page {page}/{total_pages}, loaded {len(self.loadedstories)} stories.')
|
||||||
if len(footer_text) >= self.cols:
|
#self.set_footer(f'{self.loadedstories}')
|
||||||
footer_text = footer_text[:self.cols - 1]
|
|
||||||
self.set_footer(footer_text)
|
|
||||||
|
|
||||||
async def handle_input(self):
|
async def handle_input(self):
|
||||||
c = self.screen.getch()
|
c = self.screen.getch()
|
||||||
story = self.loadedstories[self.topstories[self.story_pos + self.cursor_pos]]
|
story = self.loadedstories[self.topstories[self.story_pos + self.cursor_pos]]
|
||||||
|
|
||||||
if c == ord('q'): # Quit
|
if c == ord('q'): # Quit
|
||||||
await self.exit()
|
await self.exit()
|
||||||
|
elif c == curses.KEY_UP:
|
||||||
elif c == curses.KEY_UP or c == ord('k'):
|
|
||||||
self.cursor_pos -= 1
|
self.cursor_pos -= 1
|
||||||
if self.cursor_pos < 0:
|
if self.cursor_pos < 0:
|
||||||
self.cursor_pos = self.stories_in_a_site-1
|
self.cursor_pos = self.stories_in_a_site-1
|
||||||
@ -109,7 +104,7 @@ class Client:
|
|||||||
self.story_pos -= self.stories_in_a_site
|
self.story_pos -= self.stories_in_a_site
|
||||||
self.story_pos = 0 if self.story_pos < 0 else self.story_pos
|
self.story_pos = 0 if self.story_pos < 0 else self.story_pos
|
||||||
|
|
||||||
elif c == curses.KEY_DOWN or c == ord('j'):
|
elif c == curses.KEY_DOWN:
|
||||||
self.cursor_pos += 1
|
self.cursor_pos += 1
|
||||||
if self.cursor_pos >= self.stories_in_a_site:
|
if self.cursor_pos >= self.stories_in_a_site:
|
||||||
self.cursor_pos = 0
|
self.cursor_pos = 0
|
||||||
@ -138,29 +133,6 @@ class Client:
|
|||||||
self.stories_in_a_site = self.lines - 3
|
self.stories_in_a_site = self.lines - 3
|
||||||
await self.load_more_if_needed()
|
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):
|
async def load_more_if_needed(self):
|
||||||
|
|
||||||
@ -169,12 +141,14 @@ class Client:
|
|||||||
await self.load_stories(self.story_pos, self.story_pos + self.stories_in_a_site)
|
await self.load_stories(self.story_pos, self.story_pos + self.stories_in_a_site)
|
||||||
|
|
||||||
async def load_stories(self, from_pos, to_pos):
|
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.set_footer(f'Loading stories...')
|
||||||
self.screen.refresh()
|
self.screen.refresh()
|
||||||
story_list = []
|
story_list = []
|
||||||
tasks = []
|
tasks = []
|
||||||
|
#async with self.session as session:
|
||||||
session = self.session
|
session = self.session
|
||||||
for i in self.topstories[from_pos:to_pos]:
|
for idx, i in enumerate(self.topstories[from_pos:to_pos]):
|
||||||
tasks.append(asyncio.ensure_future(api.get_story(session, i)))
|
tasks.append(asyncio.ensure_future(api.get_story(session, i)))
|
||||||
story_list = await asyncio.gather(*tasks)
|
story_list = await asyncio.gather(*tasks)
|
||||||
for story in story_list:
|
for story in story_list:
|
||||||
@ -204,6 +178,7 @@ class Client:
|
|||||||
self.set_footer("Reloading...")
|
self.set_footer("Reloading...")
|
||||||
self.screen.refresh()
|
self.screen.refresh()
|
||||||
self.topstories = api.get_topstories()
|
self.topstories = api.get_topstories()
|
||||||
|
#self.loadedstories = []
|
||||||
self.story_pos = 0
|
self.story_pos = 0
|
||||||
self.cursor_pos = 0
|
self.cursor_pos = 0
|
||||||
await self.load_stories(self.cursor_pos, self.cursor_pos + self.stories_in_a_site)
|
await self.load_stories(self.cursor_pos, self.cursor_pos + self.stories_in_a_site)
|
||||||
@ -211,6 +186,7 @@ class Client:
|
|||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
client = Client()
|
client = Client()
|
||||||
|
#async with client.run():
|
||||||
asyncio.run(client.run())
|
asyncio.run(client.run())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
|
Loading…
Reference in New Issue
Block a user