Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mason_logger): add ProgressOptions API #478

Merged
merged 13 commits into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/mason_logger/lib/src/mason_logger.dart
Expand Up @@ -53,7 +53,17 @@ class Logger {
void delayed(String? message) => _queue.add(message);

/// Writes progress message to stdout.
Progress progress(String message) => Progress._(message, _stdout, level);
/// Provide a animation list via [progressAnimation].
Progress progress(
String message, {
List<String>? progressAnimation,
felangel marked this conversation as resolved.
Show resolved Hide resolved
}) =>
Progress._(
message,
_stdout,
level,
progressAnimation,
);

/// Writes error message to stderr.
void err(String? message) {
Expand Down
8 changes: 6 additions & 2 deletions packages/mason_logger/lib/src/progress.dart
Expand Up @@ -9,14 +9,16 @@ class Progress {
this._message,
this._stdout,
this._level,
) : _stopwatch = Stopwatch() {
List<String>? progressAnimation,
) : _stopwatch = Stopwatch(),
_progressAnimation = progressAnimation ?? _defaultProgressAnimation {
_stopwatch
..reset()
..start();
_timer = Timer.periodic(const Duration(milliseconds: 80), _onTick);
}

static const List<String> _progressAnimation = [
static const List<String> _defaultProgressAnimation = [
'⠋',
'⠙',
'⠹',
Expand All @@ -29,6 +31,8 @@ class Progress {
'⠏'
];

final List<String> _progressAnimation;

final io.Stdout _stdout;

final Level _level;
Expand Down
43 changes: 43 additions & 0 deletions packages/mason_logger/test/src/mason_logger_test.dart
Expand Up @@ -506,6 +506,49 @@ void main() {
stdin: () => stdin,
);
});

test('writes custom progress animation to stdout', () async {
await IOOverrides.runZoned(
() async {
const time = '(0.Xs)';
const message = 'test message';
final done = Logger().progress(
message,
progressAnimation: [
'a',
'b',
'c',
],
);
await Future<void>.delayed(const Duration(milliseconds: 400));
done.complete();
verifyInOrder([
() {
stdout.write(
'''${lightGreen.wrap('\b${'\b' * (message.length + 4 + time.length)}a')} $message... ${darkGray.wrap('(0.1s)')}''',
);
},
() {
stdout.write(
'''${lightGreen.wrap('\b${'\b' * (message.length + 4 + time.length)}b')} $message... ${darkGray.wrap('(0.2s)')}''',
);
},
() {
stdout.write(
'''${lightGreen.wrap('\b${'\b' * (message.length + 4 + time.length)}c')} $message... ${darkGray.wrap('(0.3s)')}''',
);
},
() {
stdout.write(
'''\b${'\b' * (message.length + 4 + time.length)}\u001b[2K${lightGreen.wrap('✓')} $message ${darkGray.wrap('(0.4s)')}\n''',
);
},
]);
},
stdout: () => stdout,
stdin: () => stdin,
);
});
});

group('.chooseAny', () {
Expand Down