logger.dart 29.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6

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

9
import '../base/context.dart';
10
import '../convert.dart';
11
import '../globals.dart' as globals;
12
import 'io.dart';
13
import 'terminal.dart' show AnsiTerminal, Terminal, TerminalColor, OutputPreferences;
14
import 'utils.dart';
15

16
const int kDefaultStatusPadding = 59;
17 18 19 20 21 22
const Duration _kFastOperation = Duration(seconds: 2);
const Duration _kSlowOperation = Duration(minutes: 2);

/// The [TimeoutConfiguration] instance.
///
/// If not provided via injection, a default instance is provided.
23
TimeoutConfiguration get timeoutConfiguration => context.get<TimeoutConfiguration>() ?? const TimeoutConfiguration();
24

25 26 27 28 29 30 31 32 33
/// A factory for generating [Stopwatch] instances for [Status] instances.
class StopwatchFactory {
  /// const constructor so that subclasses may be const.
  const StopwatchFactory();

  /// Create a new [Stopwatch] instance.
  Stopwatch createStopwatch() => Stopwatch();
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class TimeoutConfiguration {
  const TimeoutConfiguration();

  /// The expected time that various "slow" operations take, such as running
  /// the analyzer.
  ///
  /// Defaults to 2 minutes.
  Duration get slowOperation => _kSlowOperation;

  /// The expected time that various "fast" operations take, such as a hot
  /// reload.
  ///
  /// Defaults to 2 seconds.
  Duration get fastOperation => _kFastOperation;
}
49

50
typedef VoidCallback = void Function();
51

52
abstract class Logger {
Devon Carew's avatar
Devon Carew committed
53
  bool get isVerbose => false;
54

55 56
  bool quiet = false;

57
  bool get supportsColor;
58

59 60
  bool get hasTerminal;

61
  Terminal get _terminal;
62 63 64 65

  OutputPreferences get _outputPreferences;

  TimeoutConfiguration get _timeoutConfiguration;
66

67
  /// Display an error `message` to the user. Commands should use this if they
68
  /// fail in some way.
69
  ///
70 71 72
  /// The `message` argument is printed to the stderr in red by default.
  ///
  /// The `stackTrace` argument is the stack trace that will be printed if
73
  /// supplied.
74 75 76 77
  ///
  /// The `emphasis` argument will cause the output message be printed in bold text.
  ///
  /// The `color` argument will print the message in the supplied color instead
78 79
  /// of the default of red. Colors will not be printed if the output terminal
  /// doesn't support them.
80 81
  ///
  /// The `indent` argument specifies the number of spaces to indent the overall
82 83
  /// message. If wrapping is enabled in [outputPreferences], then the wrapped
  /// lines will be indented as well.
84 85
  ///
  /// If `hangingIndent` is specified, then any wrapped lines will be indented
86 87
  /// by this much more than the first line, if wrapping is enabled in
  /// [outputPreferences].
88 89 90
  ///
  /// If `wrap` is specified, then it overrides the
  /// `outputPreferences.wrapText` setting.
91 92 93 94 95
  void printError(
    String message, {
    StackTrace stackTrace,
    bool emphasis,
    TerminalColor color,
96 97
    int indent,
    int hangingIndent,
98
    bool wrap,
99
  });
100 101 102

  /// Display normal output of the command. This should be used for things like
  /// progress messages, success messages, or just normal command output.
103
  ///
104 105 106
  /// The `message` argument is printed to the stderr in red by default.
  ///
  /// The `stackTrace` argument is the stack trace that will be printed if
107
  /// supplied.
108 109
  ///
  /// If the `emphasis` argument is true, it will cause the output message be
110
  /// printed in bold text. Defaults to false.
111 112
  ///
  /// The `color` argument will print the message in the supplied color instead
113 114
  /// of the default of red. Colors will not be printed if the output terminal
  /// doesn't support them.
115 116
  ///
  /// If `newline` is true, then a newline will be added after printing the
117
  /// status. Defaults to true.
118 119
  ///
  /// The `indent` argument specifies the number of spaces to indent the overall
120 121
  /// message. If wrapping is enabled in [outputPreferences], then the wrapped
  /// lines will be indented as well.
122 123
  ///
  /// If `hangingIndent` is specified, then any wrapped lines will be indented
124 125
  /// by this much more than the first line, if wrapping is enabled in
  /// [outputPreferences].
126 127 128
  ///
  /// If `wrap` is specified, then it overrides the
  /// `outputPreferences.wrapText` setting.
129
  void printStatus(
130 131 132 133 134
    String message, {
    bool emphasis,
    TerminalColor color,
    bool newline,
    int indent,
135
    int hangingIndent,
136
    bool wrap,
137
  });
138 139 140 141

  /// Use this for verbose tracing output. Users can turn this output on in order
  /// to help diagnose issues with the toolchain or with their setup.
  void printTrace(String message);
Devon Carew's avatar
Devon Carew committed
142

143
  /// Start an indeterminate progress display.
144
  ///
145 146 147 148 149
  /// The `message` argument is the message to display to the user.
  ///
  /// The `timeout` argument sets a duration after which an additional message
  /// may be shown saying that the operation is taking a long time. (Not all
  /// [Status] subclasses show such a message.) Set this to null if the
Chris Bracken's avatar
Chris Bracken committed
150
  /// operation can legitimately take an arbitrary amount of time (e.g. waiting
151
  /// for the user).
152
  ///
153 154 155 156 157
  /// The `progressId` argument provides an ID that can be used to identify
  /// this type of progress (e.g. `hot.reload`, `hot.restart`).
  ///
  /// The `progressIndicatorPadding` can optionally be used to specify spacing
  /// between the `message` and the progress indicator, if any.
158 159
  Status startProgress(
    String message, {
160
    @required Duration timeout,
161
    String progressId,
162 163
    bool multilineOutput = false,
    int progressIndicatorPadding = kDefaultStatusPadding,
164
  });
165

166
  /// Send an event to be emitted.
167 168
  ///
  /// Only surfaces a value in machine modes, Loggers may ignore this message in
169 170
  /// non-machine modes.
  void sendEvent(String name, [Map<String, dynamic> args]) { }
171 172 173

  /// Clears all output.
  void clear();
174 175
}

176
class StdoutLogger extends Logger {
177
  StdoutLogger({
178
    @required Terminal terminal,
179 180 181
    @required Stdio stdio,
    @required OutputPreferences outputPreferences,
    @required TimeoutConfiguration timeoutConfiguration,
182
    StopwatchFactory stopwatchFactory = const StopwatchFactory(),
183 184 185 186
  })
    : _stdio = stdio,
      _terminal = terminal,
      _timeoutConfiguration = timeoutConfiguration,
187
      _outputPreferences = outputPreferences,
188
      _stopwatchFactory = stopwatchFactory;
189 190

  @override
191
  final Terminal _terminal;
192 193 194 195 196
  @override
  final OutputPreferences _outputPreferences;
  @override
  final TimeoutConfiguration _timeoutConfiguration;
  final Stdio _stdio;
197
  final StopwatchFactory _stopwatchFactory;
198

199 200
  Status _status;

201
  @override
Devon Carew's avatar
Devon Carew committed
202
  bool get isVerbose => false;
203

204 205 206 207 208 209
  @override
  bool get supportsColor => _terminal.supportsColor;

  @override
  bool get hasTerminal => _stdio.stdinHasTerminal;

210
  @override
211 212 213 214 215
  void printError(
    String message, {
    StackTrace stackTrace,
    bool emphasis,
    TerminalColor color,
216 217
    int indent,
    int hangingIndent,
218
    bool wrap,
219
  }) {
220
    _status?.pause();
221
    message ??= '';
222 223 224 225 226 227
    message = wrapText(message,
      indent: indent,
      hangingIndent: hangingIndent,
      shouldWrap: wrap ?? _outputPreferences.wrapText,
      columnWidth: _outputPreferences.wrapColumn,
    );
228
    if (emphasis == true) {
229
      message = _terminal.bolden(message);
230
    }
231
    message = _terminal.color(message, color ?? TerminalColor.red);
232
    writeToStdErr('$message\n');
233
    if (stackTrace != null) {
234
      writeToStdErr('$stackTrace\n');
235
    }
236
    _status?.resume();
237 238
  }

239
  @override
240
  void printStatus(
241 242 243 244 245
    String message, {
    bool emphasis,
    TerminalColor color,
    bool newline,
    int indent,
246
    int hangingIndent,
247
    bool wrap,
248
  }) {
249
    _status?.pause();
250
    message ??= '';
251 252 253 254 255 256
    message = wrapText(message,
      indent: indent,
      hangingIndent: hangingIndent,
      shouldWrap: wrap ?? _outputPreferences.wrapText,
      columnWidth: _outputPreferences.wrapColumn,
    );
257
    if (emphasis == true) {
258
      message = _terminal.bolden(message);
259 260
    }
    if (color != null) {
261
      message = _terminal.color(message, color);
262 263
    }
    if (newline != false) {
264
      message = '$message\n';
265
    }
266
    writeToStdOut(message);
267
    _status?.resume();
268 269 270
  }

  @protected
271
  void writeToStdOut(String message) => _stdio.stdoutWrite(message);
272 273

  @protected
274
  void writeToStdErr(String message) => _stdio.stderrWrite(message);
275

276
  @override
277
  void printTrace(String message) { }
278

279
  @override
280 281
  Status startProgress(
    String message, {
282
    @required Duration timeout,
283
    String progressId,
284 285
    bool multilineOutput = false,
    int progressIndicatorPadding = kDefaultStatusPadding,
286
  }) {
287
    assert(progressIndicatorPadding != null);
Devon Carew's avatar
Devon Carew committed
288 289
    if (_status != null) {
      // Ignore nested progresses; return a no-op status object.
290 291 292
      return SilentStatus(
        timeout: timeout,
        onFinish: _clearStatus,
293 294
        timeoutConfiguration: _timeoutConfiguration,
        stopwatch: _stopwatchFactory.createStopwatch(),
295
      )..start();
296
    }
297
    if (supportsColor) {
298
      _status = AnsiStatus(
299
        message: message,
300
        timeout: timeout,
301
        multilineOutput: multilineOutput,
302 303
        padding: progressIndicatorPadding,
        onFinish: _clearStatus,
304 305
        stdio: _stdio,
        timeoutConfiguration: _timeoutConfiguration,
306
        stopwatch: _stopwatchFactory.createStopwatch(),
307
        terminal: _terminal,
308
      )..start();
Devon Carew's avatar
Devon Carew committed
309
    } else {
310 311
      _status = SummaryStatus(
        message: message,
312
        timeout: timeout,
313 314
        padding: progressIndicatorPadding,
        onFinish: _clearStatus,
315 316
        stdio: _stdio,
        timeoutConfiguration: _timeoutConfiguration,
317
        stopwatch: _stopwatchFactory.createStopwatch(),
318
      )..start();
319
    }
320
    return _status;
321
  }
322 323 324 325

  void _clearStatus() {
    _status = null;
  }
326 327

  @override
328
  void sendEvent(String name, [Map<String, dynamic> args]) { }
329 330 331 332 333 334 335

  @override
  void clear() {
    _status?.pause();
    writeToStdOut(_terminal.clearScreen() + '\n');
    _status?.resume();
  }
336 337
}

338 339 340
/// A [StdoutLogger] which replaces Unicode characters that cannot be printed to
/// the Windows console with alternative symbols.
///
341 342 343 344 345
/// By default, Windows uses either "Consolas" or "Lucida Console" as fonts to
/// render text in the console. Both fonts only have a limited character set.
/// Unicode characters, that are not available in either of the two default
/// fonts, should be replaced by this class with printable symbols. Otherwise,
/// they will show up as the unrepresentable character symbol '�'.
346
class WindowsStdoutLogger extends StdoutLogger {
347
  WindowsStdoutLogger({
348
    @required Terminal terminal,
349 350 351
    @required Stdio stdio,
    @required OutputPreferences outputPreferences,
    @required TimeoutConfiguration timeoutConfiguration,
352
    StopwatchFactory stopwatchFactory = const StopwatchFactory(),
353 354 355 356 357
  }) : super(
      terminal: terminal,
      stdio: stdio,
      outputPreferences: outputPreferences,
      timeoutConfiguration: timeoutConfiguration,
358
      stopwatchFactory: stopwatchFactory,
359 360
    );

361 362
  @override
  void writeToStdOut(String message) {
363
    // TODO(jcollins-g): wrong abstraction layer for this, move to [Stdio].
364 365 366
    final String windowsMessage = _terminal.supportsEmoji
      ? message
      : message.replaceAll('🔥', '')
367
               .replaceAll('🖼️', '')
368
               .replaceAll('✗', 'X')
369 370
               .replaceAll('✓', '√')
               .replaceAll('🔨', '');
371
    _stdio.stdoutWrite(windowsMessage);
372 373 374
  }
}

375
class BufferLogger extends Logger {
376 377 378
  BufferLogger({
    @required AnsiTerminal terminal,
    @required OutputPreferences outputPreferences,
379 380
    TimeoutConfiguration timeoutConfiguration = const TimeoutConfiguration(),
    StopwatchFactory stopwatchFactory = const StopwatchFactory(),
381 382
  }) : _outputPreferences = outputPreferences,
       _terminal = terminal,
383 384
       _timeoutConfiguration = timeoutConfiguration,
       _stopwatchFactory = stopwatchFactory;
385

386
  /// Create a [BufferLogger] with test preferences.
387 388 389 390 391 392 393 394 395
  BufferLogger.test({
    Terminal terminal,
    OutputPreferences outputPreferences,
  }) : _terminal = terminal ?? Terminal.test(),
       _outputPreferences = outputPreferences ?? OutputPreferences.test(),
       _timeoutConfiguration = const TimeoutConfiguration(),
       _stopwatchFactory = const StopwatchFactory();


396 397 398 399
  @override
  final OutputPreferences _outputPreferences;

  @override
400
  final Terminal _terminal;
401 402 403 404

  @override
  final TimeoutConfiguration _timeoutConfiguration;

405 406
  final StopwatchFactory _stopwatchFactory;

407
  @override
Devon Carew's avatar
Devon Carew committed
408 409
  bool get isVerbose => false;

410 411 412
  @override
  bool get supportsColor => _terminal.supportsColor;

413 414 415
  final StringBuffer _error = StringBuffer();
  final StringBuffer _status = StringBuffer();
  final StringBuffer _trace = StringBuffer();
416
  final StringBuffer _events = StringBuffer();
417 418 419 420

  String get errorText => _error.toString();
  String get statusText => _status.toString();
  String get traceText => _trace.toString();
421
  String get eventText => _events.toString();
422

423 424 425
  @override
  bool get hasTerminal => false;

426
  @override
427 428 429 430 431
  void printError(
    String message, {
    StackTrace stackTrace,
    bool emphasis,
    TerminalColor color,
432 433
    int indent,
    int hangingIndent,
434
    bool wrap,
435
  }) {
436 437 438 439 440 441 442
    _error.writeln(_terminal.color(
      wrapText(message,
        indent: indent,
        hangingIndent: hangingIndent,
        shouldWrap: wrap ?? _outputPreferences.wrapText,
        columnWidth: _outputPreferences.wrapColumn,
      ),
443 444
      color ?? TerminalColor.red,
    ));
445 446
  }

447
  @override
448
  void printStatus(
449 450 451 452 453
    String message, {
    bool emphasis,
    TerminalColor color,
    bool newline,
    int indent,
454
    int hangingIndent,
455
    bool wrap,
456
  }) {
457
    if (newline != false) {
458 459 460 461 462 463
      _status.writeln(wrapText(message,
        indent: indent,
        hangingIndent: hangingIndent,
        shouldWrap: wrap ?? _outputPreferences.wrapText,
        columnWidth: _outputPreferences.wrapColumn,
      ));
464
    } else {
465 466 467 468 469 470
      _status.write(wrapText(message,
        indent: indent,
        hangingIndent: hangingIndent,
        shouldWrap: wrap ?? _outputPreferences.wrapText,
        columnWidth: _outputPreferences.wrapColumn,
      ));
471
    }
472
  }
473 474

  @override
475
  void printTrace(String message) => _trace.writeln(message);
Devon Carew's avatar
Devon Carew committed
476

477
  @override
478 479
  Status startProgress(
    String message, {
480
    @required Duration timeout,
481
    String progressId,
482 483
    bool multilineOutput = false,
    int progressIndicatorPadding = kDefaultStatusPadding,
484
  }) {
485
    assert(progressIndicatorPadding != null);
486
    printStatus(message);
487 488 489
    return SilentStatus(
      timeout: timeout,
      timeoutConfiguration: _timeoutConfiguration,
490
      stopwatch: _stopwatchFactory.createStopwatch(),
491
    )..start();
492
  }
493

494
  @override
495 496 497 498
  void clear() {
    _error.clear();
    _status.clear();
    _trace.clear();
499
    _events.clear();
500
  }
501 502

  @override
503 504 505 506 507 508
  void sendEvent(String name, [Map<String, dynamic> args]) {
    _events.write(json.encode(<String, Object>{
      'name': name,
      'args': args
    }));
  }
Devon Carew's avatar
Devon Carew committed
509 510
}

511
class VerboseLogger extends Logger {
512 513 514 515
  VerboseLogger(this.parent,  {
    StopwatchFactory stopwatchFactory = const StopwatchFactory()
  }) : _stopwatch = stopwatchFactory.createStopwatch(),
       _stopwatchFactory = stopwatchFactory {
516
    _stopwatch.start();
517
  }
Devon Carew's avatar
Devon Carew committed
518

519 520
  final Logger parent;

521 522 523
  final Stopwatch _stopwatch;

  @override
524
  Terminal get _terminal => parent._terminal;
525 526 527 528 529 530

  @override
  OutputPreferences get _outputPreferences => parent._outputPreferences;

  @override
  TimeoutConfiguration get _timeoutConfiguration => parent._timeoutConfiguration;
531

532 533
  final StopwatchFactory _stopwatchFactory;

534
  @override
Devon Carew's avatar
Devon Carew committed
535 536
  bool get isVerbose => true;

537
  @override
538 539 540 541 542
  void printError(
    String message, {
    StackTrace stackTrace,
    bool emphasis,
    TerminalColor color,
543 544
    int indent,
    int hangingIndent,
545
    bool wrap,
546
  }) {
547 548
    _emit(
      _LogType.error,
549 550 551 552 553 554
      wrapText(message,
        indent: indent,
        hangingIndent: hangingIndent,
        shouldWrap: wrap ?? _outputPreferences.wrapText,
        columnWidth: _outputPreferences.wrapColumn,
      ),
555 556
      stackTrace,
    );
Devon Carew's avatar
Devon Carew committed
557 558
  }

559
  @override
560
  void printStatus(
561 562 563 564 565
    String message, {
    bool emphasis,
    TerminalColor color,
    bool newline,
    int indent,
566
    int hangingIndent,
567
    bool wrap,
568
  }) {
569 570 571 572 573 574
    _emit(_LogType.status, wrapText(message,
      indent: indent,
      hangingIndent: hangingIndent,
      shouldWrap: wrap ?? _outputPreferences.wrapText,
      columnWidth: _outputPreferences.wrapColumn,
    ));
Devon Carew's avatar
Devon Carew committed
575 576
  }

577
  @override
Devon Carew's avatar
Devon Carew committed
578
  void printTrace(String message) {
579
    _emit(_LogType.trace, message);
Devon Carew's avatar
Devon Carew committed
580 581
  }

582
  @override
583 584
  Status startProgress(
    String message, {
585
    @required Duration timeout,
586
    String progressId,
587 588
    bool multilineOutput = false,
    int progressIndicatorPadding = kDefaultStatusPadding,
589
  }) {
590
    assert(progressIndicatorPadding != null);
591
    printStatus(message);
592
    final Stopwatch timer = _stopwatchFactory.createStopwatch()..start();
593 594
    return SilentStatus(
      timeout: timeout,
595
      timeoutConfiguration: _timeoutConfiguration,
596 597
      // This is intentionally a different stopwatch than above.
      stopwatch: _stopwatchFactory.createStopwatch(),
598 599
      onFinish: () {
        String time;
600
        if (timeout == null || timeout > _timeoutConfiguration.fastOperation) {
601 602 603 604 605 606 607 608 609 610 611
          time = getElapsedAsSeconds(timer.elapsed);
        } else {
          time = getElapsedAsMilliseconds(timer.elapsed);
        }
        if (timeout != null && timer.elapsed > timeout) {
          printTrace('$message (completed in $time, longer than expected)');
        } else {
          printTrace('$message (completed in $time)');
        }
      },
    )..start();
612 613
  }

614
  void _emit(_LogType type, String message, [ StackTrace stackTrace ]) {
615
    if (message.trim().isEmpty) {
616
      return;
617
    }
Devon Carew's avatar
Devon Carew committed
618

619 620
    final int millis = _stopwatch.elapsedMilliseconds;
    _stopwatch.reset();
Devon Carew's avatar
Devon Carew committed
621

622
    String prefix;
623
    const int prefixWidth = 8;
624 625 626 627
    if (millis == 0) {
      prefix = ''.padLeft(prefixWidth);
    } else {
      prefix = '+$millis ms'.padLeft(prefixWidth);
628
      if (millis >= 100) {
629
        prefix = _terminal.bolden(prefix);
630
      }
631 632
    }
    prefix = '[$prefix] ';
Devon Carew's avatar
Devon Carew committed
633

634 635
    final String indent = ''.padLeft(prefix.length);
    final String indentMessage = message.replaceAll('\n', '\n$indent');
Devon Carew's avatar
Devon Carew committed
636 637

    if (type == _LogType.error) {
638
      parent.printError(prefix + _terminal.bolden(indentMessage));
639
      if (stackTrace != null) {
640
        parent.printError(indent + stackTrace.toString().replaceAll('\n', '\n$indent'));
641
      }
Devon Carew's avatar
Devon Carew committed
642
    } else if (type == _LogType.status) {
643
      parent.printStatus(prefix + _terminal.bolden(indentMessage));
Devon Carew's avatar
Devon Carew committed
644
    } else {
645
      parent.printStatus(prefix + indentMessage);
Devon Carew's avatar
Devon Carew committed
646 647
    }
  }
648 649

  @override
650
  void sendEvent(String name, [Map<String, dynamic> args]) { }
651 652 653 654 655 656

  @override
  bool get supportsColor => parent.supportsColor;

  @override
  bool get hasTerminal => parent.hasTerminal;
657 658 659

  @override
  void clear() => parent.clear();
Devon Carew's avatar
Devon Carew committed
660 661
}

662
enum _LogType { error, status, trace }
663

664 665
typedef SlowWarningCallback = String Function();

666 667 668
/// A [Status] class begins when start is called, and may produce progress
/// information asynchronously.
///
669 670 671 672
/// Some subclasses change output once [timeout] has expired, to indicate that
/// something is taking longer than expected.
///
/// The [SilentStatus] class never has any output.
673 674 675 676 677 678 679 680
///
/// The [AnsiSpinner] subclass shows a spinner, and replaces it with a single
/// space character when stopped or canceled.
///
/// The [AnsiStatus] subclass shows a spinner, and replaces it with timing
/// information when stopped. When canceled, the information isn't shown. In
/// either case, a newline is printed.
///
681 682 683
/// The [SummaryStatus] subclass shows only a static message (without an
/// indicator), then updates it when the operation ends.
///
684 685
/// Generally, consider `logger.startProgress` instead of directly creating
/// a [Status] or one of its subclasses.
686
abstract class Status {
687 688 689 690
  Status({
    @required this.timeout,
    @required TimeoutConfiguration timeoutConfiguration,
    this.onFinish,
691 692 693
    @required Stopwatch stopwatch,
  }) : _timeoutConfiguration = timeoutConfiguration,
       _stopwatch = stopwatch;
694

695
  /// A [SilentStatus] or an [AnsiSpinner] (depending on whether the
696
  /// terminal is fancy enough), already started.
697 698
  factory Status.withSpinner({
    @required Duration timeout,
699
    @required TimeoutConfiguration timeoutConfiguration,
700
    @required Stopwatch stopwatch,
701
    @required Terminal terminal,
702 703 704
    VoidCallback onFinish,
    SlowWarningCallback slowWarningCallback,
  }) {
705
    if (terminal.supportsColor) {
706 707 708 709
      return AnsiSpinner(
        timeout: timeout,
        onFinish: onFinish,
        slowWarningCallback: slowWarningCallback,
710
        timeoutConfiguration: timeoutConfiguration,
711
        stopwatch: stopwatch,
712
        terminal: terminal,
713 714
      )..start();
    }
715 716 717 718
    return SilentStatus(
      timeout: timeout,
      onFinish: onFinish,
      timeoutConfiguration: timeoutConfiguration,
719
      stopwatch: stopwatch,
720
    )..start();
721 722
  }

723
  final Duration timeout;
724
  final VoidCallback onFinish;
725
  final TimeoutConfiguration _timeoutConfiguration;
726

727
  @protected
728
  final Stopwatch _stopwatch;
729 730

  @protected
731
  @visibleForTesting
732 733 734 735
  bool get seemsSlow => timeout != null && _stopwatch.elapsed > timeout;

  @protected
  String get elapsedTime {
736
    if (timeout == null || timeout > _timeoutConfiguration.fastOperation) {
737
      return getElapsedAsSeconds(_stopwatch.elapsed);
738
    }
739 740
    return getElapsedAsMilliseconds(_stopwatch.elapsed);
  }
741

742
  /// Call to start spinning.
743
  void start() {
744 745
    assert(!_stopwatch.isRunning);
    _stopwatch.start();
746 747
  }

748
  /// Call to stop spinning after success.
749
  void stop() {
750
    finish();
751 752
  }

753
  /// Call to cancel the spinner after failure or cancellation.
754
  void cancel() {
755 756 757 758 759 760 761 762 763 764 765 766 767
    finish();
  }

  /// Call to clear the current line but not end the progress.
  void pause() { }

  /// Call to resume after a pause.
  void resume() { }

  @protected
  void finish() {
    assert(_stopwatch.isRunning);
    _stopwatch.stop();
768
    if (onFinish != null) {
769
      onFinish();
770
    }
771 772 773
  }
}

774 775 776 777
/// A [SilentStatus] shows nothing.
class SilentStatus extends Status {
  SilentStatus({
    @required Duration timeout,
778
    @required TimeoutConfiguration timeoutConfiguration,
779
    @required Stopwatch stopwatch,
780
    VoidCallback onFinish,
781 782 783 784
  }) : super(
    timeout: timeout,
    onFinish: onFinish,
    timeoutConfiguration: timeoutConfiguration,
785
    stopwatch: stopwatch,
786
  );
787 788 789 790 791 792 793

  @override
  void finish() {
    if (onFinish != null) {
      onFinish();
    }
  }
794 795 796 797 798 799 800 801
}

/// Constructor writes [message] to [stdout].  On [cancel] or [stop], will call
/// [onFinish]. On [stop], will additionally print out summary information.
class SummaryStatus extends Status {
  SummaryStatus({
    this.message = '',
    @required Duration timeout,
802 803
    @required TimeoutConfiguration timeoutConfiguration,
    @required Stopwatch stopwatch,
804 805
    this.padding = kDefaultStatusPadding,
    VoidCallback onFinish,
806
    Stdio stdio,
807 808
  }) : assert(message != null),
       assert(padding != null),
809
       _stdio = stdio ?? globals.stdio,
810 811 812 813 814 815
       super(
         timeout: timeout,
         onFinish: onFinish,
         timeoutConfiguration: timeoutConfiguration,
         stopwatch: stopwatch,
        );
816 817 818

  final String message;
  final int padding;
819
  final Stdio _stdio;
820 821 822 823 824 825 826 827 828

  bool _messageShowingOnCurrentLine = false;

  @override
  void start() {
    _printMessage();
    super.start();
  }

829
  void _writeToStdOut(String message) => _stdio.stdoutWrite(message);
830

831 832
  void _printMessage() {
    assert(!_messageShowingOnCurrentLine);
833
    _writeToStdOut('${message.padRight(padding)}     ');
834 835 836 837 838
    _messageShowingOnCurrentLine = true;
  }

  @override
  void stop() {
839
    if (!_messageShowingOnCurrentLine) {
840
      _printMessage();
841
    }
842 843
    super.stop();
    writeSummaryInformation();
844
    _writeToStdOut('\n');
845 846 847 848 849
  }

  @override
  void cancel() {
    super.cancel();
850
    if (_messageShowingOnCurrentLine) {
851
      _writeToStdOut('\n');
852
    }
853 854 855 856 857 858 859 860 861 862 863
  }

  /// Prints a (minimum) 8 character padded time.
  ///
  /// If [timeout] is less than or equal to [kFastOperation], the time is in
  /// seconds; otherwise, milliseconds. If the time is longer than [timeout],
  /// appends "(!)" to the time.
  ///
  /// Examples: `    0.5s`, `   150ms`, ` 1,600ms`, `    3.1s (!)`
  void writeSummaryInformation() {
    assert(_messageShowingOnCurrentLine);
864
    _writeToStdOut(elapsedTime.padLeft(_kTimePadding));
865
    if (seemsSlow) {
866
      _writeToStdOut(' (!)');
867
    }
868 869 870 871 872
  }

  @override
  void pause() {
    super.pause();
873
    _writeToStdOut('\n');
874 875 876 877
    _messageShowingOnCurrentLine = false;
  }
}

878
/// An [AnsiSpinner] is a simple animation that does nothing but implement a
879 880 881 882
/// terminal spinner. When stopped or canceled, the animation erases itself.
///
/// If the timeout expires, a customizable warning is shown (but the spinner
/// continues otherwise unabated).
883
class AnsiSpinner extends Status {
884 885
  AnsiSpinner({
    @required Duration timeout,
886 887
    @required TimeoutConfiguration timeoutConfiguration,
    @required Stopwatch stopwatch,
888
    @required Terminal terminal,
889 890
    VoidCallback onFinish,
    this.slowWarningCallback,
891 892
    Stdio stdio,
  }) : _stdio = stdio ?? globals.stdio,
893
       _terminal = terminal,
894 895 896 897 898 899
       super(
         timeout: timeout,
         onFinish: onFinish,
         timeoutConfiguration: timeoutConfiguration,
         stopwatch: stopwatch,
        );
900

901 902
  final String _backspaceChar = '\b';
  final String _clearChar = ' ';
903
  final Stdio _stdio;
904
  final Terminal _terminal;
905

906 907
  bool timedOut = false;

908 909 910
  int ticks = 0;
  Timer timer;

911
  // Windows console font has a limited set of Unicode characters.
912
  List<String> get _animation => !_terminal.supportsEmoji
913 914
      ? const <String>[r'-', r'\', r'|', r'/']
      : const <String>['⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷'];
915

916 917
  static const String _defaultSlowWarning = '(This is taking an unexpectedly long time.)';
  final SlowWarningCallback slowWarningCallback;
918

919 920 921 922
  String _slowWarning = '';

  String get _currentAnimationFrame => _animation[ticks % _animation.length];
  int get _currentLength => _currentAnimationFrame.length + _slowWarning.length;
923 924
  String get _backspace => _backspaceChar * (spinnerIndent + _currentLength);
  String get _clear => _clearChar *  (spinnerIndent + _currentLength);
925 926 927

  @protected
  int get spinnerIndent => 0;
928 929 930 931

  @override
  void start() {
    super.start();
932
    assert(timer == null);
933 934 935
    _startSpinner();
  }

936
  void _writeToStdOut(String message) => _stdio.stdoutWrite(message);
937

938
  void _startSpinner() {
939
    _writeToStdOut(_clear); // for _callback to backspace over
940
    timer = Timer.periodic(const Duration(milliseconds: 100), _callback);
941 942 943
    _callback(timer);
  }

944 945 946 947
  void _callback(Timer timer) {
    assert(this.timer == timer);
    assert(timer != null);
    assert(timer.isActive);
948
    _writeToStdOut(_backspace);
949 950
    ticks += 1;
    if (seemsSlow) {
951 952
      if (!timedOut) {
        timedOut = true;
953
        _writeToStdOut('$_clear\n');
954
      }
955
      if (slowWarningCallback != null) {
956
        _slowWarning = slowWarningCallback();
957
      } else {
958
        _slowWarning = _defaultSlowWarning;
959
      }
960
      _writeToStdOut(_slowWarning);
961
    }
962
    _writeToStdOut('${_clearChar * spinnerIndent}$_currentAnimationFrame');
963 964
  }

965
  @override
966 967
  void finish() {
    assert(timer != null);
968 969
    assert(timer.isActive);
    timer.cancel();
970 971 972 973 974 975
    timer = null;
    _clearSpinner();
    super.finish();
  }

  void _clearSpinner() {
976
    _writeToStdOut('$_backspace$_clear$_backspace');
977 978
  }

979
  @override
980 981
  void pause() {
    assert(timer != null);
982
    assert(timer.isActive);
983
    _clearSpinner();
984
    timer.cancel();
985 986 987 988 989 990 991
  }

  @override
  void resume() {
    assert(timer != null);
    assert(!timer.isActive);
    _startSpinner();
992 993 994
  }
}

995 996 997 998 999 1000 1001 1002
const int _kTimePadding = 8; // should fit "99,999ms"

/// Constructor writes [message] to [stdout] with padding, then starts an
/// indeterminate progress indicator animation (it's a subclass of
/// [AnsiSpinner]).
///
/// On [cancel] or [stop], will call [onFinish]. On [stop], will
/// additionally print out summary information.
1003
class AnsiStatus extends AnsiSpinner {
1004
  AnsiStatus({
1005 1006 1007
    this.message = '',
    this.multilineOutput = false,
    this.padding = kDefaultStatusPadding,
1008 1009
    @required Duration timeout,
    @required Stopwatch stopwatch,
1010
    @required Terminal terminal,
1011
    VoidCallback onFinish,
1012 1013
    Stdio stdio,
    TimeoutConfiguration timeoutConfiguration,
1014 1015 1016
  }) : assert(message != null),
       assert(multilineOutput != null),
       assert(padding != null),
1017 1018 1019 1020 1021 1022
       super(
         timeout: timeout,
         onFinish: onFinish,
         stdio: stdio,
         timeoutConfiguration: timeoutConfiguration,
         stopwatch: stopwatch,
1023
         terminal: terminal,
1024
        );
1025

1026
  final String message;
1027
  final bool multilineOutput;
1028 1029
  final int padding;

1030 1031
  static const String _margin = '     ';

1032 1033 1034 1035 1036
  @override
  int get spinnerIndent => _kTimePadding - 1;

  int _totalMessageLength;

1037 1038
  @override
  void start() {
1039
    _startStatus();
1040
    super.start();
1041
  }
1042

1043 1044 1045
  void _startStatus() {
    final String line = '${message.padRight(padding)}$_margin';
    _totalMessageLength = line.length;
1046
    _writeToStdOut(line);
1047 1048
  }

1049
  @override
1050
  void stop() {
1051 1052
    super.stop();
    writeSummaryInformation();
1053
    _writeToStdOut('\n');
1054
  }
1055

1056
  @override
1057 1058
  void cancel() {
    super.cancel();
1059
    _writeToStdOut('\n');
1060 1061
  }

1062 1063
  /// Print summary information when a task is done.
  ///
1064
  /// If [multilineOutput] is false, replaces the spinner with the summary message.
1065
  ///
1066
  /// If [multilineOutput] is true, then it prints the message again on a new
1067
  /// line before writing the elapsed time.
1068
  void writeSummaryInformation() {
1069
    if (multilineOutput) {
1070
      _writeToStdOut('\n${'$message Done'.padRight(padding)}$_margin');
1071
    }
1072
    _writeToStdOut(elapsedTime.padLeft(_kTimePadding));
1073
    if (seemsSlow) {
1074
      _writeToStdOut(' (!)');
1075
    }
1076
  }
1077

1078
  void _clearStatus() {
1079 1080 1081 1082 1083
    _writeToStdOut(
      '${_backspaceChar * _totalMessageLength}'
      '${_clearChar * _totalMessageLength}'
      '${_backspaceChar * _totalMessageLength}',
    );
1084 1085 1086
  }

  @override
1087 1088 1089
  void pause() {
    super.pause();
    _clearStatus();
1090 1091 1092
  }

  @override
1093 1094 1095
  void resume() {
    _startStatus();
    super.resume();
1096 1097
  }
}