OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library test.runner.console; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:math' as math; |
| 9 |
| 10 import 'package:async/async.dart'; |
| 11 |
| 12 import '../util/io.dart'; |
| 13 import '../utils.dart'; |
| 14 |
| 15 /// An interactive console for taking user commands. |
| 16 class Console { |
| 17 /// The registered commands. |
| 18 final _commands = <String, _Command>{}; |
| 19 |
| 20 /// The pending next line of standard input, if we're waiting on one. |
| 21 CancelableOperation _nextLine; |
| 22 |
| 23 /// Whether the console is currently running. |
| 24 bool _running = false; |
| 25 |
| 26 /// The terminal escape for red text, or the empty string if this is Windows |
| 27 /// or not outputting to a terminal. |
| 28 final String _red; |
| 29 |
| 30 /// The terminal escape for bold text, or the empty string if this is |
| 31 /// Windows or not outputting to a terminal. |
| 32 final String _bold; |
| 33 |
| 34 /// The terminal escape for removing test coloring, or the empty string if |
| 35 /// this is Windows or not outputting to a terminal. |
| 36 final String _noColor; |
| 37 |
| 38 /// Creates a new [Console]. |
| 39 /// |
| 40 /// If [color] is true, this uses Unix terminal colors. |
| 41 Console({bool color: true}) |
| 42 : _red = color ? '\u001b[31m' : '', |
| 43 _bold = color ? '\u001b[1m' : '', |
| 44 _noColor = color ? '\u001b[0m' : '' { |
| 45 registerCommand("help", "Displays this help information.", _displayHelp); |
| 46 } |
| 47 |
| 48 /// Registers a command to be run whenever the user types [name]. |
| 49 /// |
| 50 /// The [description] should be a one-line description of the command to print |
| 51 /// in the help output. The [body] callback will be called when the user types |
| 52 /// the command, and may return a [Future]. |
| 53 void registerCommand(String name, String description, body()) { |
| 54 if (_commands.containsKey(name)) { |
| 55 throw new ArgumentError( |
| 56 'The console already has a command named "$name".'); |
| 57 } |
| 58 |
| 59 _commands[name] = new _Command(name, description, body); |
| 60 } |
| 61 |
| 62 /// Starts running the console. |
| 63 /// |
| 64 /// This prints the initial prompt and loops while waiting for user input. |
| 65 void start() { |
| 66 _running = true; |
| 67 invoke(() async { |
| 68 while (_running) { |
| 69 stdout.write("> "); |
| 70 _nextLine = cancelableNext(stdinLines); |
| 71 var commandName = await _nextLine.value; |
| 72 _nextLine = null; |
| 73 |
| 74 var command = _commands[commandName]; |
| 75 if (command == null) { |
| 76 stderr.writeln( |
| 77 "${_red}Unknown command $_bold$commandName$_noColor$_red." |
| 78 "$_noColor"); |
| 79 } else { |
| 80 await command.body(); |
| 81 } |
| 82 } |
| 83 }); |
| 84 } |
| 85 |
| 86 /// Stops the console running. |
| 87 void stop() { |
| 88 _running = false; |
| 89 if (_nextLine != null) { |
| 90 stdout.writeln(); |
| 91 _nextLine.cancel(); |
| 92 } |
| 93 } |
| 94 |
| 95 /// Displays the help info for the console commands. |
| 96 void _displayHelp() { |
| 97 var maxCommandLength = _commands.values |
| 98 .map((command) => command.name.length) |
| 99 .reduce(math.max); |
| 100 |
| 101 for (var command in _commands.values) { |
| 102 var name = command.name.padRight(maxCommandLength + 4); |
| 103 print("$_bold$name$_noColor${command.description}"); |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 /// An individual console command. |
| 109 class _Command { |
| 110 /// The name of the command. |
| 111 final String name; |
| 112 |
| 113 /// The single-line description of the command. |
| 114 final String description; |
| 115 |
| 116 /// The callback to run when the command is invoked. |
| 117 final Function body; |
| 118 |
| 119 _Command(this.name, this.description, this.body); |
| 120 } |
OLD | NEW |