def do_verification_prompt(self, callback): #global config stores the last user that logged in (and his password) settings = GlobalSettings.get() def make_textpad(): editWindow = self.stdscr.derwin(1, curses.COLS - startX, startY + 1, startX) editWindow.clear() textpad = curses.textpad.Textbox(editWindow) return textpad, editWindow #just directly login: self.username = str(settings.username) self.password = str(settings.password) self.savePass = settings.save_password startY = 1 startX = 0 while not self.username or not self.password: self.stdscr.addstr( startY, startX, 'Enter your username- use emacs bindings (ctrl-g to enter).') curses.curs_set(1) self.stdscr.refresh() textpad, win = make_textpad() #TODO: this should be in a thread self.username = textpad.edit().rstrip() win.clear() self.stdscr.addstr( startY, startX, 'Enter your password- use emacs bindings (ctrl-g to enter).') textpad, win = make_textpad() win.clear() self.stdscr.refresh() self.password = textpad.edit().rstrip() #check that the username is possibly valid: if not Globals.USERNAME_REGEX.match(self.username): log_msg( "Usernames can only contain A-Z, a-z, 0-9, -, _, and spaces in the middle", 0) self.username = None curses.curs_set(0) callback(self.username, self.password)
def _handle_command(window, state, uris, info, prior): """ Show the info on the bottom and let the user write text which is returned. :param info: Information shown before the command edit field. :param prior: Text that should be in the command field already. :raises ValueError: If something went wrong and there is no output. """ # Throw an exception if the window is too small. if state.width < len(info) + len(prior) + 1 or state.height < 4: raise ValueError() window.addstr(state.height - 1, 0, info) window.refresh() # Show and create the command window with the prior information. cmdwindow = curses.newwin(0, 0, state.height - 1, len(info)) cmdwindow.addstr(0, 0, prior) textpad = curses.textpad.Textbox(cmdwindow, insert_mode=True) def _clear(): window.addstr(state.height - 1, 0, ' ' * (state.width - 1)) window.refresh() curses.curs_set(0) def _validator(c): # Throw an exception if the window is resized while editing. if c == curses.KEY_RESIZE: _handle_resize(window, state, uris) _clear() raise ValueError() elif c == ord('q') or c == 27: _clear() raise ValueError() else: return c curses.curs_set(2) textpad.edit(_validator) curses.curs_set(0) return textpad.gather().strip()
def do_verification_prompt(self, callback): #global config stores the last user that logged in (and his password) settings = GlobalSettings.get() def make_textpad(): editWindow = self.stdscr.derwin(1, curses.COLS-startX, startY + 1, startX) editWindow.clear() textpad = curses.textpad.Textbox(editWindow) return textpad, editWindow #just directly login: self.username = str(settings.username) self.password = str(settings.password) self.savePass = settings.save_password startY = 1 startX = 0 while not self.username or not self.password: self.stdscr.addstr(startY, startX, 'Enter your username- use emacs bindings (ctrl-g to enter).') curses.curs_set(1) self.stdscr.refresh() textpad, win = make_textpad() #TODO: this should be in a thread self.username = textpad.edit().rstrip() win.clear() self.stdscr.addstr(startY, startX, 'Enter your password- use emacs bindings (ctrl-g to enter).') textpad, win = make_textpad() win.clear() self.stdscr.refresh() self.password = textpad.edit().rstrip() #check that the username is possibly valid: if not Globals.USERNAME_REGEX.match(self.username): log_msg("Usernames can only contain A-Z, a-z, 0-9, -, _, and spaces in the middle", 0) self.username = None curses.curs_set(0) callback(self.username, self.password)
def _editworld(worldwin, worldnames, world): import curses.textpad # if the world is empty, create a default world if not world: world = World() else: worldnames.remove(world.name) # create a new window as a subwindow of the old lines0, cols0 = worldwin.getmaxyx() lines = min(lines0 - 4, 17) cols = cols0 - 4 y = (lines0 - lines) / 2 editwin = worldwin.derwin(lines, cols, y, 2) editwin.keypad(1) editwin.erase() editwin.border() editwin.addstr( 0, 4, "| Editing World: %s |" % (world.name and world.name or '<new>'), \ curses.A_BOLD ) _showkeys(editwin, lines - 2, 2, '[d]elete [enter]/[e]dit - [Q]/[S]ave [A]bort') # now loop pos = 0 message, lastmessage = None, None while True: # Paint all the fields y, x = 2, 2 for i, field in enumerate(EDITFIELDS): editwin.addstr(y, x, field['name'] + ':', curses.A_BOLD) editwin.addnstr(y, x + 15, _getfield(world, field) + " " * 80, cols - 19, i == pos and curses.A_REVERSE or curses.A_NORMAL) y += 1 # get input while True: # show the help for the current item field = EDITFIELDS[pos] editwin.addnstr(y + 1, 2, field['help'] + (" " * 80), cols - 4) # get input editwin.move(2 + pos, 17) editwin.refresh() c = editwin.getkey() if c in ('i', 'KEY_UP', '\x10'): if pos > 0: pos -= 1 break elif c in ('j', 'KEY_DOWN', '\x0e'): if (pos + 1) < len(EDITFIELDS): pos += 1 break elif c in ('e', 'KEY_ENTER', ' ', '\n', 'd', 'KEY_DC'): value = _getfield(world, field) if c in ('d', 'KEY_DC'): value = '' elif 'edit' in field: value = field['edit'](world, value) else: # create a text editing box linewin = editwin.derwin(1, cols - 19, 2 + pos, 17) linewin.erase() if c != ' ': linewin.addstr(0, 0, value) textpad = curses.textpad.Textbox(linewin) curses.noecho() value = textpad.edit().strip() if ' ' in value: message = 'No spaces allowed in values' elif 'validate' in field: message = field['validate'](world, worldnames, value) if not message: _setfield(world, field, value) else: _setfield(world, field, value) break elif c in ('y', 'n', 'Y', 'N'): if 'yesno' in field: _setfield(world, field, c) break elif c in ('S', 'Q'): # return world only if it has a name if world.category != world.INVALID: return world else: return None elif c in ('A', 'KEY_ESC'): return None message, lastmessage = _show_message(editwin, message, lastmessage)
def _editworld( worldwin, worldnames, world ): import curses.textpad # if the world is empty, create a default world if not world: world = World() else: worldnames.remove( world.name ) # create a new window as a subwindow of the old lines0, cols0 = worldwin.getmaxyx() lines = min( lines0-4, 17 ) cols = cols0-4 y = (lines0-lines)/2 editwin = worldwin.derwin( lines, cols, y, 2 ) editwin.keypad( 1 ) editwin.erase() editwin.border() editwin.addstr( 0, 4, "| Editing World: %s |" % (world.name and world.name or '<new>'), \ curses.A_BOLD ) _showkeys( editwin, lines-2, 2, '[d]elete [enter]/[e]dit - [Q]/[S]ave [A]bort' ) # now loop pos=0 message, lastmessage = None, None while True: # Paint all the fields y, x = 2, 2 for i, field in enumerate( EDITFIELDS ): editwin.addstr( y, x, field['name']+':', curses.A_BOLD ) editwin.addnstr( y, x+15, _getfield( world, field ) + " "*80, cols-19, i==pos and curses.A_REVERSE or curses.A_NORMAL ) y += 1 # get input while True: # show the help for the current item field = EDITFIELDS[pos] editwin.addnstr( y+1, 2, field['help']+(" "*80), cols-4 ) # get input editwin.move( 2+pos, 17 ) editwin.refresh() c = editwin.getkey() if c in ( 'i', 'KEY_UP', '\x10' ): if pos>0: pos -= 1 break elif c in ( 'j', 'KEY_DOWN', '\x0e' ): if (pos+1) < len( EDITFIELDS ): pos += 1 break elif c in ( 'e', 'KEY_ENTER', ' ', '\n', 'd', 'KEY_DC' ): value = _getfield( world, field ) if c in ( 'd', 'KEY_DC' ): value = '' elif 'edit' in field: value = field['edit'](world, value ) else: # create a text editing box linewin = editwin.derwin( 1, cols-19, 2+pos, 17 ) linewin.erase() if c != ' ': linewin.addstr( 0, 0, value ) textpad = curses.textpad.Textbox( linewin ) curses.noecho() value = textpad.edit().strip() if ' ' in value: message = 'No spaces allowed in values' elif 'validate' in field: message = field['validate']( world, worldnames, value ) if not message: _setfield( world, field, value ) else: _setfield( world, field, value ) break elif c in ( 'y', 'n', 'Y', 'N' ): if 'yesno' in field: _setfield( world, field, c ) break elif c in ( 'S', 'Q' ): # return world only if it has a name if world.category!=world.INVALID: return world else: return None elif c in ( 'A', 'KEY_ESC' ): return None message, lastmessage = _show_message( editwin, message, lastmessage )