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 interval to ProgressAnimation #1391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/mason_logger/lib/src/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class ProgressOptions {
/// {@endtemplate}
class ProgressAnimation {
/// {@macro progress_animation}
const ProgressAnimation({this.frames = _defaultFrames});
const ProgressAnimation({
this.frames = _defaultFrames,
this.interval = _defaultInterval,
});

static const _defaultInterval = Duration(milliseconds: 80);

static const _defaultFrames = [
'⠋',
Expand All @@ -41,6 +46,11 @@ class ProgressAnimation {

/// The list of animation frames.
final List<String> frames;

/// The interval at which new frames are produced.
/// In other words, the amount of time spent showing a single frame.
/// Defaults to 80ms per frame.
final Duration interval;
}

/// {@template progress}
Expand Down Expand Up @@ -69,7 +79,7 @@ class Progress {
return;
}

_timer = Timer.periodic(const Duration(milliseconds: 80), _onTick);
_timer = Timer.periodic(options.animation.interval, _onTick);
}

static const _padding = 15;
Expand Down
31 changes: 31 additions & 0 deletions packages/mason_logger/test/src/progress_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,37 @@ void main() {
);
});

test('writes custom progress interval to stdout', () async {
await _runZoned(
() async {
const time = '(0.Xs)';
const message = 'test message';
const progressOptions = ProgressOptions(
animation: ProgressAnimation(interval: Duration(milliseconds: 400)),
);
final done = Logger().progress(message, options: progressOptions);
await Future<void>.delayed(const Duration(milliseconds: 400));
done.complete();
verifyInOrder([
() {
stdout.write(
'''${lightGreen.wrap('\b${'\b' * (message.length + 4 + time.length)}⠋')} $message... ${darkGray.wrap('(0.1s)')}''',
);
},
() {
stdout.write(
'''\b${'\b' * (message.length + 4 + time.length)}\u001b[2K${lightGreen.wrap('✓')} $message ${darkGray.wrap('(0.4s)')}\n''',
);
},
]);
verifyNever(() => stdout.write(any(that: contains('0.2s'))));
verifyNever(() => stdout.write(any(that: contains('0.3s'))));
},
stdout: () => stdout,
stdin: () => stdin,
);
});

test('supports empty list of animation frames', () async {
await _runZoned(
() async {
Expand Down
Loading