diff --git a/rtv/main.py b/rtv/main.py index 8f7ccd5..701b1d4 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -31,6 +31,7 @@ Global Commands `q` : Quit the program. `o` : Open the url of the selected item in the default web browser. + `?` : Show the help message. Subreddit Mode Right or `Enter` : Open the currently selected submission in a new page. diff --git a/rtv/submission.py b/rtv/submission.py index 240e438..c23e637 100644 --- a/rtv/submission.py +++ b/rtv/submission.py @@ -6,7 +6,7 @@ import six from .content import SubmissionContent from .page import BasePage -from .utils import LoadScreen, Color, ESCAPE +from .utils import LoadScreen, Color, ESCAPE, display_help class SubmissionPage(BasePage): @@ -54,6 +54,10 @@ class SubmissionPage(BasePage): elif cmd in (ESCAPE, curses.KEY_LEFT, ord('h')): break + elif cmd == ord('?'): + display_help(self.stdscr) + self.draw() + elif cmd == ord('q'): sys.exit() diff --git a/rtv/subreddit.py b/rtv/subreddit.py index 59cc3f2..31bd0d2 100644 --- a/rtv/subreddit.py +++ b/rtv/subreddit.py @@ -7,7 +7,7 @@ from .errors import SubredditNameError from .page import BasePage from .submission import SubmissionPage from .content import SubredditContent -from .utils import LoadScreen, text_input, display_message, Color, ESCAPE +from .utils import LoadScreen, text_input, display_message, Color, ESCAPE, display_help class SubredditPage(BasePage): @@ -52,6 +52,10 @@ class SubredditPage(BasePage): elif cmd == curses.KEY_RESIZE: self.draw() + elif cmd == ord('?'): + display_help(self.stdscr) + self.draw() + elif cmd == ord('q'): sys.exit() @@ -67,7 +71,7 @@ class SubredditPage(BasePage): self.reddit, name, self.loader) except (SubredditNameError, HTTPError): - display_message(self.stdscr, 'Invalid Subreddit') + display_message(self.stdscr, ['Invalid Subreddit']) else: self.nav.page_index, self.nav.cursor_index = 0, 0 @@ -132,4 +136,4 @@ class SubredditPage(BasePage): text = '{author}'.format(**data) win.addnstr(row, 1, text, n_cols-1, curses.A_BOLD) text = ' {subreddit}'.format(**data) - win.addnstr(text, n_cols - win.getyx()[1], Color.YELLOW) \ No newline at end of file + win.addnstr(text, n_cols - win.getyx()[1], Color.YELLOW) diff --git a/rtv/utils.py b/rtv/utils.py index 94b6045..0eac40e 100644 --- a/rtv/utils.py +++ b/rtv/utils.py @@ -16,6 +16,25 @@ from .errors import EscapePressed ESCAPE = 27 +help_msg = """Global Commands + Arrow Keys or `hjkl`: Navigations. + `r` or `F5` : Refresh the current page. + `q` : Quit the program. + `o` : Open the url of the selected item in the default web + browser. + `?` : Show this help message. + +Subreddit Mode + Right or `Enter` : Open the currently selected submission in a new page. + `/` : Open a prompt to switch to a different subreddit. + +Submission Mode + Right or `Enter` : Toggle the currently selected comment between hidden + and visible. + Left : Exit the submission page and return to the subreddit. +""" + + class Color(object): COLORS = { @@ -118,17 +137,21 @@ def text_input(window): def display_message(stdscr, message): "Display a message box at the center of the screen and wait for a keypress" - message_len = len(message) n_rows, n_cols = stdscr.getmaxyx() - s_row = (n_rows - 2) // 2 - s_col = (n_cols - message_len - 1) // 2 - window = stdscr.derwin(3, message_len+2, s_row, s_col) + + box_width = max(map(len, message)) + box_height = len(message) + s_row = (n_rows - box_height) // 2 + s_col = (n_cols - box_width - 1) // 2 + window = stdscr.derwin(box_height + 2, box_width + 3, s_row, s_col) window.erase() window.border() - window.addstr(1, 1, message) - window.refresh() + for i in range(box_height): + window.addstr(i + 1, 1, message[i]) + + window.refresh() stdscr.getch() window.clear() @@ -136,6 +159,14 @@ def display_message(stdscr, message): stdscr.refresh() +def display_help(stdscr): + """Display a help message box at the center of the screen and wait for a + keypress""" + + help_msgs = help_msg.split("\n") + display_message(stdscr, help_msgs) + + class LoadScreen(object): def __init__(self, stdscr): @@ -250,4 +281,4 @@ def curses_session(): stdscr.keypad(0) curses.echo() curses.nocbreak() - curses.endwin() \ No newline at end of file + curses.endwin()