terminal.dart 8.33 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import '../convert.dart';
8 9
import '../globals.dart';
import 'context.dart';
10
import 'io.dart' as io;
11 12
import 'platform.dart';

13 14 15 16 17 18 19 20 21 22
enum TerminalColor {
  red,
  green,
  blue,
  cyan,
  yellow,
  magenta,
  grey,
}

23 24 25 26
AnsiTerminal get terminal {
  return context?.get<AnsiTerminal>() ?? _defaultAnsiTerminal;
}
final AnsiTerminal _defaultAnsiTerminal = AnsiTerminal();
27

28 29 30 31
OutputPreferences get outputPreferences {
  return context?.get<OutputPreferences>() ?? _defaultOutputPreferences;
}
final OutputPreferences _defaultOutputPreferences = OutputPreferences();
32 33 34 35 36 37 38 39

/// A class that contains the context settings for command text output to the
/// console.
class OutputPreferences {
  OutputPreferences({
    bool wrapText,
    int wrapColumn,
    bool showColor,
40
  }) : wrapText = wrapText ?? io.stdio.hasTerminal,
41 42
       _overrideWrapColumn = wrapColumn,
       showColor = showColor ?? platform.stdoutSupportsAnsi ?? false;
43

44 45 46
  /// A version of this class for use in tests.
  OutputPreferences.test() : wrapText = false, _overrideWrapColumn = null, showColor = false;

47 48 49 50 51
  /// If [wrapText] is true, then any text sent to the context's [Logger]
  /// instance (e.g. from the [printError] or [printStatus] functions) will be
  /// wrapped (newlines added between words) to be no longer than the
  /// [wrapColumn] specifies. Defaults to true if there is a terminal. To
  /// determine if there's a terminal, [OutputPreferences] asks the context's
52
  /// stdio.
53 54
  final bool wrapText;

55 56 57 58
  /// The terminal width used by the [wrapText] function if there is no terminal
  /// attached to [io.Stdio], --wrap is on, and --wrap-columns was not specified.
  static const int kDefaultTerminalColumns = 100;

59 60 61 62
  /// The column at which output sent to the context's [Logger] instance
  /// (e.g. from the [printError] or [printStatus] functions) will be wrapped.
  /// Ignored if [wrapText] is false. Defaults to the width of the output
  /// terminal, or to [kDefaultTerminalColumns] if not writing to a terminal.
63 64
  final int _overrideWrapColumn;
  int get wrapColumn {
65
    return _overrideWrapColumn ?? io.stdio.terminalColumns ?? kDefaultTerminalColumns;
66
  }
67 68 69 70 71 72 73 74 75 76 77 78

  /// Whether or not to output ANSI color codes when writing to the output
  /// terminal. Defaults to whatever [platform.stdoutSupportsAnsi] says if
  /// writing to a terminal, and false otherwise.
  final bool showColor;

  @override
  String toString() {
    return '$runtimeType[wrapText: $wrapText, wrapColumn: $wrapColumn, showColor: $showColor]';
  }
}

79
class AnsiTerminal {
80
  static const String bold = '\u001B[1m';
81 82 83
  static const String resetAll = '\u001B[0m';
  static const String resetColor = '\u001B[39m';
  static const String resetBold = '\u001B[22m';
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  static const String clear = '\u001B[2J\u001B[H';

  static const String red = '\u001b[31m';
  static const String green = '\u001b[32m';
  static const String blue = '\u001b[34m';
  static const String cyan = '\u001b[36m';
  static const String magenta = '\u001b[35m';
  static const String yellow = '\u001b[33m';
  static const String grey = '\u001b[1;30m';

  static const Map<TerminalColor, String> _colorMap = <TerminalColor, String>{
    TerminalColor.red: red,
    TerminalColor.green: green,
    TerminalColor.blue: blue,
    TerminalColor.cyan: cyan,
    TerminalColor.magenta: magenta,
    TerminalColor.yellow: yellow,
    TerminalColor.grey: grey,
  };
103

104 105
  static String colorCode(TerminalColor color) => _colorMap[color];

106 107
  bool get supportsColor => platform.stdoutSupportsAnsi ?? false;
  final RegExp _boldControls = RegExp('(${RegExp.escape(resetBold)}|${RegExp.escape(bold)})');
108

109 110 111 112 113
  /// Whether we are interacting with the flutter tool via the terminal.
  ///
  /// If not set, defaults to false.
  bool usesTerminalUi = false;

114
  String bolden(String message) {
115
    assert(message != null);
116
    if (!supportsColor || message.isEmpty) {
117
      return message;
118
    }
119
    final StringBuffer buffer = StringBuffer();
120 121 122 123 124 125 126
    for (String line in message.split('\n')) {
      // If there were bolds or resetBolds in the string before, then nuke them:
      // they're redundant. This prevents previously embedded resets from
      // stopping the boldness.
      line = line.replaceAll(_boldControls, '');
      buffer.writeln('$bold$line$resetBold');
    }
127 128 129 130 131 132 133
    final String result = buffer.toString();
    // avoid introducing a new newline to the emboldened text
    return (!message.endsWith('\n') && result.endsWith('\n'))
        ? result.substring(0, result.length - 1)
        : result;
  }

134 135
  String color(String message, TerminalColor color) {
    assert(message != null);
136
    if (!supportsColor || color == null || message.isEmpty) {
137
      return message;
138
    }
139
    final StringBuffer buffer = StringBuffer();
140 141 142 143 144 145 146 147
    final String colorCodes = _colorMap[color];
    for (String line in message.split('\n')) {
      // If there were resets in the string before, then keep them, but
      // restart the color right after. This prevents embedded resets from
      // stopping the colors, and allows nesting of colors.
      line = line.replaceAll(resetColor, '$resetColor$colorCodes');
      buffer.writeln('$colorCodes$line$resetColor');
    }
148 149 150 151 152 153 154 155
    final String result = buffer.toString();
    // avoid introducing a new newline to the colored text
    return (!message.endsWith('\n') && result.endsWith('\n'))
        ? result.substring(0, result.length - 1)
        : result;
  }

  String clearScreen() => supportsColor ? clear : '\n\n';
156 157

  set singleCharMode(bool value) {
158 159 160 161 162 163 164 165 166 167 168
    if (!io.stdinHasTerminal) {
      return;
    }
    final io.Stdin stdin = io.stdin;
    // The order of setting lineMode and echoMode is important on Windows.
    if (value) {
      stdin.echoMode = false;
      stdin.lineMode = false;
    } else {
      stdin.lineMode = true;
      stdin.echoMode = true;
169 170 171 172 173 174 175 176
    }
  }

  Stream<String> _broadcastStdInString;

  /// Return keystrokes from the console.
  ///
  /// Useful when the console is in [singleCharMode].
177
  Stream<String> get keystrokes {
178
    _broadcastStdInString ??= io.stdin.transform<String>(const AsciiDecoder(allowInvalid: true)).asBroadcastStream();
179 180 181
    return _broadcastStdInString;
  }

182 183
  /// Prompts the user to input a character within a given list. Re-prompts if
  /// entered character is not in the list.
184
  ///
185 186 187
  /// The `prompt`, if non-null, is the text displayed prior to waiting for user
  /// input each time. If `prompt` is non-null and `displayAcceptedCharacters`
  /// is true, the accepted keys are printed next to the `prompt`.
188
  ///
189 190 191 192
  /// The returned value is the user's input; if `defaultChoiceIndex` is not
  /// null, and the user presses enter without any other input, the return value
  /// will be the character in `acceptedCharacters` at the index given by
  /// `defaultChoiceIndex`.
193 194
  ///
  /// If [usesTerminalUi] is false, throws a [StateError].
195 196 197
  Future<String> promptForCharInput(
    List<String> acceptedCharacters, {
    String prompt,
198
    int defaultChoiceIndex,
199
    bool displayAcceptedCharacters = true,
200 201 202
  }) async {
    assert(acceptedCharacters != null);
    assert(acceptedCharacters.isNotEmpty);
203 204
    assert(prompt == null || prompt.isNotEmpty);
    assert(displayAcceptedCharacters != null);
205 206 207
    if (!usesTerminalUi) {
      throw StateError('cannot prompt without a terminal ui');
    }
208 209 210
    List<String> charactersToDisplay = acceptedCharacters;
    if (defaultChoiceIndex != null) {
      assert(defaultChoiceIndex >= 0 && defaultChoiceIndex < acceptedCharacters.length);
211
      charactersToDisplay = List<String>.from(charactersToDisplay);
212 213 214
      charactersToDisplay[defaultChoiceIndex] = bolden(charactersToDisplay[defaultChoiceIndex]);
      acceptedCharacters.add('\n');
    }
215 216
    String choice;
    singleCharMode = true;
217 218
    while (choice == null || choice.length > 1 || !acceptedCharacters.contains(choice)) {
      if (prompt != null) {
219
        printStatus(prompt, emphasis: true, newline: false);
220
        if (displayAcceptedCharacters) {
221
          printStatus(' [${charactersToDisplay.join("|")}]', newline: false);
222
        }
223 224
        printStatus(': ', emphasis: true, newline: false);
      }
225
      choice = await keystrokes.first;
226 227 228
      printStatus(choice);
    }
    singleCharMode = false;
229
    if (defaultChoiceIndex != null && choice == '\n') {
230
      choice = acceptedCharacters[defaultChoiceIndex];
231
    }
232 233 234
    return choice;
  }
}