You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.4 KiB
Python

#!/usr/bin/env python3
#TODO: make sure things dont explode no matter what terminal size
# x prevent linebreaks in a single story
# x only load as many stories as fit
# -> refresh on resize
import requests
from bs4 import BeautifulSoup as Soup
import curses
import webbrowser
import api
spinner_states = ['-', '\\', '|', '/']
class Client:
def __init__(self):
# set up curses
self.screen = curses.initscr()
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, 255, 208)
curses.noecho()
curses.cbreak()
self.screen.keypad(True)
self.topstories = api.get_topstories()
self.loadedstories = []
self.story_pos = 0
self.cursor_pos = 0
self.lines = curses.LINES
self.cols = curses.COLS
self.stories_in_a_site = self.lines - 3
def load_stories(self, from_pos, to_pos):
for idx, i in enumerate(self.topstories[from_pos:to_pos]):
#stdscr.clear()
self.set_footer(f'[{spinner_states[idx%4]}] Loading { to_pos - from_pos } stories...')
self.screen.refresh()
self.loadedstories.append(api.get_story(i))
def set_footer(self, footer):
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
title_string = '[Y] Hacker News'
title_string +=' ' * ( self.cols - len(title_string) - 1) + '\n'
self.screen.addstr(title_string, curses.color_pair(1))
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
text = f'{prefix} ()\n'
chars_available = self.cols - len(text)
max_title_len = min((chars_available//3)*2, len(story.title))
max_url_len = chars_available - max_title_len
title = story.title[:max_title_len-1] + "" if len(story.title) > max_title_len else story.title
link = story.link.replace('https://', '').replace('http://', '')
link = link[:max_url_len-1] + "" if len(link) > max_url_len else link
#text = '{}{} ({})\n'.format(prefix, title, link)
self.screen.addstr(prefix)
if i == self.cursor_pos:
self.screen.addstr(title, curses.A_UNDERLINE)
self.screen.addstr(f' ({link})\n', curses.A_DIM )
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
self.screen.addstr(detail)
else:
self.screen.addstr(title)
self.screen.addstr(f' ({link})\n', curses.A_DIM )
self.set_footer(f'Loaded {len(self.loadedstories)} stories.')
def handle_input(self):
c = self.screen.getch()
if c == ord('q'): # Quit
self.exit()
elif c == curses.KEY_UP:
self.cursor_pos -= 1
if self.cursor_pos < 0:
self.cursor_pos = self.stories_in_a_site-1
elif c == curses.KEY_DOWN:
self.cursor_pos += 1
if self.cursor_pos >= self.stories_in_a_site:
self.cursor_pos = 0
elif c == ord('c'):
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:
webbrowser.open(self.loadedstories[self.story_pos + self.cursor_pos].link)
elif c == curses.KEY_RESIZE:
curses.resize_term(*self.screen.getmaxyx())
self.lines, self.cols = self.screen.getmaxyx()
self.stories_in_a_site = self.lines - 3
if len(self.loadedstories) < self.story_pos + self.stories_in_a_site:
# load more
self.load_stories(len(self.loadedstories), self.story_pos + self.stories_in_a_site)
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():
try:
client = Client()
client.run()
except Exception as e:
curses.endwin()
raise e
if __name__ == '__main__':
main()