Compare commits

...

2 Commits

Author SHA1 Message Date
bcb7cfc4f3 fix width being one off 2023-08-31 23:13:27 +02:00
098f2a9666 remove embarassing logging 2023-08-31 17:57:22 +02:00

24
hn.py
View File

@ -46,7 +46,7 @@ class Client:
self.stories_in_a_site = self.lines - 3
title_string = '[Y] Hacker News'
title_string +=' ' * (self.cols - len(title_string) - 1) + '\n'
title_string += ' ' * (self.cols - len(title_string))
self.screen.addstr(title_string, curses.color_pair(1))
@ -55,14 +55,15 @@ class Client:
prefix = '>' if i == self.cursor_pos else ''
# calculate length of line
text = f'{prefix} ()\n'
text = f'{prefix} ()'
chars_available = self.cols - len(text)
max_title_len = min((chars_available//4)*3, 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
link = link[:max_url_len - 1] + "" if len(link) > max_url_len else link
self.screen.addstr(prefix)
if i == self.cursor_pos:
@ -70,7 +71,9 @@ class Client:
self.screen.addstr(title, curses.A_DIM | curses.A_UNDERLINE)
else:
self.screen.addstr(title, curses.A_UNDERLINE)
self.screen.addstr(f' ({link})\n', curses.A_DIM )
self.screen.addstr(f' ({link})', curses.A_DIM )
if len(text) + len(title) + len(link) + 1 < self.cols:
self.screen.addstr('\n')
detail = f' by {story.author} | {story.comments} comments | {story.votes} points\n'
self.screen.addstr(detail)
else:
@ -78,7 +81,10 @@ class Client:
self.screen.addstr(title, curses.A_DIM)
else:
self.screen.addstr(title)
self.screen.addstr(f' ({link})\n', curses.A_DIM )
self.screen.addstr(f' ({link})', curses.A_DIM )
if len(text) + len(title) + len(link) + 1 < self.cols:
self.screen.addstr('\n')
page = int(self.story_pos / self.stories_in_a_site + 1)
total_pages = math.ceil(len(self.loadedstories)/500)
@ -117,25 +123,21 @@ class Client:
else:
webbrowser.open(story.link)
story.read = True
elif c == ord('r'):
await self.reload()
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
with open('log', 'a') as f:
f.write(f'resizing!!!')
await self.load_more_if_needed()
async def load_more_if_needed(self):
with open('log', 'a') as f:
f.write(f'len: {len(self.loadedstories)} res: { self.story_pos + self.stories_in_a_site}\n')
if len(self.loadedstories) < self.story_pos + self.stories_in_a_site:
# load more
with open('log', 'a') as f:
f.write('loading more...\n')
await self.load_stories(self.story_pos, self.story_pos + self.stories_in_a_site)
async def load_stories(self, from_pos, to_pos):