diff --git a/rtv/__main__.py b/rtv/__main__.py index bf8779c..44b5bd8 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -15,6 +15,7 @@ from .curses_helpers import curses_session from .submission import SubmissionPage from .subreddit import SubredditPage from .docs import * +from .__version__ import __version__ __all__ = [] @@ -68,6 +69,13 @@ def main(): args = command_line() local_config = load_config() + # set the terminal title + title = 'rtv {0}'.format(__version__) + if os.name == 'nt': + os.system('title {0}'.format(title)) + else: + sys.stdout.write("\x1b]2;{0}\x07".format(title)) + # Fill in empty arguments with config file values. Paramaters explicitly # typed on the command line will take priority over config file params. for key, val in local_config.items(): diff --git a/rtv/curses_helpers.py b/rtv/curses_helpers.py index f9a2085..ee9fdaf 100644 --- a/rtv/curses_helpers.py +++ b/rtv/curses_helpers.py @@ -52,12 +52,14 @@ def show_notification(stdscr, message): for index, line in enumerate(message, start=1): window.addstr(index, 1, line) window.refresh() - stdscr.getch() + ch = stdscr.getch() window.clear() window = None stdscr.refresh() + return ch + def show_help(stdscr): """ diff --git a/rtv/docs.py b/rtv/docs.py index 025a9e6..988583a 100644 --- a/rtv/docs.py +++ b/rtv/docs.py @@ -31,6 +31,7 @@ Global Commands `r` : Refresh the current page `q` : Quit the program `ENTER` or `o` : Open the selected item in the default web browser + `u` : Log in `?` : Show this help message Subreddit Mode diff --git a/rtv/page.py b/rtv/page.py index 4a30465..70d13f6 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -6,6 +6,7 @@ import praw.errors from .helpers import clean from .curses_helpers import Color, show_notification, show_help, text_input +from .docs import AGENT __all__ = ['Navigator'] @@ -226,17 +227,59 @@ class BasePage(object): except praw.errors.LoginOrScopeRequired: show_notification(self.stdscr, ['Login to vote']) - def prompt_input(self, prompt): + @BaseController.register('u') + def login(self): + """ + Prompt to log into the user's account. Log out if the user is already + logged in. + """ + + if self.reddit.is_logged_in(): + self.logout() + return + + username = self.prompt_input('Enter username:') + password = self.prompt_input('Enter password:', hide=True) + if not username or not password: + curses.flash() + return + + try: + self.reddit.login(username, password) + except praw.errors.InvalidUserPass: + show_notification(self.stdscr, ['Invalid user/pass']) + else: + show_notification(self.stdscr, ['Logged in']) + + def logout(self): + """ + Prompt to log out of the user's account. + """ + + ch = self.prompt_input("Log out? (y/n):") + if ch == 'y': + self.reddit.clear_authentication() + show_notification(self.stdscr, ['Logged out']) + elif ch != 'n': + curses.flash() + + def prompt_input(self, prompt, hide=False): """Prompt the user for input""" attr = curses.A_BOLD | Color.CYAN n_rows, n_cols = self.stdscr.getmaxyx() - self.stdscr.addstr(n_rows - 1, 0, prompt, attr) - self.stdscr.refresh() - window = self.stdscr.derwin(1, n_cols - len(prompt), - n_rows - 1, len(prompt)) - window.attrset(attr) - out = text_input(window) + if hide: + prompt += ' ' * (n_cols - len(prompt) - 1) + self.stdscr.addstr(n_rows-1, 0, prompt, attr) + out = self.stdscr.getstr(n_rows-1, 1) + else: + self.stdscr.addstr(n_rows - 1, 0, prompt, attr) + self.stdscr.refresh() + window = self.stdscr.derwin(1, n_cols - len(prompt), + n_rows - 1, len(prompt)) + window.attrset(attr) + out = text_input(window) + return out def draw(self):