Changeset 602:bb62c7392d44

Show
Ignore:
Timestamp:
06/11/08 11:23:37 (7 months ago)
Author:
Maxime Petazzoni <maxime.petazzoni@…>
Branch:
default
Message:

Improve the user interface by providing a readline-powered
prompt, with command history.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • usb_console/rcmd_frontend.py

    r599 r602  
    88# the terms of the GNU Public License (GPL) version 2. 
    99 
     10import atexit 
     11import code 
    1012import nxt 
     13import os 
     14import readline 
    1115import struct 
    1216import sys 
     17import time 
    1318from nxt.lowlevel import get_device 
    1419 
    1520NXOS_INTERFACE = 0 
    1621 
    17 def prompt(): 
    18     print "rcmd>", 
    19     return raw_input() 
     22class RcmdConsole(code.InteractiveConsole): 
     23 
     24  def __init__(self, locals=None, filename="<console>", 
     25               histfile=os.path.expanduser("~/.nxos-rcmd.hist")): 
     26    code.InteractiveConsole.__init__(self) 
     27    self.init_history(histfile) 
     28 
     29  def init_history(self, histfile): 
     30    readline.parse_and_bind("tab: complete") 
     31    if hasattr(readline, "read_history_file"): 
     32      try: 
     33        readline.read_history_file(histfile) 
     34      except IOError: 
     35        pass 
     36      atexit.register(self.save_history, histfile) 
     37 
     38  def save_history(self, histfile): 
     39    readline.write_history_file(histfile) 
     40 
     41 
     42  def set_brick(self, brick): 
     43    self.brick = brick 
     44 
     45  def push(self, line): 
     46    if line == 'quit' or line == 'exit': 
     47      print "Use Ctrl-D (i.e. EOF) to exit" 
     48      return False 
     49 
     50    self.brick.write(line) 
     51    if line == 'end': 
     52      sys.exit(0) 
     53 
     54    return False 
    2055 
    2156def main(): 
     
    2964    print "ok." 
    3065 
    31     while True: 
    32       command = prompt() 
    33       if not brick.write(command + '\n') or command == 'end': 
    34         return 
     66    prompt = RcmdConsole() 
     67    prompt.set_brick(brick) 
     68    prompt.interact('Remote robot command console.') 
     69 
     70    print "Closing link...", 
     71    brick.write('end') 
     72    print "done." 
    3573 
    3674if __name__ == "__main__":