This commit is contained in:
Michael Lazar
2015-04-02 20:52:56 -07:00
4 changed files with 62 additions and 8 deletions

View File

@@ -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():

View File

@@ -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):
"""

View File

@@ -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

View File

@@ -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):