def init(): global stdscr, chatlog_win, input_win, players_win, input_textbox stdscr = curses.initscr() curses.cbreak() curses.noecho() stdscr.keypad(1) h, w = stdscr.getmaxyx() PNW = 20 # player name width INH = 4 # input window height stdscr.vline(0, w - PNW - 1, curses.ACS_VLINE, h) stdscr.hline(h - INH - 1, 0, curses.ACS_HLINE, w - PNW - 1) chatlog_win = curses.newwin(h - INH - 1, w - PNW - 1, 0, 0) input_win = curses.newwin(INH, w - PNW - 1, h - INH, 0) players_win = curses.newwin(h, PNW, 0, w - PNW) chatlog_win.idlok(1) chatlog_win.scrollok(1) players_win.idlok(1) players_win.scrollok(1) input_textbox = Textbox(input_win) input_textbox.stripspaces = True stdscr.noutrefresh() input_win.noutrefresh() players_win.noutrefresh() chatlog_win.noutrefresh() curses.doupdate()
def main(self, stdscr): terminal_size = shutil.get_terminal_size() self.terminal_width = terminal_size[0] self.terminal_height = terminal_size[1] # Clear screen stdscr.clear() #Create the timeline window self.timeline_win = self.create_timeline_win() #Create the Prompt credentials = self.api.VerifyCredentials() prompt_win, prompt_width = self.create_prompt_win( credentials.screen_name) prompt_win.refresh() #Create Input window edit_line_win = self.create_edit_line_win(prompt_width) box = Textbox(edit_line_win, self) box.stripspaces = 1 #Refresh tweets and start loop self.do_refresh() while True: message = box.edit(validate=self.validate_input) edit_line_win.clear() self.handle_command(message)
def request_input(self, title, content=''): self.stdscr.addstr(0, 0, title) add_stdscr = curses.newwin(1, 60, 2, 1) rectangle(self.stdscr, 1, 0, 3, 62) self.stdscr.refresh() add_stdscr.addstr(0,0,content) box = Textbox(add_stdscr) box.stripspaces = True box.edit() return box.gather()[:-1]
def __edit_mail_to(self, to): '''Edit mail to field''' curses.curs_set(1) h, w = self.__stdscr.getmaxyx() to_msg = " To (Ctrl + G to save): (Use ';' to separate multiple emails)" editwin = curses.newwin(1, w - 5, 3, 2) editwin.insstr(to) self.__stdscr.attron(curses.A_BOLD) self.__stdscr.addstr(2, 2, to_msg) self.__stdscr.attroff(curses.A_BOLD) self.__stdscr.refresh() box = Textbox(editwin) box.stripspaces = True box.edit() self.__set_main_layout(self.__body.splitlines()) curses.curs_set(0) return box.gather()
def __edit_box(self, email, password, posy, posx, isPass=False): '''Edit box for login and password''' # TODO: Use isPass to show asterisk for password nol = 1 noc = 40 editwin = curses.newwin(nol, noc, posy, posx) self.__setup_layout(email, password, isPass) if isPass: ch = self.__stdscr.getch() password = password password_asterisk = "*" * len(password) editwin.insstr(password_asterisk) while ch != curses.ascii.BEL: if ch == curses.KEY_BACKSPACE: try: password = password[:-1] except: pass elif ch >= 32 and ch <= 127: password += chr(ch) password_asterisk = "*" * len(password) editwin.clear() editwin.insstr(password_asterisk) editwin.refresh() ch = self.__stdscr.getch() curses.curs_set(0) return password else: curses.curs_set(1) editwin.insstr(email) editwin.refresh() box_email = Textbox(editwin) box_email.stripspaces = True box_email.edit() curses.curs_set(0) return box_email.gather()
def main(stdscr): global x # Compute terminal size and, thus, usable area height = curses.LINES - 3 width = curses.COLS - 4 begin_y = 2 begin_x = 1 progress = 0 # Display header information stdscr.addstr(0, width//2-18, " Teleprompter for Terminals by Cuperino ", curses.A_STANDOUT) stdscr.addstr(1, 0, "Enter text to prompt: (press Ctrl-G to start prompting)") # Create editable area window editwin = curses.newwin(height, width, begin_y, 1+begin_x) rectangle(stdscr, begin_y, begin_x, 2+height, 2+width) stdscr.refresh() editwin.insstr(1,1,"Welcome to Teleprompter for Terminals") editwin.insstr(2,4,"Are you ready to tell a story?") editwin.move(2,34) # Turn edit window into a Textbox box = Textbox(editwin) # Keep whitespace when gathering data box.stripspaces = False # Let the user edit until Ctrl-G is struck. box.edit() # Get resulting contents message = box.gather() # Add reading area markers stdscr.addstr(height//2+1, 0, ">>") stdscr.addstr(height//2+1, width+1, "<<") # Display instructions stdscr.addstr(height+2, 5, " [ W decrease speed ] [ S increase speed ] ") stdscr.refresh() # Create prompter pad (a pad can extend beyond viewable area) # Having a fixed pad size (height*3) is one of those things that makes this implementation impractical for production. # Pagination functions would have to be implemented, but hey, this is an April Fools joke. prompter = curses.newpad(height*3, width) # Overlay prompter over editwin prompter.overlay(editwin, 0, 0, begin_y, begin_x, height-2, width-2) # Don't prompt the user for input, grab the last keypress on getch() prompter.nodelay(True) # Set velocity for the first time updateVelocity() # pause = False # Add text to prompter prompter.addstr(height-1, 0, message) # Begin scroll loop # Using Python's for loop is a bad implementation because it makes it difficult to change scrolling directions. # Re implementing this with a while loop is adviced for production. i = 1 while i < height*3: # Refresh promptable area while scrolling the pad. if x > 0: prompter.refresh(i, 0, 2, 2, height+1, width) i = i + 1 if x < 0: prompter.refresh(i, 0, 2, 2, height+1, width) i = i - 1 else: i = i # Post scrolling delay while x==0 or delay-progress > 0.002: time.sleep(0.002) progress = progress + 0.002 # Debug: Default delay used during testing # time.sleep(0.2) # Grab the last key pressed key = prompter.getch() if key != -1: # if key == 32: # pause = not pause if key in (87, 119): x = x-1 if key in (83, 115): x = x+1 updateVelocity() # Debug: Display key on prompable area if debug: message = str(key) prompter.addstr(height-1, 0, message) progress = 0 # Add a 0.5s delay for retro feelings time.sleep(0.5) # Notify prompting complete stdscr.addstr(height//2+1, width//2+1-8, "Prompting complete") stdscr.refresh() time.sleep(2) stdscr.addstr(height//2+2, width//2+1-5, "April fools!") stdscr.refresh() time.sleep(2) stdscr.addstr(height//2+4, width//2+1-13, "New software is on the way!") stdscr.refresh() time.sleep(4)
def inter_loop(self): start_y, start_x = self.scrs['text'].getyx() pad = Textbox(self.scrs['text']) pad.stripspaces = True msg = ['Welcome to CBTM Client.','List of accepted commands(separate '+\ 'the commands and the arguments with ","):'] for k, v in self.commands.items(): msg.append('%s -- %s' % (k, v)) data = '' while True: self.show_response(msg) msg = [] #Get user input while True: ch = self.scrs['text'].getch() if ch in self.pr: self.scrs['text'].echochar(ch) else: if ch == ord('\n'): data = self.get_text(pad) break elif ch == 14: continue elif ch == 16: self.show_text(data) self.scrs['text'].move(1, 1 + len(data)) elif ch == 263: tmp = self.get_text(pad)[:-1] self.show_text(tmp) self.scrs['text'].move(1, 1 + len(tmp)) else: # self.show_text(str(ch)) pad.do_command(ch) y, x = self.scrs['text'].getyx() if x < start_x: self.text_reset() #Separate the text entered by the user on commas args = data.split(',') try: #Get the first argument (Code for command) code = args.pop(0) except IndexError: code = -1 #Interprets the command try: command = self.commands[code] except KeyError: msg.append('Code not recognized: ' + code) msg.append('Try:') for k, v in self.commands.items(): msg.append('%s -- %s' % (k, v)) continue if command == 'quit': break elif command == 'set_port': #Execute set port self.port = int(args[0]) msg.append('Port for CBTM communication set to %d' % self.port) continue elif command == 'set_host': #Execute set host self.host = str(args[0]) msg.append('CBTM host set to %s' % self.host) continue elif code in ['rl', 'us', 'su', 'sdu', 'rbu']: # Checks for number of arguments if len(args) != 1: msg.append('The requested command needs one argument.') continue elif code in ['ru']: # Checks for number of arguments if len(args) != 4: msg.append('The requested command needs 4 arguments.') continue msg.append('Message from CBTM:') try: # Send command to CBTM Server rp = self.send(self.structure(command, args)) msg.extend(rp.split('\n')) except socket.error: msg = [ 'No server active on port %d' % self.port, ]