OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 library commando; |
| 6 |
5 import 'dart:async'; | 7 import 'dart:async'; |
6 import 'dart:convert'; | 8 import 'dart:convert'; |
7 import 'dart:io'; | 9 import 'dart:io'; |
8 import 'dart:math'; | 10 import 'dart:math'; |
9 | 11 |
10 import 'terminfo.dart'; | 12 import 'package:ddbg/terminfo.dart'; |
11 | 13 |
12 typedef List<String> CommandCompleter(List<String> commandParts); | 14 typedef List<String> CommandCompleter(List<String> commandParts); |
13 | 15 |
14 class Commando { | 16 class Commando { |
15 // Ctrl keys | 17 // Ctrl keys |
16 static const runeCtrlA = 0x01; | 18 static const runeCtrlA = 0x01; |
17 static const runeCtrlB = 0x02; | 19 static const runeCtrlB = 0x02; |
18 static const runeCtrlD = 0x04; | 20 static const runeCtrlD = 0x04; |
19 static const runeCtrlE = 0x05; | 21 static const runeCtrlE = 0x05; |
20 static const runeCtrlF = 0x06; | 22 static const runeCtrlF = 0x06; |
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 // Write the new text. | 487 // Write the new text. |
486 pos = _writeRange(newLine, pos, newLine.length); | 488 pos = _writeRange(newLine, pos, newLine.length); |
487 | 489 |
488 // Clear any extra characters at the end. | 490 // Clear any extra characters at the end. |
489 pos = _clearRange(pos, _currentLine.length); | 491 pos = _clearRange(pos, _currentLine.length); |
490 | 492 |
491 // Move the cursor back to the input point. | 493 // Move the cursor back to the input point. |
492 _cursorPos = _move(pos, newCursorPos); | 494 _cursorPos = _move(pos, newCursorPos); |
493 _currentLine = newLine; | 495 _currentLine = newLine; |
494 } | 496 } |
| 497 |
| 498 void print(String text) { |
| 499 bool togglePrompt = _promptShown; |
| 500 if (togglePrompt) { |
| 501 hide(); |
| 502 } |
| 503 _stdout.writeln(text); |
| 504 if (togglePrompt) { |
| 505 show(); |
| 506 } |
| 507 } |
495 | 508 |
496 void hide() { | 509 void hide() { |
497 if (!_promptShown) { | 510 if (!_promptShown) { |
498 return; | 511 return; |
499 } | 512 } |
500 _promptShown = false; | 513 _promptShown = false; |
501 // We need to erase everything, including the prompt. | 514 // We need to erase everything, including the prompt. |
502 var curLine = _getLine(_cursorPos); | 515 var curLine = _getLine(_cursorPos); |
503 var lastLine = _getLine(_currentLine.length); | 516 var lastLine = _getLine(_currentLine.length); |
504 | 517 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 if ('gondola'.startsWith(lastWord)) { | 693 if ('gondola'.startsWith(lastWord)) { |
681 completions.add('gondola'); | 694 completions.add('gondola'); |
682 } | 695 } |
683 | 696 |
684 return completions; | 697 return completions; |
685 } | 698 } |
686 | 699 |
687 | 700 |
688 int _helpCount = 0; | 701 int _helpCount = 0; |
689 Commando cmdo; | 702 Commando cmdo; |
| 703 var cmdoSubscription; |
690 | 704 |
691 | 705 |
692 void _handleCommand(String rawCommand) { | 706 void _handleCommand(String rawCommand) { |
693 String command = rawCommand.trim(); | 707 String command = rawCommand.trim(); |
694 cmdo.hide(); | 708 cmdo.hide(); |
695 if (command == 'quit') { | 709 if (command == 'quit') { |
696 cmdo.close().then((_) { | 710 var future = cmdoSubscription.cancel(); |
697 print('Exiting'); | 711 if (future != null) { |
698 }); | 712 future.then((_) { |
| 713 print('Exiting'); |
| 714 exit(0); |
| 715 }); |
| 716 } else { |
| 717 print('Exiting'); |
| 718 exit(0); |
| 719 } |
699 } else if (command == 'help') { | 720 } else if (command == 'help') { |
700 switch (_helpCount) { | 721 switch (_helpCount) { |
701 case 0: | 722 case 0: |
702 print('I will not help you.'); | 723 print('I will not help you.'); |
703 break; | 724 break; |
704 case 1: | 725 case 1: |
705 print('I mean it.'); | 726 print('I mean it.'); |
706 break; | 727 break; |
707 case 2: | 728 case 2: |
708 print('Seriously.'); | 729 print('Seriously.'); |
(...skipping 11 matching lines...) Expand all Loading... |
720 } else { | 741 } else { |
721 print('Received command($command)'); | 742 print('Received command($command)'); |
722 } | 743 } |
723 cmdo.show(); | 744 cmdo.show(); |
724 } | 745 } |
725 | 746 |
726 | 747 |
727 void main() { | 748 void main() { |
728 print('[Commando demo]'); | 749 print('[Commando demo]'); |
729 cmdo = new Commando(completer:_myCompleter); | 750 cmdo = new Commando(completer:_myCompleter); |
730 cmdo.commands.listen(_handleCommand); | 751 cmdoSubscription = cmdo.commands.listen(_handleCommand); |
731 } | 752 } |
OLD | NEW |