Changeset 602:bb62c7392d44
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r599
|
r602
|
|
| 8 | 8 | # the terms of the GNU Public License (GPL) version 2. |
| 9 | 9 | |
| | 10 | import atexit |
| | 11 | import code |
| 10 | 12 | import nxt |
| | 13 | import os |
| | 14 | import readline |
| 11 | 15 | import struct |
| 12 | 16 | import sys |
| | 17 | import time |
| 13 | 18 | from nxt.lowlevel import get_device |
| 14 | 19 | |
| 15 | 20 | NXOS_INTERFACE = 0 |
| 16 | 21 | |
| 17 | | def prompt(): |
| 18 | | print "rcmd>", |
| 19 | | return raw_input() |
| | 22 | class 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 |
| 20 | 55 | |
| 21 | 56 | def main(): |
| … |
… |
|
| 29 | 64 | print "ok." |
| 30 | 65 | |
| 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." |
| 35 | 73 | |
| 36 | 74 | if __name__ == "__main__": |