command_help.dart 6.1 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter 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:math' as math;

7 8 9
import 'package:meta/meta.dart';

import 'logger.dart';
10
import 'platform.dart';
11 12
import 'terminal.dart';

13 14
// ignore_for_file: non_constant_identifier_names

15 16 17
const String fire = '🔥';
const int maxLineWidth = 84;

18
/// Encapsulates the help text construction and printing.
19
class CommandHelp {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  CommandHelp({
    @required Logger logger,
    @required AnsiTerminal terminal,
    @required Platform platform,
    @required OutputPreferences outputPreferences,
  }) : _logger = logger,
       _terminal = terminal,
       _platform = platform,
       _outputPreferences = outputPreferences;

  final Logger _logger;

  final AnsiTerminal _terminal;

  final Platform _platform;

  final OutputPreferences _outputPreferences;

  CommandHelpOption _L;
39 40 41 42 43
  CommandHelpOption get L => _L ??= _makeOption(
    'L',
    'Dump layer tree to the console.',
    'debugDumpLayerTree',
  );
44 45

  CommandHelpOption _P;
46 47 48 49 50
  CommandHelpOption get P => _P ??= _makeOption(
    'P',
    'Toggle performance overlay.',
    'WidgetsApp.showPerformanceOverlay',
  );
51 52

  CommandHelpOption _R;
53 54 55 56
  CommandHelpOption get R => _R ??= _makeOption(
    'R',
    'Hot restart.',
  );
57 58

  CommandHelpOption _S;
59 60 61 62 63
  CommandHelpOption get S => _S ??= _makeOption(
    'S',
    'Dump accessibility tree in traversal order.',
    'debugDumpSemantics',
  );
64 65

  CommandHelpOption _U;
66 67 68 69 70
  CommandHelpOption get U => _U ??= _makeOption(
    'U',
    'Dump accessibility tree in inverse hit test order.',
    'debugDumpSemantics',
  );
71 72

  CommandHelpOption _a;
73 74 75 76 77
  CommandHelpOption get a => _a ??= _makeOption(
    'a',
    'Toggle timeline events for all widget build methods.',
    'debugProfileWidgetBuilds',
  );
78

79
  CommandHelpOption _c;
80 81 82 83
  CommandHelpOption get c => _c ??= _makeOption(
    'c',
    'Clear the screen',
  );
84

85
  CommandHelpOption _d;
86 87 88 89
  CommandHelpOption get d => _d ??= _makeOption(
    'd',
    'Detach (terminate "flutter run" but leave application running).',
  );
90 91

  CommandHelpOption _h;
92 93 94 95
  CommandHelpOption get h => _h ??= _makeOption(
    'h',
    'Repeat this help message.',
  );
96 97

  CommandHelpOption _i;
98 99 100 101 102
  CommandHelpOption get i => _i ??= _makeOption(
    'i',
    'Toggle widget inspector.',
    'WidgetsApp.showWidgetInspectorOverride',
  );
103 104

  CommandHelpOption _o;
105 106 107 108 109
  CommandHelpOption get o => _o ??= _makeOption(
    'o',
    'Simulate different operating systems.',
    'defaultTargetPlatform',
  );
110 111

  CommandHelpOption _p;
112 113 114 115 116
  CommandHelpOption get p => _p ??= _makeOption(
    'p',
    'Toggle the display of construction lines.',
    'debugPaintSizeEnabled',
  );
117 118

  CommandHelpOption _q;
119 120 121 122
  CommandHelpOption get q => _q ??= _makeOption(
    'q',
    'Quit (terminate the application on the device).',
  );
123 124

  CommandHelpOption _r;
125 126 127 128
  CommandHelpOption get r => _r ??= _makeOption(
    'r',
    'Hot reload. $fire$fire$fire',
  );
129 130

  CommandHelpOption _s;
131 132 133 134
  CommandHelpOption get s => _s ??= _makeOption(
    's',
    'Save a screenshot to flutter.png.',
  );
135 136

  CommandHelpOption _t;
137 138 139 140 141
  CommandHelpOption get t => _t ??= _makeOption(
    't',
    'Dump rendering tree to the console.',
    'debugDumpRenderTree',
  );
142

143 144 145 146 147 148
  CommandHelpOption _v;
  CommandHelpOption get v => _v ??= _makeOption(
    'v',
    'Launch DevTools.',
  );

149
  CommandHelpOption _w;
150 151 152 153 154
  CommandHelpOption get w => _w ??= _makeOption(
    'w',
    'Dump widget hierarchy to the console.',
    'debugDumpApp',
  );
155 156

  CommandHelpOption _z;
157 158 159 160
  CommandHelpOption get z => _z ??= _makeOption(
    'z',
    'Toggle elevation checker.',
  );
161

162 163 164 165 166 167
  CommandHelpOption _k;
  CommandHelpOption get k => _k ??= _makeOption(
    'k',
    'Toggle CanvasKit rendering.',
  );

168 169 170 171 172 173
  CommandHelpOption _M;
  CommandHelpOption get M => _M ??= _makeOption(
    'M',
    'Write SkSL shaders to a unique file in the project directory.',
  );

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  CommandHelpOption _makeOption(String key, String description, [
    String inParenthesis = '',
  ]) {
    return CommandHelpOption(
      key,
      description,
      inParenthesis: inParenthesis,
      logger: _logger,
      terminal: _terminal,
      platform: _platform,
      outputPreferences: _outputPreferences,
    );
  }
}

/// Encapsulates printing help text for a single option.
class CommandHelpOption {
  CommandHelpOption(
    this.key,
    this.description, {
    this.inParenthesis = '',
    @required Logger logger,
196
    @required Terminal terminal,
197 198 199 200 201 202 203 204 205
    @required Platform platform,
    @required OutputPreferences outputPreferences,
  }) : _logger = logger,
       _terminal = terminal,
       _platform = platform,
       _outputPreferences = outputPreferences;

  final Logger _logger;

206
  final Terminal _terminal;
207 208

  final Platform _platform;
209

210
  final OutputPreferences _outputPreferences;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

  /// The key associated with this command
  final String key;
  /// A description of what this command does
  final String description;
  /// Text shown in parenthesis to give the context
  final String inParenthesis;

  bool get _hasTextInParenthesis => inParenthesis != null && inParenthesis.isNotEmpty;

  int get _rawMessageLength => key.length + description.length;

  @override
  String toString() {
    final StringBuffer message = StringBuffer();
226
    message.writeAll(<String>[_terminal.bolden(key), description], ' ');
227 228 229
    if (!_hasTextInParenthesis) {
      return message.toString();
    }
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    bool wrap = false;
    final int maxWidth = math.max(
      _outputPreferences.wrapColumn ?? 0,
      maxLineWidth,
    );
    final int adjustedMessageLength = _platform.stdoutSupportsAnsi
      ? _rawMessageLength + 1
      : message.length;
    int width = maxWidth - adjustedMessageLength;
    final String parentheticalText = '($inParenthesis)';
    if (width < parentheticalText.length) {
      width = maxWidth;
      wrap = true;
    }
    if (wrap) {
      message.write('\n');
247
    }
248 249 250
    // pad according to the raw text
    message.write(''.padLeft(width - parentheticalText.length));
    message.write(_terminal.color(parentheticalText, TerminalColor.grey));
251 252 253 254
    return message.toString();
  }

  void print() {
255
    _logger.printStatus(toString());
256 257
  }
}