Index: tools/ddbg/lib/commando.dart |
=================================================================== |
--- tools/ddbg/lib/commando.dart (revision 30608) |
+++ tools/ddbg/lib/commando.dart (working copy) |
@@ -30,18 +30,45 @@ |
static const runeSpace = 0x20; |
static const runeDEL = 0x7F; |
- Commando(this._stdin, |
- this._stdout, |
- this._handleCommand, |
- {this.prompt : '> ', this.completer : null}) { |
+ StreamController<String> _commandController; |
+ |
+ Stream get commands => _commandController.stream; |
+ |
+ Commando({consoleIn, |
+ consoleOut, |
+ this.prompt : '> ', |
+ this.completer : null}) { |
+ _stdin = (consoleIn != null ? consoleIn : stdin); |
+ _stdout = (consoleOut != null ? consoleOut : stdout); |
+ _commandController = new StreamController<String>( |
+ onCancel: _onCancel); |
_stdin.echoMode = false; |
_stdin.lineMode = false; |
_screenWidth = _term.cols - 1; |
_writePrompt(); |
+ // TODO(turnidge): Handle errors in _stdin here. |
_stdinSubscription = |
- _stdin.transform(UTF8.decoder).listen(_handleText, onDone:done); |
+ _stdin.transform(UTF8.decoder).listen(_handleText, onDone:_done); |
} |
+ Future _onCancel() { |
+ _stdin.echoMode = true; |
+ _stdin.lineMode = true; |
+ var future = _stdinSubscription.cancel(); |
+ if (future != null) { |
+ return future; |
+ } else { |
+ return new Future.value(); |
+ } |
+ } |
+ |
+ // Before terminating, call close() to restore terminal settings. |
+ void _done() { |
+ _onCancel().then((_) { |
+ _commandController.close(); |
+ }); |
+ } |
+ |
void _handleText(String text) { |
try { |
if (!_promptShown) { |
@@ -79,11 +106,7 @@ |
} |
} |
} catch(e, trace) { |
- stderr.writeln('\nUnexpected exception: $e'); |
- stderr.writeln(trace); |
- stderr.close().then((_) { |
- done(); |
- }); |
+ _commandController.addError(e, trace); |
} |
} |
@@ -102,8 +125,8 @@ |
case runeCtrlD: |
if (_currentLine.length == 0) { |
// ^D on an empty line means quit. |
- _stdout.writeln(); |
- done(); |
+ _stdout.writeln("^D"); |
+ _done(); |
} else { |
_delete(); |
} |
@@ -207,12 +230,6 @@ |
return (char >= 0x00 && char < 0x20) || (char == 0x7f); |
} |
- void done() { |
- _stdin.echoMode = true; |
- _stdin.lineMode = true; |
- _stdinSubscription.cancel(); |
- } |
- |
void _writePromptAndLine() { |
_writePrompt(); |
var pos = _writeRange(_currentLine, 0, _currentLine.length); |
@@ -368,7 +385,7 @@ |
_stdout.writeln(); |
// Call the user's command handler. |
- _handleCommand(new String.fromCharCodes(_currentLine)); |
+ _commandController.add(new String.fromCharCodes(_currentLine)); |
_currentLine = []; |
_cursorPos = 0; |
@@ -610,7 +627,6 @@ |
Stdin _stdin; |
StreamSubscription _stdinSubscription; |
IOSink _stdout; |
- final _handleCommand; |
final String prompt; |
bool _promptShown = true; |
final CommandCompleter completer; |
@@ -676,8 +692,11 @@ |
void _handleCommand(String rawCommand) { |
String command = rawCommand.trim(); |
+ cmdo.hide(); |
if (command == 'quit') { |
- cmdo.done(); |
+ cmdo.close().then((_) { |
+ print('Exiting'); |
+ }); |
} else if (command == 'help') { |
switch (_helpCount) { |
case 0: |
@@ -702,11 +721,12 @@ |
} else { |
print('Received command($command)'); |
} |
+ cmdo.show(); |
} |
void main() { |
- stdout.writeln('[Commando demo]'); |
- cmd = new Commando(stdin, stdout, _handleCommand, |
- completer:_myCompleter); |
+ print('[Commando demo]'); |
+ cmdo = new Commando(completer:_myCompleter); |
+ cmdo.commands.listen(_handleCommand); |
} |