def reset_curses(self): """ Set back to normal the terminal """ self.scr.keypad(0) curses.echo() curses.nocbreak() curses.noraw() curses.endwin()
def __init__(self, stdscr): '''Login window''' # This part is for cursor positions. self.STARTPOS = 14 self.ENDPOS = 15 self.maxInput = self.STARTPOS + self.ENDPOS curses.curs_set(1) curses.noraw() #stdscrDim = stdscr.getmaxyx() self.height = 10 self.width = 50 self.ypos = (Window.height - self.height) / 2 self.xpos = (Window.width - self.width) / 2 self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos) self.win.keypad(1) strHeader = ' Welcome to Login Form ' strUserName = '******' strPassword = '******' self.win.box() self.win.addstr(1, (self.width - len(strHeader)) / 2, strHeader, curses.A_REVERSE) self.win.addstr(3, 2, strUserName, curses.A_NORMAL) self.win.addstr(5, 2, strPassword, curses.A_NORMAL) # Buttons self.win.addstr(8, 2, ' [CTRL-B] - Back ', curses.A_REVERSE) self.win.addstr(8, 30, ' [CTRL-L] - Login ', curses.A_REVERSE) self.win.refresh() stdscr.refresh() # If terminal window is resized, refresh the parent.
def redraw_loop(screen, manager): from perf_moon.cli import report_metrics, line_is_heading coloredlogs.set_level(logging.ERROR) cursor_mode = curses.curs_set(0) curses.noraw() screen.nodelay(True) try: while True: lnum = 0 for line in report_metrics(manager): attributes = 0 if line_is_heading(line): attributes |= curses.A_BOLD screen.addstr(lnum, 0, line, attributes) lnum += 1 screen.refresh() for i in range(10): if screen.getch() == ord('q'): return time.sleep(0.1) manager.refresh() screen.erase() finally: curses.curs_set(cursor_mode) screen.erase()
def cleanup(self): Cursor.show() self.screen.keypad(False) self.screen.nodelay(False) curses.noraw() curses.echo() curses.endwin()
def main(): os.environ["ESCDELAY"] = "0" stdscr = curses.initscr() curses.noecho() curses.raw() curses.curs_set(False) # Enable mouse actions. curses.mousemask(curses.BUTTON1_CLICKED | curses.BUTTON1_DOUBLE_CLICKED) stdscr.keypad(True) stdscr.nodelay(True) stdscr.notimeout(True) try: history = [] while True: key, arg = get_key(stdscr) k = "{!r}, {!r}".format(key, arg) history.append(k) if len(history) > 10: history.pop(0) stdscr.clear() for i in range(len(history)): stdscr.addstr(i, 2, history[i]) if key == "q" or key == 113: break finally: curses.curs_set(True) curses.noraw() stdscr.keypad(False) curses.echo() curses.endwin()
def run(self): """Run the editor main loop (read and execute command)""" try: screen = curses.initscr() curses.noecho() curses.raw() curses.meta(1) screen.keypad(1) height, width = screen.getmaxyx() self._width = width self._height = height self._window.set_size(width, height - 1) self.redisplay(screen) while 1: cmd = self.read_command(screen) if cmd is None: curses.beep() self.message("No command on key '%s'" % self.last_key()) else: self.message(None) cmd.run() self.redisplay(screen) finally: screen.keypad(0) curses.meta(0) curses.noraw() curses.endwin()
def run(self): """Run the editor main loop (read and execute command)""" try: screen = curses.initscr() curses.noecho() curses.raw() curses.meta(1) screen.keypad(1) height, width = screen.getmaxyx() self._width = width self._height = height self._window.set_size(width, height-1) self.redisplay(screen) while 1: cmd = self.read_command(screen) if cmd is None: curses.beep() self.message("No command on key '%s'" % self.last_key()) else: self.message(None) cmd.run() self.redisplay(screen) finally: screen.keypad(0) curses.meta(0) curses.noraw() curses.endwin()
def initCurses(): curses.initscr() curses.start_color() curses.init_pair(1, 10, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_BLACK, 10) curses.noecho() curses.noraw() curses.curs_set(2)
def __exit__(self, type, value, tb): # go to last line of screen, maybe cause scrolling? self.color_assigner.close() self.stdscr.keypad(0) curses.noraw() curses.nl() curses.echo() curses.endwin() signal.signal(signal.SIGTSTP, self.orig_sigtstp)
def curses_end(): """ If you aren't wrapping a function using curses.wrapper, you will have to call this whenever your program terminates """ import curses curses.echo() curses.noraw() curses.nocbreak() curses.endwin()
def manual(self): #curses.is_term_resized(nlines, ncols) - true if resize_term() woudlmodify window structure stdscr = curses.initscr() curses.noecho() curses.raw() curses.cbreak() stdscr.keypad(True) self.man_draw(stdscr) pos=0 max = stdscr.getmaxyx() while True: max = stdscr.getmaxyx() c = stdscr.getch() if stdscr.is_wintouched(): self.man_draw(stdscr) if c == ord('q'): break elif c == curses.KEY_UP: pos -= 1 if pos < 0: pos = 0 self.man_draw(stdscr, pos) stdscr.addnstr(max[0]-1, 0, " ", max[1], curses.A_NORMAL) stdscr.addnstr(max[0]-1, 0, " Manual page rot2proG [command mode] press q to quit ", max[1], curses.A_REVERSE) elif c == curses.KEY_DOWN: if ((45 - max[0]) <= pos): self.man_draw(stdscr, pos) stdscr.addnstr(max[0]-1, 0, " ", max[1], curses.A_NORMAL) stdscr.addnstr(max[0]-1, 0, " Manual page rot2proG [command mode] (END) press q to quit ", max[1], curses.A_REVERSE) elif ((45 - pos) > max[0]): pos += 1 self.man_draw(stdscr, pos) stdscr.addnstr(max[0]-1, 0, " ", max[1], curses.A_NORMAL) stdscr.addnstr(max[0]-1, 0, " Manual page rot2proG [command mode] press q to quit ", max[1], curses.A_REVERSE) else: pos += 1 self.man_draw(stdscr, pos) stdscr.addnstr(max[0]-1, 0, " ", max[1], curses.A_NORMAL) stdscr.addnstr(max[0]-1, 0, " Manual page rot2proG [command mode] press q to quit ", max[1], curses.A_REVERSE) elif c == curses.KEY_F11: self.man_draw(stdscr) stdscr.addnstr(max[0]-1, 0, " ", max[1], curses.A_NORMAL) stdscr.addnstr(max[0]-1, 0, " Manual page rot2proG [command mode] press q to quit ", max[1], curses.A_REVERSE) curses.nocbreak() curses.noraw() stdscr.keypad(False) curses.echo() curses.endwin()
def final(self): # finalize self.__scr.keypad(0) curses.noraw() curses.nocbreak() curses.echo() curses.endwin() # unset singleton object __obj = None
def scr_quit(): global _cur_scr, _inited if not _inited: return _cur_scr.keypad(0) curses.echo() curses.nocbreak() curses.noraw() curses.endwin() _inited = 0
def _close(self): """Close the curses interface.""" # restore original terminal size if self.orig_size and console: self._resize(*self.orig_size) curses.noraw() curses.nl() curses.nocbreak() if self.screen: self.screen.keypad(False) curses.echo() curses.endwin()
def __init__(self, stdscr): '''Registration window''' # This part is for cursor positions. self.STARTPOS = 21 self.ENDPOS = 30 self.maxInput = self.STARTPOS + self.ENDPOS curses.curs_set(1) curses.noraw() stdscrDim = stdscr.getmaxyx() self.height = 22 self.width = 55 self.ypos = (stdscrDim[0] - self.height) / 2 self.xpos = (stdscrDim[1] - self.width) / 2 self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos) self.win.keypad(1) strHeader = ' Welcome to Registration Form ' strGender = 'Gender:' strFirstName = 'First name:' strLastName = 'Last name:' strUserName = '******' strEmail = 'E-mail:' strPassword = '******' strConfirmPassword = '******' strDateOfBirth = 'Date of birth:' self.win.box() self.win.addstr(1, (self.width-len(strHeader))/2, strHeader, curses.A_REVERSE) self.win.addstr(3, 2, strGender, curses.A_NORMAL) self.win.addstr(5, 2, strFirstName, curses.A_NORMAL) self.win.addstr(7, 2, strLastName, curses.A_NORMAL) self.win.addstr(9, 2, strUserName, curses.A_NORMAL) self.win.addstr(11, 2, strEmail, curses.A_NORMAL) self.win.addstr(13, 2, strPassword, curses.A_NORMAL) self.win.addstr(15, 2, strConfirmPassword, curses.A_NORMAL) self.win.addstr(17, 2, strDateOfBirth, curses.A_NORMAL) self.win.refresh() # Buttons self.win.addstr(20, 2, ' [CTRL-B] - Back ', curses.A_REVERSE) self.win.addstr(20, 32, ' [CTRL-R] - Register ', curses.A_REVERSE) self.win.refresh() stdscr.refresh() # If terminal window is resized, refresh the parent.
def close(self): self._logger.debug('Closing screen.') if self._screen_type == self.SCREEN_TYPE_NCURSES: self._screen.keypad(False) curses.curs_set(True) curses.noraw() curses.echo() curses.nocbreak() curses.endwin() if self._screen_type == self.SCREEN_TYPE_FPDEVICE: self._screen.close()
def close(self): """ Close the text interface. """ video.VideoPlugin.close(self) if self.curses_init: # restore original terminal size self._resize(*self.orig_size) # restore sane terminal state curses.noraw() curses.nl() curses.nocbreak() self.screen.keypad(False) curses.echo() curses.endwin()
def __exit__(self, type, value, traceback): """Close the curses interface.""" base.VideoPlugin.__exit__(self, type, value, traceback) if self.curses_init: # restore original terminal size self._resize(*self.orig_size) # restore sane terminal state curses.noraw() curses.nl() curses.nocbreak() self.screen.keypad(False) curses.echo() curses.endwin()
def __init__(self, stdscr): curses.curs_set(1) curses.noraw() stdscrDim = stdscr.getmaxyx() self.height = 20 self.width = 50 self.ypos = (stdscrDim[0] - self.height) / 2 self.xpos = (stdscrDim[1] - self.width) / 2 self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos) curses.raw() self.win.keypad(1) curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_MAGENTA, curses.COLOR_BLACK) strHeader1 = ' You Are Logged as Administrator ' strHeader2 = 'Choose from options below:' strCreateUser = '******' strDeleteUser = '******' strAddTable = '[3] Add Category' strAddContact = '[4] Add Contact' strEditContact = '[5] Edit Contact' strDelContact = '[6] Delete Contact' strListByCategory = '[7] List Contacts by Category' strListContact = '[8] List All Contacts' strAdd = '[CTRL-B] Back to main menu (Logout)' self.win.bkgd(' ', curses.color_pair(3)) self.win.attron(curses.color_pair(2)) self.win.addstr(1, (self.width-len(strHeader1)) / 2, strHeader1, curses.A_BOLD) self.win.addstr(2, (self.width-len(strHeader2)) / 2, strHeader2, curses.A_NORMAL) self.win.addstr(4, 3, strCreateUser, curses.A_NORMAL) self.win.addstr(5, 3, strDeleteUser, curses.A_NORMAL) self.win.addstr(6, 3, strAddTable, curses.A_NORMAL) self.win.addstr(7, 3, strAddContact, curses.A_NORMAL) self.win.addstr(8, 3, strEditContact, curses.A_NORMAL) self.win.addstr(9, 3, strDelContact, curses.A_NORMAL) self.win.addstr(10, 3, strListByCategory, curses.A_NORMAL) self.win.addstr(11, 3, strListContact, curses.A_NORMAL) self.win.addstr(18, 3, strAdd, curses.A_NORMAL) self.win.box() self.win.attroff(curses.color_pair(2)) self.win.refresh()
def _close(self): """Close the curses interface.""" try: curses.noraw() curses.nl() curses.nocbreak() if self.screen: self.screen.keypad(False) curses.echo() curses.endwin() # restore original terminal size, colours and cursor console.reset() except Exception as e: logging.error('Exception on closing curses interface: %s', e)
def curses_screen(): """ Curses context manager. Returns the screen window on entry. Restores the terminal view on exit. """ # Disable the curses ESC translation delay. # FIXME: It might be possible to set the ESCDELAY variable in libncurses.so # directly with ctypes. os.environ["ESCDELAY"] = "0" # Get stdin from the TTY, even if it's been redirected. old_stdin_fd = os.dup(0) os.dup2(os.open("/dev/tty", os.O_RDONLY), 0) win = curses.initscr() curses.noecho() curses.cbreak() curses.curs_set(False) curses.raw() win.keypad(True) win.notimeout(True) win.nodelay(True) # Enable mouse events. mouse_mask = curses.BUTTON1_CLICKED | curses.BUTTON1_DOUBLE_CLICKED new_mask, _ = curses.mousemask(mouse_mask) if new_mask != mouse_mask: logging.warning("problem with mouse support: {:x} {:x}".format( mouse_mask, new_mask)) init_attrs() try: yield win finally: win.nodelay(False) win.notimeout(False) win.keypad(False) curses.noraw() curses.curs_set(True) curses.nocbreak() curses.echo() curses.endwin() # Restore the old stdin. os.dup2(old_stdin_fd, 0)
def close(): """ Close the text interface. """ if wait_on_close: sys.stdout.write(ansi.esc_set_title % (caption + ' - press a key to close window')) # redraw in case terminal didn't recognise ansi sequence redraw() while window.getch() == -1: pass if curses: curses.noraw() curses.nl() curses.nocbreak() screen.keypad(False) curses.echo() curses.endwin()
def close(): """ Close the text interface. """ if wait_on_close: sys.stdout.write(ansi.esc_set_title % (caption + ' - press a key to close window')) sys.stdout.flush() # redraw in case terminal didn't recognise ansi sequence redraw() while window.getch() == -1: pass if curses: curses.noraw() curses.nl() curses.nocbreak() screen.keypad(False) curses.echo() curses.endwin()
def render_editor2(stdscr, filename, is_new_file=True): h, w = stdscr.getmaxyx() if is_new_file: csv_data = CSVData(filename) else: csv_data = CSVData(filename) csv_data.load() g = Grid(stdscr, 20, 6, h, w, 1, 0, 0, 0, 1, 10, csv_data) g.render() while True: # ch = stdscr.getch() # if ch == ord('q'): # break g.render() g.handle_input() curses.noraw()
def terminate(self): """ Cursesライブラリの非初期化を行う。 終了するときには呼び出さなくてはいけない。 というかそうしないとまずくなる。 """ if self.terminated: return self.terminated = True self.console.keypad(0) curses.nocbreak() curses.noraw() curses.echo() curses.endwin() atexit.unregister(self.terminate)
def redraw_loop(screen, manager): """The main loop that continuously redraws Apache web server metrics.""" # Ugly workaround to avoid circular import errors due to interdependencies # between the apache_manager.cli and apache_manager.interactive modules. from apache_manager.cli import report_metrics, line_is_heading # Hide warnings (they'll mess up the curses layout). coloredlogs.set_level(logging.ERROR) # Hide the text cursor. cursor_mode = curses.curs_set(0) # Make Control-C behave normally. curses.noraw() # Enable non-blocking getch(). screen.nodelay(True) try: # Repeat until the user aborts. while True: lnum = 0 for line in report_metrics(manager): attributes = 0 if line_is_heading(line): attributes |= curses.A_BOLD screen.addstr(lnum, 0, line, attributes) lnum += 1 # Redraw screen. screen.refresh() # Wait a while before refreshing the screen, but enable the user to # quit in the mean time. for i in range(10): if screen.getch() == ord('q'): return # Don't burn through CPU like crazy :-). time.sleep(0.1) # Update metrics in next iteration. manager.refresh() # Clear screen for next iteration. screen.erase() finally: # Restore cursor mode. curses.curs_set(cursor_mode) # Clean up the screen after we're done. screen.erase()
async def __aexit__(self, type, value, tb): self.running = False # go to last line of screen, maybe cause scrolling? self.color_assigner.close() self.stdscr.keypad(0) curses.noraw() curses.nl() curses.echo() curses.endwin() try: signal.signal(signal.SIGINT, self.orig_sigint) except TypeError: self.log.exception('%s', f'orig_sigint: {self.orig_sigint!r}') try: signal.signal(signal.SIGTSTP, self.orig_sigtstp) except TypeError: self.log.exception('%s', f'orig_sigtstp: {self.orig_sigtstp!r}') try: signal.signal(signal.SIGWINCH, self.orig_sigwinch) except TypeError: self.log.exception('%s', f'orig_sigwinch: {self.orig_sigwinch!r}')
def __call__(self, c): if c == curses.ascii.ETX: # ctrl-c self.queue.put_nowait(Key.Break) elif c == curses.ascii.EOT: # ctrl-d try: curses.noraw() curses.echo() curses.endwin() except Exception: pass os._exit(1) elif c == curses.ascii.NL or c == curses.ascii.CR: self.queue.put_nowait(Key.Return) elif c == curses.KEY_UP: self.queue.put_nowait(Key.Up) elif c == curses.KEY_DOWN: self.queue.put_nowait(Key.Down) elif c == curses.KEY_LEFT: self.queue.put_nowait(Key.Left) elif c == curses.KEY_RIGHT: self.queue.put_nowait(Key.Right) elif c == curses.ascii.TAB or c == curses.ascii.HT: self.queue.put_nowait(Key.Tab) elif c == curses.ascii.DEL or c == curses.ascii.BS: self.queue.put_nowait(Key.Backspace) elif c == curses.KEY_DC: self.queue.put_nowait(Key.Delete) elif c == curses.ascii.ESC: self.state = "^[" elif self.state == "^[" and c == ord("["): self.state = "^[[" elif self.state == "^[[" and c == ord("3"): self.state = "^[[3" elif self.state == "^[[3" and c == ord("~"): self.state = "" self.queue.put_nowait(Key.Delete) elif curses.ascii.isprint(c): self.queue.put_nowait(c)
async def tui_init(app): """Inits the curses library and starts the interface Arguments: app {pocket.Pocket} -- The pocket instance """ screen = curses.initscr() curses.start_color() curses.raw() curses.cbreak() curses.noecho() try: await tui(screen, app) except Exception as error: print(error) finally: screen.clear() screen.refresh() screen.keypad(0) curses.noraw() curses.nocbreak() # Turn off cbreak mode curses.echo() # Turn echo back on curses.endwin()
def __exit__(self, exc_type, exc_val, exc_tb): curses.noraw() self.screen.keypad(False) curses.echo() curses.endwin()
def walk(moon=False, mirror=False): import curses, random, math tau = 0.003 if moon: logo_left = Neubig.__shuffle(Neubig.__logo_right) logo_right = Neubig.__shuffle(Neubig.__logo_left) else: logo_left = Neubig.__shuffle(Neubig.__logo_left) logo_right = Neubig.__shuffle(Neubig.__logo_right) window = curses.initscr() curses.noecho() window.keypad(1) curses.cbreak() curses.raw() window.timeout(20) h, w = window.getmaxyx() if mirror: w_real = w w //= 2 state = 0 x = -47 y = int((h - 33) / 2) t = 0 logo = logo_right logo_rev = logo_left while True: thr = 0.5 * (1 - math.exp(-tau * t)) key = window.getch() if key == curses.KEY_RESIZE: h, w = window.getmaxyx() y = int((h - 33) / 2) if y < 0: y = 0 window.clear() for i, line in enumerate(logo): if y + i < 0: continue if h <= y + i + 1: break if x < 0: window.addstr(y + i, 0, line[-x : w - x]) else: window.addstr(y + i, x, line[0 : w - x]) if mirror: x_rev = w_real - x - len(line) if x_rev < w: window.addstr(y + i, w, logo_rev[i][-x_rev + w : w_real - x_rev]) else: window.addstr(y + i, x_rev, logo_rev[i][0 : w_real - x_rev]) if 0 <= x and x < w - 47: t += 1 rnd = random.random() if state == 0: if rnd < thr: state = 1 t = 0 elif state == 1: if rnd < thr: state = 0 elif rnd > 1 - thr: state = 2 t = 0 else: if rnd < thr: state = 1 t = 0 if state == 0: x += 1 logo = logo_right logo_rev = logo_left if state == 2: x -= 1 logo = logo_left logo_rev = logo_right if x == w or x == -47: break window.move(h - 1, 0) curses.noraw() curses.nocbreak() window.keypad(0) curses.echo() curses.endwin()
def render_editor(stdscr, filename, is_new_file=True): stdscr.clear() h = 1 w = 10 x_pos = 0 y_pos = 0 if is_new_file: csv_data = CSVData(filename) else: csv_data = CSVData(filename) csv_data.load() contents = [] # Create 2d array for i in range(5): contents.append([]) for j in range(5): contents[i].append([]) # Fill array for y in range(5): for x in range(5): contents[x][y] = Entry(stdscr, 1 + (y * 3), 2 + (x * (w + (w // 2)) + 2), h, w) stdscr.refresh() curses.raw() while True: stdscr.clear() for y in range(len(contents)): for x in range(len(contents[0])): contents[x][y].render() # stdscr.refresh() contents[x_pos][y_pos].highlight() stdscr.refresh() key = stdscr.getch() if key == curses.KEY_DOWN and y_pos < len(contents[0]) - 1: contents[x_pos][y_pos].highlight() y_pos += 1 elif key == curses.KEY_UP and y_pos > 0: contents[x_pos][y_pos].highlight() y_pos -= 1 elif key == curses.KEY_RIGHT and x_pos < len(contents) - 1: contents[x_pos][y_pos].highlight() x_pos += 1 elif key == curses.KEY_LEFT and x_pos > 0: contents[x_pos][y_pos].highlight() x_pos -= 1 elif key == ord("q"): break elif key == curses.KEY_ENTER or key in [10, 13]: contents[x_pos][y_pos].edit_entry() txt = contents[x_pos][y_pos].get_text() csv_data.update_data(contents) # stdscr.clear() # stdscr.addstr(0, 0, f"Entered: {txt}") # stdscr.refresh() # stdscr.getch() elif key == curses.KEY_DC or key == curses.KEY_DL: contents[x_pos][y_pos].clear_text() elif key == 19: # CTRL + S csv_data.update_data(contents) csv_data.save() stdscr.refresh() curses.noraw()
def stop(self): curses.noraw() curses.echo() curses.endwin()
def __fini_curses(self): curses.noraw() curses.echo() curses.endwin()
def getch(self): def add_next_byte(): '''Local function: for checking next byte if the input was different then ascii char set.''' c = self.win.getch() if 128 <= ch <= 191: return ch else: raise UnicodeError # Is the position X-ed (checked)? posy1 = False posy2 = False posy3 = False # Invoking needed variables for cursor position movingX = self.STARTPOS rightX = self.STARTPOS container = '' while True: curses.raw() self.win.keypad(1) ch = self.win.getch() kname = curses.keyname(ch) y, x = self.win.getyx() if kname == '^X': return '^X' elif ch == curses.KEY_UP: y, x = self.win.getyx() if y == 6: if not posy1 and not posy2 and not posy3: self.win.move(8, 4) else: self.win.move(11, 40) elif y == 8: self.win.move(7, 4) elif y == 7: self.win.move(6, 4) elif y == 11: self.win.move(8, 4) elif ch == curses.KEY_DOWN: y, x = self.win.getyx() if y == 6: self.win.move(7, 4) elif y == 7: self.win.move(8, 4) elif y == 8: if not posy1 and not posy2 and not posy3: self.win.move(6, 4) else: self.win.move(11, 40) elif y == 11: self.win.move(6, 4) elif ch == curses.KEY_LEFT: y, x = self.win.getyx() if y == 11: movingX = x # probably movingX has to be a static variable if movingX > self.STARTPOS: movingX -= 1 # Tracking the X position self.win.move(y, movingX) else: self.win.move(y, self.STARTPOS) self.warning() elif ch == curses.KEY_RIGHT: y, x = self.win.getyx() if y == 11: movingX = x if movingX < self.maxInput and movingX < rightX: movingX += 1 # Tracking the X position self.win.move(y, movingX) else: if movingX == self.maxInput: self.win.move(y, self.maxInput) curses.beep() curses.flash() elif movingX == rightX: self.win.move(y, rightX) self.warning() elif ch == curses.KEY_BACKSPACE: y, x = self.win.getyx() if y == 11: if rightX > self.STARTPOS: container = container[:-1] x -= 1 rightX -= 1 movingX -= 1 self.win.addch(y, x, ' ') self.win.move(y, x) self.win.refresh() else: self.warning() elif ch == 127: # DEL pass elif ch == 10: pass elif ch == 9: # TAB ('^I') for Gnome-terminal pass elif ch == 2: return 2 elif ch == curses.KEY_HOME: if y == 11: self.win.move(y, self.STARTPOS) movingX = self.STARTPOS elif ch == curses.KEY_END: if y == 11: x = self.STARTPOS + len(container) self.win.move(y, x) movingX = x rightX = x elif ch == 32: # Space character cursorPosition = self.win.getyx() if cursorPosition[0] != 11: # if y is different then 11. curses.noraw() if cursorPosition == (6, 4): if not posy1: self.win.addch('X') self.win.move(6, 4) self.win.refresh() posy1 = True elif posy1: self.win.addch(' ') self.win.move(6, 4) self.win.refresh() posy1 = False elif cursorPosition == (7, 4): if not posy2: self.win.addch('X') self.win.move(7, 4) self.win.refresh() posy2 = True elif posy2: self.win.addch(' ') self.win.move(7, 4) self.win.refresh() posy2 = False elif cursorPosition == (8, 4): if not posy3: self.win.addch('X') self.win.move(8, 4) self.win.refresh() posy3 = True elif posy3: self.win.addch(' ') self.win.move(8, 4) self.win.refresh() posy3 = False elif y == 11 and \ (ch in range(48, 58) or \ ch in range(65, 91) or \ ch in range(97, 123) or \ ch in range(127, 245) or \ ch == ord('_') or \ ch == ord('-') or \ ch == ord('.') or \ ch == ord('@')): curses.noraw() if movingX == rightX: if rightX < self.maxInput: if ch <= 127: # 1 byte container += chr(ch) elif 194 <= ch <= 223: # 2 byte container += chr(ch) container += chr(add_next_byte()) elif 224 <= ch <= 239: # 3 byte container += chr(ch) container += chr(add_next_byte()) container += chr(add_next_byte()) elif 240 <= ch <= 244: # 4 byte container += chr(ch) container += chr(add_next_byte()) container += chr(add_next_byte()) container += chr(add_next_byte()) self.win.addch(ch) movingX += 1 rightX += 1 self.win.move(y, rightX) else: self.warning() elif movingX < rightX: if rightX < self.maxInput: containerLeft = container[:movingX-self.STARTPOS] containerRight = container[movingX-self.STARTPOS:] container = containerLeft container += chr(ch) container += containerRight movingX += 1 rightX += 1 self.win.addstr(y, self.STARTPOS, container) self.win.move(y, movingX) self.win.refresh() else: self.warning else: pass
def _restore(): curses.noraw() curses.nl()
def end_ui(self): curses.noraw() self._screen.keypad(0) curses.echo() curses.endwin()
def main(): stdscr = curses.initscr() curses.noecho() curses.nonl() curses.raw() colors = curses.has_colors() curses.start_color() if colors: curses.use_default_colors() stdscr.keypad(1) stdscr.scrollok(1) maxy, maxx = stdscr.getmaxyx() stdscr.attrset(curses.A_REVERSE) stdscr.addstr('‡\n') stdscr.attrset(0) x = stdscr.inch(0, 0) stdscr.move(2, 0) stdscr.addstr('inch(0,0) = %s; A_UNDERLINE=%d\n' % (repr(x), curses.A_UNDERLINE)) stdscr.addstr( 'COLORS=%d COLOR_PAIRS=%d has_colors()=%s can_change_color()=%s\n' % ( curses.COLORS, curses.COLOR_PAIRS, colors, curses.can_change_color())) stdscr.addstr('%d\n' % (maxx,)) rgbs=[] if colors: for i in range(curses.COLORS): rgb = '%02x%02x%02x' % tuple(int((j/1000)*255.0) for j in curses.color_content(i)) rgbs.append(rgb) stdscr.addstr(' %s' % (rgb,)) if (stdscr.getyx()[1] + 7) > maxx: stdscr.addstr('\n')o stdscr.addstr('\n') stdscr.addstr('press a key') stdscr.get_wch() stdscr.addstr('\n') pairs = {} if colors: for i in range(min(curses.COLORS, curses.COLOR_PAIRS - 1)): curses.init_pair(i+1, -1, i) ## if curses.can_change_color(): ## for i in range(curses.COLORS): ## curses.init_color(i, 0, 1000, 0) for i in range(1, curses.COLOR_PAIRS): #stdscr.addstr(' %03d' % (i,), curses.color_pair(i)) stdscr.addstr(' %s' % (rgbs[i-1],), curses.color_pair(i)) y, x = stdscr.getyx() if x + 7 > maxx: stdscr.addstr('\n') else: stdscr.addstr('\n') stdscr.addstr('press a key') stdscr.get_wch() stdscr.addstr('\n') if colors: if curses.can_change_color(): for i in range(curses.COLORS): curses.init_color(i, 0, 1000, 0) stdscr.addstr('press q to quit\n') x = None while x != 'q': x = stdscr.get_wch() stdscr.addstr('%s %s %s %s\n' % ( type(x), repr(x), repr(x) if isinstance(x, str) else curses.keyname(x), repr(x) if isinstance(x, str) else unkey.get(x, '???'), )) if x == curses.KEY_RESIZE: maxy, maxx = stdscr.getmaxyx() stdscr.addstr('Resize! %d %d\n' % (maxy, maxx)) curses.noraw() curses.nl() curses.echo() curses.endwin()
def destroy(cls): if cls.stdscr: curses.echo() curses.nocbreak() curses.noraw() curses.endwin()
#try: #s = unichr(c) #stdscr.addstr("%X %s" % (c,s.decode("UTF-8"))) #stdscr.addstr("%X|%s" % (c,s.encode("UTF-8"))) #stdscr.addstr("%X|%s" % (c,s)) #stdscr.addstr(s.encode("ISO8859-15")) #except: # pass finally: curses.nocbreak(); curses.noraw(); stdscr.keypad(0); curses.echo() curses.endwin() """ 11111111112 012345678901234567890 0 SCORE: 123 1 aaplod 2 palo 3 4 ___ ____ _____ ______ 5 ___ ____ 6 ___ ____ 7 ___ 8 ___ 9
def restore_terminal(): logger.info("restore_terminal()") curses.noraw() curses.nocbreak() curses.echo() curses.endwin()
def Restore(self): curses.nocbreak() curses.echo() curses.noraw() curses.endwin()
def __init__(self, stdscr): # self.CURSPOS = ((6, 4), (7, 4), (8, 4), (11, 36)) # self.STARTPOS and self.ENDPOS is only for last input field. self.STARTPOS = 40 self.ENDPOS = 25 self.maxInput = self.STARTPOS + self.ENDPOS curses.curs_set(1) curses.noraw() curses.cbreak() stdscrDim = stdscr.getmaxyx() self.height = 20 self.width = 70 self.ypos = (stdscrDim[0] - self.height) / 2 self.xpos = (stdscrDim[1] - self.width) / 2 self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos) curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK) self.win.bkgd(' ', curses.color_pair(4)) strHeader1 = ' You Are Logged as a User ' strHeader2 = 'Choose from options below:' strListAll = 'Mark with SPACE wich table to search.' #strKJI = u'[ ] Kiemelt jelent\u030b\u006Fs\u0301\u0065g\u030B\u0075 int\u0301\u0065zm\u0301\u0065nyek' strKJI = '[ ] Kiemelt jelentőségű intézmények' strEM = '[ ] e-magyar' strKJII = '[ ] Kiemelt jelentőségű ifjúsági intézmények' strSearch = 'Search in marked tables for subject:' strList = '[CTRL-L] List all from selected categories' strBack = '[CTRL-B] Back to main window' strExit = '[CTRL-X] - Exit' self.win.attron(curses.color_pair(2)) self.win.box() self.win.addstr(1, (self.width - len(strHeader1)) / 2, strHeader1, curses.A_REVERSE | curses.A_BOLD) self.win.addstr(2, (self.width - len(strHeader2)) / 2, strHeader2, curses.A_NORMAL) self.win.addstr(4, 3, strListAll, curses.A_UNDERLINE) self.win.attroff(curses.color_pair(2)) self.win.refresh() self.win.attron(curses.color_pair(3)) self.win.addstr(6, 3, strKJI, curses.A_NORMAL) self.win.addstr(7, 3, strEM, curses.A_NORMAL) self.win.addstr(8, 3, strKJII, curses.A_NORMAL) self.win.addstr(11, 3, strSearch, curses.A_NORMAL) self.win.attroff(curses.color_pair(3)) self.win.refresh() self.win.attron(curses.color_pair(4)) self.win.addstr(16, 3, strList, curses.A_NORMAL) self.win.addstr(17, 3, strBack, curses.A_NORMAL) self.win.addstr(18, 3, strExit, curses.A_NORMAL) self.win.attroff(curses.color_pair(4)) self.win.refresh() stdscr.refresh() # If terminal window is resized, refresh the parent. self.win.move(6, 4)
def getch(self, flag, end): container = '' y, x = self.win.getyx() movingX = x rightX = self.STARTPOS while True: if end: curses.raw() curses.noecho() self.win.keypad(1) curses.curs_set(0) ch = self.win.getch() kname = curses.keyname(ch) if kname == '^R': return kname elif not end: curses.raw() curses.noecho() self.win.keypad(1) ch = self.win.getch() kname = curses.keyname(ch) if ch == 2: # If user hits CTRL-B return 2 elif kname == '^R': return '^R' elif ch == curses.KEY_BACKSPACE: # Backspace if rightX > self.STARTPOS: # If cursor is bigger then start position. container = container[:-1] # Delete the last character. y, x = self.win.getyx() x -= 1 # --+ rightX -= 1 # |--> Keep track of pointers movingX -= 1 # --+ self.win.addch(y, x, ' ') # Remove last char from screen. self.win.move(y, x) # Move back the cursor self.win.refresh() else: self.warning() elif ch == 127: # DEL pass elif ch == 9: # TAB ('^I') for Gnome-terminal return ch elif ch == curses.KEY_BTAB: # SHIFT+TAB for Gnome-terminal return ch elif ch == curses.KEY_HOME: self.win.move(y, self.STARTPOS) movingX = self.STARTPOS elif ch == curses.KEY_END: x = self.STARTPOS + len(container) self.win.move(y, self.STARTPOS + len(container)) movingX = x rightX = x elif ch == 10: # If user hits the enter, if container != '': # if we have something in container and return container else: return 10 # or we don't. elif ch == curses.KEY_LEFT: y, x = self.win.getyx() movingX = x # probably movingX has to be a static variable if movingX > self.STARTPOS: movingX -= 1 # Tracking the X position self.win.move(y, movingX) else: self.win.move(y, self.STARTPOS) self.warning() elif ch == curses.KEY_RIGHT: y, x = self.win.getyx() movingX = x if movingX < self.maxInput and movingX < rightX: movingX += 1 # Tracking the X position self.win.move(y, movingX) else: if movingX == self.maxInput: self.win.move(y, self.maxInput) curses.beep() curses.flash() elif movingX == rightX: self.win.move(y, rightX) self.warning() elif ch in range(65, 91) or \ ch in range(97, 123) or \ ch in range(48, 58) or \ ch == ord('_') or \ ch == ord('-') or \ ch == ord('.') or \ ch == ord('@'): curses.noraw() if not flag and movingX == rightX: # If we aren't in the password if rightX < self.maxInput: # max right position self.win.addch(ch) # field and no arrow key used yet. container += chr(ch) movingX += 1 rightX += 1 else: self.warning() elif not flag and movingX < rightX: # If we moved with arrow keys if rightX < self.maxInput: # max right position containerLeft = container[:movingX-self.STARTPOS] containerRight = container[movingX-self.STARTPOS:] container = containerLeft container += chr(ch) container += containerRight movingX += 1 rightX += 1 self.win.addstr(y, self.STARTPOS, container) self.win.move(y, movingX) self.win.refresh() else: self.warning() elif flag: # If we are in the password field if rightX < self.maxInput: # max right position curses.echo() container += chr(ch) self.win.addch(curses.ACS_DIAMOND) movingX += 1 rightX += 1 else: self.warning()
def restore_terminal(stdscr): curses.noraw() curses.nocbreak() stdscr.keypad(False) curses.echo() curses.endwin()
def restore_screen(self): curses.nocbreak() curses.echo() curses.noraw() curses.endwin()
def reset(): curses.nocbreak() curses.noraw() curses.echo() scr.keypad(False) curses.endwin()
def endcurses(): curses.nl() curses.echo() curses.noraw() curses.endwin()