command_help.dart 6.08 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
import 'logger.dart';
8
import 'platform.dart';
9 10 11
import 'terminal.dart';

const String fire = '🔥';
12
const String image = '🖼️';
13 14
const int maxLineWidth = 84;

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

  final Logger _logger;

  final AnsiTerminal _terminal;

  final Platform _platform;

  final OutputPreferences _outputPreferences;

35
  late final CommandHelpOption I = _makeOption(
36 37 38 39 40
    'I',
    'Toggle oversized image inversion $image.',
    'debugInvertOversizedImages',
  );

41
  late final CommandHelpOption L = _makeOption(
42 43 44 45
    'L',
    'Dump layer tree to the console.',
    'debugDumpLayerTree',
  );
46

47
  late final CommandHelpOption P = _makeOption(
48 49 50 51
    'P',
    'Toggle performance overlay.',
    'WidgetsApp.showPerformanceOverlay',
  );
52

53
  late final CommandHelpOption R = _makeOption(
54 55 56
    'R',
    'Hot restart.',
  );
57

58
  late final CommandHelpOption S = _makeOption(
59 60 61 62
    'S',
    'Dump accessibility tree in traversal order.',
    'debugDumpSemantics',
  );
63

64
  late final CommandHelpOption U = _makeOption(
65 66 67 68
    'U',
    'Dump accessibility tree in inverse hit test order.',
    'debugDumpSemantics',
  );
69

70
  late final CommandHelpOption a = _makeOption(
71 72 73 74
    'a',
    'Toggle timeline events for all widget build methods.',
    'debugProfileWidgetBuilds',
  );
75

76
  late final CommandHelpOption b = _makeOption(
77 78 79 80 81
    'b',
    'Toggle the platform brightness setting (dark and light mode).',
    'debugBrightnessOverride',
  );

82
  late final CommandHelpOption c = _makeOption(
83 84 85
    'c',
    'Clear the screen',
  );
86

87
  late final CommandHelpOption d = _makeOption(
88 89 90
    'd',
    'Detach (terminate "flutter run" but leave application running).',
  );
91

92
  late final CommandHelpOption g = _makeOption(
93 94 95 96
    'g',
    'Run source code generators.'
  );

97
  late final CommandHelpOption h = _makeOption(
98 99 100
    'h',
    'Repeat this help message.',
  );
101

102
  late final CommandHelpOption i = _makeOption(
103 104 105 106
    'i',
    'Toggle widget inspector.',
    'WidgetsApp.showWidgetInspectorOverride',
  );
107

108
  late final CommandHelpOption o = _makeOption(
109 110 111 112
    'o',
    'Simulate different operating systems.',
    'defaultTargetPlatform',
  );
113

114
  late final CommandHelpOption p = _makeOption(
115 116 117 118
    'p',
    'Toggle the display of construction lines.',
    'debugPaintSizeEnabled',
  );
119

120
  late final CommandHelpOption q = _makeOption(
121 122 123
    'q',
    'Quit (terminate the application on the device).',
  );
124

125
  late final CommandHelpOption r = _makeOption(
126 127 128
    'r',
    'Hot reload. $fire$fire$fire',
  );
129

130
  late final CommandHelpOption s = _makeOption(
131 132 133
    's',
    'Save a screenshot to flutter.png.',
  );
134

135
  late final CommandHelpOption t = _makeOption(
136 137 138 139
    't',
    'Dump rendering tree to the console.',
    'debugDumpRenderTree',
  );
140

141
  late final CommandHelpOption w = _makeOption(
142 143 144 145
    'w',
    'Dump widget hierarchy to the console.',
    'debugDumpApp',
  );
146

147
  late final CommandHelpOption z = _makeOption(
148 149 150
    'z',
    'Toggle elevation checker.',
  );
151

152
  late final CommandHelpOption k = _makeOption(
153 154 155 156
    'k',
    'Toggle CanvasKit rendering.',
  );

157
  late final CommandHelpOption M = _makeOption(
158 159 160 161
    'M',
    'Write SkSL shaders to a unique file in the project directory.',
  );

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  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 = '',
183 184 185 186
    required Logger logger,
    required Terminal terminal,
    required Platform platform,
    required OutputPreferences outputPreferences,
187 188 189 190 191 192 193
  }) : _logger = logger,
       _terminal = terminal,
       _platform = platform,
       _outputPreferences = outputPreferences;

  final Logger _logger;

194
  final Terminal _terminal;
195 196

  final Platform _platform;
197

198
  final OutputPreferences _outputPreferences;
199

200
  /// The key associated with this command.
201
  final String key;
202
  /// A description of what this command does.
203
  final String description;
204
  /// Text shown in parenthesis to give the context.
205 206 207 208 209 210 211 212 213
  final String inParenthesis;

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

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

  @override
  String toString() {
    final StringBuffer message = StringBuffer();
214
    message.writeAll(<String>[_terminal.bolden(key), description], ' ');
215 216 217
    if (!_hasTextInParenthesis) {
      return message.toString();
    }
218

219 220
    bool wrap = false;
    final int maxWidth = math.max(
221
      _outputPreferences.wrapColumn,
222 223 224 225 226 227 228 229 230 231 232 233 234
      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');
235
    }
236 237 238
    // pad according to the raw text
    message.write(''.padLeft(width - parentheticalText.length));
    message.write(_terminal.color(parentheticalText, TerminalColor.grey));
239

240
    // Terminals seem to require this because we have both bolded and colored
241 242 243 244 245
    // a line. Otherwise the next line comes out bold until a reset bold.
    if (_terminal.supportsColor) {
      message.write(AnsiTerminal.resetBold);
    }

246 247 248 249
    return message.toString();
  }

  void print() {
250
    _logger.printStatus(toString());
251 252
  }
}