diff --git a/rtv/docs.py b/rtv/docs.py index 025a9e6..f6b0e3c 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 + `+` : Log in `?` : Show this help message Subreddit Mode diff --git a/rtv/page.py b/rtv/page.py index 4a30465..43f26c3 100644 --- a/rtv/page.py +++ b/rtv/page.py @@ -226,17 +226,43 @@ class BasePage(object): except praw.errors.LoginOrScopeRequired: show_notification(self.stdscr, ['Login to vote']) - def prompt_input(self, prompt): + @BaseController.register('+') + def login(self): + username = self.prompt_input('Enter username: ') + password = self.prompt_input('Enter password: ', hide=True) + + if (username == '') or (password == ''): + curses.flash() + return + try: + self.reddit.login(username, password) + + except praw.errors.InvalidUserPass: + show_notification(self.stdscr, + ['Invalid password for username: {}'.format(username)]) + else: + show_notification(self.stdscr, + ['Successfully logged in as: {}'.format(username)]) + + return + + 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: + self.stdscr.addstr(n_rows - 1, 0, prompt+' '*(n_rows-1-len(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):