|
|
@ -15,34 +15,45 @@ import api
|
|
|
|
spinner_states = ['-', '\\', '|', '/']
|
|
|
|
spinner_states = ['-', '\\', '|', '/']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def footer(stdscr, content):
|
|
|
|
class Client:
|
|
|
|
stdscr.addstr(curses.LINES-1, 0, content, curses.A_REVERSE)
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
# set up curses
|
|
|
|
|
|
|
|
self.screen = curses.initscr()
|
|
|
|
def main(stdscr):
|
|
|
|
curses.noecho()
|
|
|
|
stdscr.clear()
|
|
|
|
curses.cbreak()
|
|
|
|
|
|
|
|
self.screen.keypad(True)
|
|
|
|
_, width = stdscr.getmaxyx();
|
|
|
|
|
|
|
|
num_stories = curses.LINES - 3 # headline, detail, footer
|
|
|
|
self.topstories = api.get_topstories()
|
|
|
|
|
|
|
|
self.loadedstories = []
|
|
|
|
topstories = api.get_topstories()
|
|
|
|
self.story_pos = 0
|
|
|
|
stories = []
|
|
|
|
self.cursor_pos = 0
|
|
|
|
for idx, i in enumerate(topstories[:num_stories]):
|
|
|
|
self.lines = curses.LINES
|
|
|
|
stdscr.clear()
|
|
|
|
self.cols = curses.COLS
|
|
|
|
footer(stdscr, f'[{spinner_states[idx%4]}] Loading stories...')
|
|
|
|
self.stories_in_a_site = self.lines - 3
|
|
|
|
stdscr.refresh()
|
|
|
|
|
|
|
|
stories.append(api.get_story(i))
|
|
|
|
def load_stories(self, from_pos, to_pos):
|
|
|
|
|
|
|
|
for idx, i in enumerate(self.topstories[from_pos:to_pos]):
|
|
|
|
# Display list of stories in terminal window with arrow key navigation
|
|
|
|
#stdscr.clear()
|
|
|
|
current_pos = 0
|
|
|
|
self.set_footer(f'[{spinner_states[idx%4]}] Loading stories...')
|
|
|
|
while True:
|
|
|
|
self.screen.refresh()
|
|
|
|
stdscr.clear()
|
|
|
|
self.loadedstories.append(api.get_story(i))
|
|
|
|
stdscr.addstr('Hacker News Top Stories:\n')
|
|
|
|
|
|
|
|
for i, story in enumerate(stories):
|
|
|
|
def set_footer(self, footer):
|
|
|
|
prefix = '>>> ' if i == current_pos else ' '
|
|
|
|
self.screen.addstr(curses.LINES - 1, 0, footer, curses.A_REVERSE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw(self):
|
|
|
|
|
|
|
|
self.screen.clear()
|
|
|
|
|
|
|
|
self.lines = curses.LINES
|
|
|
|
|
|
|
|
self.cols = curses.COLS
|
|
|
|
|
|
|
|
# header, detail, footer:
|
|
|
|
|
|
|
|
self.stories_in_a_site = self.lines - 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.screen.addstr('Hacker News Top Stories:\n')
|
|
|
|
|
|
|
|
for i, story in enumerate(self.loadedstories[self.story_pos:self.story_pos + self.stories_in_a_site]):
|
|
|
|
|
|
|
|
prefix = '>>> ' if i == self.cursor_pos else ' '
|
|
|
|
# calculate length of line
|
|
|
|
# calculate length of line
|
|
|
|
text = f'{prefix} ()\n'
|
|
|
|
text = f'{prefix} ()\n'
|
|
|
|
chars_available = width - len(text)
|
|
|
|
chars_available = self.cols - len(text)
|
|
|
|
max_title_len = min((chars_available//3)*2, len(story.title))
|
|
|
|
max_title_len = min((chars_available//3)*2, len(story.title))
|
|
|
|
max_url_len = chars_available - max_title_len
|
|
|
|
max_url_len = chars_available - max_title_len
|
|
|
|
|
|
|
|
|
|
|
@ -51,27 +62,43 @@ def main(stdscr):
|
|
|
|
link = link[:max_url_len-1] + "…" if len(link) > max_url_len else link
|
|
|
|
link = link[:max_url_len-1] + "…" if len(link) > max_url_len else link
|
|
|
|
|
|
|
|
|
|
|
|
text = '{}{} ({})\n'.format(prefix, title, link.replace('https://', '').replace('http://', ''))
|
|
|
|
text = '{}{} ({})\n'.format(prefix, title, link.replace('https://', '').replace('http://', ''))
|
|
|
|
stdscr.addstr(text)
|
|
|
|
self.screen.addstr(text)
|
|
|
|
if i == current_pos:
|
|
|
|
if i == self.cursor_pos:
|
|
|
|
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
|
|
|
|
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
|
|
|
|
stdscr.addstr(detail)
|
|
|
|
self.screen.addstr(detail)
|
|
|
|
footer(stdscr, f'Loaded {num_stories} stories.')
|
|
|
|
self.set_footer(f'Loaded {self.stories_in_a_site} stories.')
|
|
|
|
|
|
|
|
|
|
|
|
stdscr.refresh()
|
|
|
|
def handle_input(self):
|
|
|
|
c = stdscr.getch()
|
|
|
|
c = self.screen.getch()
|
|
|
|
if c == ord('q'): # Quit
|
|
|
|
if c == ord('q'): # Quit
|
|
|
|
break
|
|
|
|
self.exit()
|
|
|
|
elif c == curses.KEY_UP:
|
|
|
|
elif c == curses.KEY_UP:
|
|
|
|
current_pos -= 1
|
|
|
|
self.cursor_pos -= 1
|
|
|
|
if current_pos < 0:
|
|
|
|
if self.cursor_pos < 0:
|
|
|
|
current_pos = len(stories)-1
|
|
|
|
self.cursor_pos = self.stories_in_a_site-1
|
|
|
|
elif c == curses.KEY_DOWN:
|
|
|
|
elif c == curses.KEY_DOWN:
|
|
|
|
current_pos += 1
|
|
|
|
self.cursor_pos += 1
|
|
|
|
if current_pos >= len(stories):
|
|
|
|
if self.cursor_pos >= self.stories_in_a_site:
|
|
|
|
current_pos = 0
|
|
|
|
self.cursor_pos = 0
|
|
|
|
elif c == ord('c'):
|
|
|
|
elif c == ord('c'):
|
|
|
|
webbrowser.open(f'https://news.ycombinator.com/item?id={stories[current_pos].id}')
|
|
|
|
webbrowser.open(f'https://news.ycombinator.com/item?id={self.loadedstories[self.story_pos + self.cursor_pos].id}')
|
|
|
|
elif c == curses.KEY_ENTER or c == 10:
|
|
|
|
elif c == curses.KEY_ENTER or c == 10:
|
|
|
|
webbrowser.open(stories[current_pos].link)
|
|
|
|
webbrowser.open(self.loadedstories[self.story_pos + self.cursor_pos].link)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
|
|
|
self.load_stories(0, self.stories_in_a_site)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
self.handle_input()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exit(self):
|
|
|
|
|
|
|
|
curses.endwin()
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
client = Client()
|
|
|
|
|
|
|
|
client.run()
|
|
|
|
|
|
|
|
|
|
|
|
curses.wrapper(main)
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
main()
|
|
|
|