From 27157f400e64822b631fd40a10e96f96a14d2b1b Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sat, 7 Mar 2015 14:03:15 -0800 Subject: [PATCH] Fixed help message crashing on small terminals. --- rtv/main.py | 29 ++++------------------------- rtv/utils.py | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 45 deletions(-) diff --git a/rtv/main.py b/rtv/main.py index 701b1d4..ef00a4d 100644 --- a/rtv/main.py +++ b/rtv/main.py @@ -4,7 +4,7 @@ import praw from requests.exceptions import ConnectionError, HTTPError from rtv.errors import SubmissionURLError, SubredditNameError -from rtv.utils import curses_session, load_config +from rtv.utils import curses_session, load_config, HELP from rtv.subreddit import SubredditPage from rtv.submission import SubmissionPage @@ -15,38 +15,17 @@ terminal window. # TODO: Figure out a way to sync this with the README epilog = """ -Usage +Controls ----- RTV currently supports browsing both subreddits and individual submissions. In each mode the controls are slightly different. In subreddit mode you can browse through the top submissions on either the front page or a specific subreddit. In submission mode you can view the self text for a submission and browse comments. - -Global Commands - Arrow Keys or `hjkl`: RTV supports both the arrow keys and vim bindings for - navigation. Move up and down to scroll through items - on the page. - `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 the 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. For - example, pressing `/` and typing "python" will open - "/r/python". You can return to Reddit's front page - by using the alias "/r/front". - -Submission Mode - Right or `Enter` : Toggle the currently selected comment between hidden - and visible. Alternatively, load addition comments - identified by "[+] more comments". - Left : Exit the submission page and return to the subreddit. """ +epilog += HELP + def main(): parser = argparse.ArgumentParser( diff --git a/rtv/utils.py b/rtv/utils.py index 0eac40e..899904b 100644 --- a/rtv/utils.py +++ b/rtv/utils.py @@ -16,25 +16,23 @@ 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. +HELP = """ +Global Commands + `UP/DOWN` or `j/k` : Scroll to the prev/next item + `r` or `F5` : Refresh the current page + `q` : Quit the program + `o` : Open 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. + `RIGHT` or `l` : View comments for the selected submission + `/` : Open a prompt to switch subreddits Submission Mode - Right or `Enter` : Toggle the currently selected comment between hidden - and visible. - Left : Exit the submission page and return to the subreddit. + `LEFT` or `h` : Return to subreddit mode + `RIGHT` or `l` : Fold the selected comment, or load additional comments """ - class Color(object): COLORS = { @@ -139,17 +137,24 @@ def display_message(stdscr, message): n_rows, n_cols = stdscr.getmaxyx() - box_width = max(map(len, message)) - box_height = len(message) + box_width = max(map(len, message)) + 2 + box_height = len(message) + 2 + + # Make sure the window is large enough to fit the message + # TODO: Should find a better way to display the message in this situation + if (box_width > n_cols) or (box_height > n_rows): + curses.flash() + return + 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) + s_col = (n_cols - box_width) // 2 + window = stdscr.derwin(box_height, box_width, s_row, s_col) window.erase() window.border() - for i in range(box_height): - window.addstr(i + 1, 1, message[i]) + for index, line in enumerate(message, start=1): + window.addstr(index, 1, line) window.refresh() stdscr.getch() @@ -163,7 +168,7 @@ 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") + help_msgs = HELP.split("\n") display_message(stdscr, help_msgs)