Code is ugly, but cursor is working.

This commit is contained in:
Michael Lazar
2015-01-23 01:25:30 -08:00
parent b468a11fa8
commit c033b2d325
2 changed files with 87 additions and 40 deletions

View File

@@ -1,6 +1,14 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
import textwrap import textwrap
def clean(unicode_string):
"""
Convert unicode string into ascii-safe characters.
"""
return unicode_string.encode('ascii', 'replace').replace('\\', '')
def strip_subreddit_url(permalink): def strip_subreddit_url(permalink):
""" """
Grab the subreddit from the permalink because submission.subreddit.url Grab the subreddit from the permalink because submission.subreddit.url
@@ -10,13 +18,6 @@ def strip_subreddit_url(permalink):
subreddit = clean(permalink).split('/')[4] subreddit = clean(permalink).split('/')[4]
return '/r/{}'.format(subreddit) return '/r/{}'.format(subreddit)
def clean(unicode_string):
"""
Convert unicode string into ascii-safe characters.
"""
return unicode_string.encode('ascii', 'replace').replace('\\', '')
def humanize_timestamp(utc_timestamp, verbose=False): def humanize_timestamp(utc_timestamp, verbose=False):
""" """
@@ -139,8 +140,8 @@ class SubredditGenerator(object):
return data return data
def iterate(self, index, n_cols): def iterate(self, index, step, n_cols):
while True: while True:
yield self.get(index, n_cols) yield self.get(index, n_cols)
index += 1 index += step

View File

@@ -19,6 +19,8 @@ class SubredditViewer(object):
self._title_window = None self._title_window = None
self._content_window = None self._content_window = None
self._sub_windows = [] self._sub_windows = []
self._direction = True
self._window_is_partial = None
self.draw() self.draw()
@@ -29,11 +31,17 @@ class SubredditViewer(object):
# Move cursor up one submission # Move cursor up one submission
if cmd == curses.KEY_UP: if cmd == curses.KEY_UP:
self.move_cursor(-1) if self._direction:
self.move_cursor_backward()
else:
self.move_cursor_forward()
# Move cursor down one submission # Move cursor down one submission
elif cmd == curses.KEY_DOWN: elif cmd == curses.KEY_DOWN:
self.move_cursor(1) if self._direction:
self.move_cursor_forward()
else:
self.move_cursor_backward()
# View submission # View submission
elif cmd in (curses.KEY_RIGHT, ord(' ')): elif cmd in (curses.KEY_RIGHT, ord(' ')):
@@ -65,17 +73,44 @@ class SubredditViewer(object):
self.draw_content() self.draw_content()
self.draw_cursor() self.draw_cursor()
def move_cursor(self, delta): def move_cursor_forward(self):
new_index = self._cursor_index + delta
if new_index < 0:
curses.flash()
return
self.remove_cursor() self.remove_cursor()
self._cursor_index += delta
last_index = len(self._sub_windows) - 1
self._cursor_index += 1
if self._cursor_index == last_index:
if self._direction:
self._page_index = self._page_index + self._cursor_index
self._cursor_index = 0
self._direction = False
else:
self._page_index = self._page_index - self._cursor_index
self._cursor_index = 0
self._direction = True
self.draw_content()
self.draw_cursor()
def move_cursor_backward(self):
self.remove_cursor()
last_index = len(self._sub_windows) - 1
self._cursor_index -= 1
if self._cursor_index < 0:
if self._direction:
self._page_index -= 1
self._cursor_index = 0
else:
self._page_index += 1
self._cursor_index = 0
self.draw_content()
self.draw_cursor() self.draw_cursor()
def draw_cursor(self): def draw_cursor(self):
@@ -103,16 +138,30 @@ class SubredditViewer(object):
self._content_window.erase() self._content_window.erase()
self._sub_windows = [] self._sub_windows = []
if self._direction:
row = 0 row = 0
for data in self.gen.iterate(self._page_index, cols-1): for data in self.gen.iterate(self._page_index, 1, cols-1):
n_rows = min(rows-row, data['n_rows']) available_rows = (rows - row)
n_rows = min(available_rows, data['n_rows'])
window = self._content_window.derwin(n_rows, cols, row, 0) window = self._content_window.derwin(n_rows, cols, row, 0)
self.draw_submission(window, data) self.draw_submission(window, data, self._direction)
self._sub_windows.append(window) self._sub_windows.append(window)
row += n_rows + 1 row += (n_rows + 1)
if row >= rows: if row >= rows:
break break
else:
row = rows
for data in self.gen.iterate(self._page_index, -1, cols-1):
available_rows = row
n_rows = min(available_rows, data['n_rows'])
window = self._content_window.derwin(n_rows, cols, row-n_rows, 0)
self.draw_submission(window, data, self._direction)
self._sub_windows.append(window)
row -= (n_rows + 1)
if row < 0:
break
self._window_is_partial = (available_rows < data['n_rows'])
self._content_window.refresh() self._content_window.refresh()
def draw_header(self): def draw_header(self):
@@ -122,34 +171,31 @@ class SubredditViewer(object):
self._title_window.refresh() self._title_window.refresh()
@staticmethod @staticmethod
def draw_submission(win, data, top_down=True): def draw_submission(win, data, direction):
n_rows, n_cols = win.getmaxyx() n_rows, n_cols = win.getmaxyx()
n_cols -= 1 # Leave space for the cursor in the first column n_cols -= 1 # Leave space for the cursor in the first column
# Handle the case where the window is not large enough to fit the data. # Handle the case where the window is not large enough to fit the data.
valid_rows = range(0, n_rows) valid_rows = range(0, n_rows)
offset = 0 if top_down else -(data['n_rows'] - n_rows) offset = 0 if direction else -(data['n_rows'] - n_rows)
n_title = len(data['split_title']) n_title = len(data['split_title'])
for row, text in enumerate(data['split_title'], start=offset): for row, text in enumerate(data['split_title'], start=offset):
if row in valid_rows: if row in valid_rows:
win.addstr(row, 1, text) win.addstr(row, 1, text)
row = n_title row = n_title + offset
if row in valid_rows: if row in valid_rows:
win.addnstr(row, 1, '{url}'.format(**data), n_cols) win.addnstr(row, 1, '{url}'.format(**data), n_cols-1)
row = n_title + 1 row = n_title + offset + 1
if row in valid_rows: if row in valid_rows:
win.addnstr(row, 1, '{created} {comments} {score}'.format(**data), n_cols) win.addnstr(row, 1, '{created} {comments} {score}'.format(**data), n_cols-1)
row = n_title + 2 row = n_title + offset + 2
if row in valid_rows: if row in valid_rows:
win.addnstr(row, 1, '{author} {subreddit}'.format(**data), n_cols) win.addnstr(row, 1, '{author} {subreddit}'.format(**data), n_cols-1)
# DEBUG
win.refresh()
def main(): def main():