def ascii_split (s): output = [] lines = s.split('\n') for elem in lines: if ansi_len(elem) > 0: output.append(elem) return output
def handle_cmd (self): cmd = self.lines[self.line_index].get().strip() if not cmd: return op, param = self.split_cmd(cmd) func = self.ac.get(op) if func: func_rc = func(param) # take out the empty line empty_line = self.lines.popleft() assert(empty_line.ro_line == '') if not self.lines or self.lines[0].ro_line != cmd: self.lines.appendleft(CmdLine(cmd)) # back in self.lines.appendleft(empty_line) self.line_index = 0 readline.add_history(cmd) self.save_console_history() # back to readonly for line in self.lines: line.invalidate() assert(self.lines[0].modified == False) color = None if not func: self.last_status = "unknown command: '{0}'".format(format_text(cmd.split()[0], 'bold')) else: # internal commands if isinstance(func_rc, str): self.last_status = func_rc # RC response else: # success if func_rc: self.last_status = format_text("[OK]", 'green') # errors else: err_msgs = ascii_split(str(func_rc)) self.last_status = format_text(err_msgs[0], 'red') if len(err_msgs) > 1: self.last_status += " [{0} more errors messages]".format(len(err_msgs) - 1) color = 'red' # trim too long lines if ansi_len(self.last_status) > TrexTUI.MIN_COLS: self.last_status = format_text(self.last_status[:TrexTUI.MIN_COLS] + "...", color, 'bold')
else: # success if func_rc: self.last_status = format_text("[OK]", 'green') # errors else: err_msgs = ascii_split(str(func_rc)) self.last_status = format_text(err_msgs[0], 'red') if len(err_msgs) > 1: self.last_status += " [{0} more errors messages]".format(len(err_msgs) - 1) color = 'red' # trim too long lines if ansi_len(self.last_status) > TrexTUI.MIN_COLS: self.last_status = format_text(self.last_status[:TrexTUI.MIN_COLS] + "...", color, 'bold') def draw (self, buffer): buffer.write("\nPress 'ESC' for navigation panel...\n") buffer.write("status: \x1b[0K{0}\n".format(self.last_status)) buffer.write("\n{0}\x1b[0K".format(self.generate_prompt(prefix = 'tui'))) self.lines[self.line_index].draw(buffer) # a readline alike command line - can be modified during edit class CmdLine(object): def __init__ (self, line): self.ro_line = line self.w_line = None
# RC response else: # success if func_rc: self.last_status = format_text("[OK]", 'green') # errors else: err_msgs = ascii_split(str(func_rc)) self.last_status = format_text(err_msgs[0], 'red') if len(err_msgs) > 1: self.last_status += " [{0} more errors messages]".format( len(err_msgs) - 1) color = 'red' # trim too long lines if ansi_len(self.last_status) > TrexTUI.MIN_COLS: self.last_status = format_text( self.last_status[:TrexTUI.MIN_COLS] + "...", color, 'bold') def draw(self, buffer): buffer.write("\nPress 'ESC' for navigation panel...\n") buffer.write("status: \x1b[0K{0}\n".format(self.last_status)) buffer.write("\n{0}\x1b[0K".format(self.generate_prompt(prefix='tui'))) self.lines[self.line_index].draw(buffer) # a readline alike command line - can be modified during edit class CmdLine(object): def __init__(self, line): self.ro_line = line self.w_line = None