diff --git a/.github/workflows/mason_logger.yaml b/.github/workflows/mason_logger.yaml index e6accde4d..83a3e33bd 100644 --- a/.github/workflows/mason_logger.yaml +++ b/.github/workflows/mason_logger.yaml @@ -36,6 +36,9 @@ jobs: - name: Analyze run: dart analyze --fatal-infos --fatal-warnings . + - name: Verify CI + run: dart example/test.dart + - name: Run Tests run: | dart pub global activate coverage 1.2.0 diff --git a/packages/mason_logger/example/test.dart b/packages/mason_logger/example/test.dart new file mode 100644 index 000000000..1997dda56 --- /dev/null +++ b/packages/mason_logger/example/test.dart @@ -0,0 +1,10 @@ +import 'package:mason_logger/mason_logger.dart'; + +Future main() async { + final logger = Logger(); + final progress = logger.progress('Calculating'); + await Future.delayed(const Duration(seconds: 1)); + progress.update('Almost done'); + await Future.delayed(const Duration(seconds: 1)); + progress.complete('Done'); +} diff --git a/packages/mason_logger/lib/src/progress.dart b/packages/mason_logger/lib/src/progress.dart index fda929742..1b77af967 100644 --- a/packages/mason_logger/lib/src/progress.dart +++ b/packages/mason_logger/lib/src/progress.dart @@ -51,6 +51,17 @@ class Progress { _stopwatch ..reset() ..start(); + + // The animation is only shown when it would be meaningful. + // Do not animate if the stdio type is not a terminal. + if (io.stdioType(_stdout) != io.StdioType.terminal) { + final frames = _options.animation.frames; + final char = frames.isEmpty ? '' : frames.first; + final prefix = char.isEmpty ? char : '${lightGreen.wrap(char)} '; + _write('$prefix$_message...'); + return; + } + _timer = Timer.periodic(const Duration(milliseconds: 80), _onTick); } @@ -62,7 +73,7 @@ class Progress { final Stopwatch _stopwatch; - late final Timer _timer; + Timer? _timer; String _message; @@ -74,26 +85,26 @@ class Progress { _write( '''$_clearLine${lightGreen.wrap('✓')} ${update ?? _message} $_time\n''', ); - _timer.cancel(); + _timer?.cancel(); } /// End the progress and mark it as failed. void fail([String? update]) { - _timer.cancel(); + _timer?.cancel(); _write('$_clearLine${red.wrap('✗')} ${update ?? _message} $_time\n'); _stopwatch.stop(); } /// Update the progress message. void update(String update) { - _write(_clearLine); + if (_timer != null) _write(_clearLine); _message = update; _onTick(_timer); } /// Cancel the progress and remove the written line. void cancel() { - _timer.cancel(); + _timer?.cancel(); _write(_clearLine); _stopwatch.stop(); } @@ -103,7 +114,7 @@ class Progress { '\r'; // bring cursor to the start of the current line } - void _onTick(Timer _) { + void _onTick(Timer? _) { _index++; final frames = _options.animation.frames; final char = frames.isEmpty ? '' : frames[_index % frames.length];