def get_user_string(self, xpos=3, ypos=15, filterfunc=str.isalnum, completer=None): # filter allowed characters using filterfunc, alphanumeric by default user_string = "" user_input = 0 if completer: completer = completer(self) while user_input != 10: user_input = self.screen.getch() if user_input == -1: # Input comes from pipe/file and is closed raise IOError self.screen_lock.acquire() # osx and unix backspace chars... if user_input == 127 or user_input == 263: if len(user_string) > 0: user_string = user_string[:-1] if completer: completer.update_input(user_string) self.screen.addstr(ypos, xpos, " " * (self.maxx-xpos-1)) elif user_input in [ord('\t'), curses.KEY_BTAB] and completer: direction = 1 if user_input == ord('\t') else -1 user_string = completer.complete(direction) self.screen.addstr(ypos, xpos, " " * (self.maxx-xpos-1)) elif user_input < 256 and user_input != 10: if filterfunc(chr(user_input)): user_string += chr(user_input) if completer: completer.update_input(user_string) self.screen.addstr(ypos, xpos, str(user_string)) self.screen.refresh() self.screen_lock.release() return user_string
def get_user_string(self, xpos=3, ypos=15, filterfunc=str.isalnum, completer=None): # filter allowed characters using filterfunc, alphanumeric by default user_string = "" user_input = 0 if completer: completer = completer(self) while user_input != 10: user_input = self.screen.getch() self.screen_lock.acquire() # osx and unix backspace chars... if user_input == 127 or user_input == 263: if len(user_string) > 0: user_string = user_string[:-1] if completer: completer.update_input(user_string) self.screen.addstr(ypos, xpos, " " * (self.maxx - xpos - 1)) elif user_input in [ord('\t'), curses.KEY_BTAB] and completer: direction = 1 if user_input == ord('\t') else -1 user_string = completer.complete(direction) self.screen.addstr(ypos, xpos, " " * (self.maxx - xpos - 1)) elif user_input < 256 and user_input != 10: if filterfunc(chr(user_input)): user_string += chr(user_input) if completer: completer.update_input(user_string) self.screen.addstr(ypos, xpos, str(user_string)) self.screen.refresh() self.screen_lock.release() return user_string
def test_when_buffer_have_a_invalid_word_positioned_word(self): current_buffer = 'id from' self.assertEqual(complete(current_buffer), 'select')
def test_get_table(self): current_buffer = 'select id from' self.assertEqual(complete(current_buffer), ['table1', 'table2'])
def test_when_buffer_have_field_word(self): current_buffer = 'select id' self.assertEqual(complete(current_buffer), ['id', 'cpf', 'from'])
def test_when_buffer_have_wrong_select_word(self): current_buffer = 'selecti' self.assertEqual(complete(current_buffer), 'select')
def test_when_buffer_have_select_word(self): current_buffer = 'select' self.assertEqual(complete(current_buffer), ['id', 'cpf'])
def test_when_buffer_is_empty(self): current_buffer = '' self.assertEqual(complete(current_buffer), 'select')