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 trailing to ProgressOptions #1247

Merged
merged 2 commits into from
Feb 11, 2024
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
13 changes: 10 additions & 3 deletions packages/mason_logger/lib/src/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ part of 'mason_logger.dart';
/// {@endtemplate}
class ProgressOptions {
/// {@macro progress_options}
const ProgressOptions({this.animation = const ProgressAnimation()});
const ProgressOptions({
this.animation = const ProgressAnimation(),
this.trailing = '...',
});

/// The progress animation configuration.
final ProgressAnimation animation;

/// The trailing string following progress messages.
/// Defaults to "..."
final String trailing;
}

/// {@template progress_animation}
Expand Down Expand Up @@ -58,7 +65,7 @@ class Progress {
final frames = _options.animation.frames;
final char = frames.isEmpty ? '' : frames.first;
final prefix = char.isEmpty ? char : '${lightGreen.wrap(char)} ';
_write('$prefix$_message...');
_write('$prefix$_message${_options.trailing}');
return;
}

Expand Down Expand Up @@ -130,7 +137,7 @@ class Progress {
final char = frames.isEmpty ? '' : frames[_index % frames.length];
final prefix = char.isEmpty ? char : '${lightGreen.wrap(char)} ';

_write('$_clearLine$prefix$_message... $_time');
_write('$_clearLine$prefix$_message${_options.trailing} $_time');
}

void _write(String object) {
Expand Down
68 changes: 68 additions & 0 deletions packages/mason_logger/test/src/progress_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ void main() {
);
});

test(
'writes static message when stdioType is not terminal w/custom trailing',
() async {
const progressOptions = ProgressOptions(trailing: '!!!');
when(() => stdout.hasTerminal).thenReturn(false);
await _runZoned(
() async {
const message = 'test message';
final done = Logger(progressOptions: progressOptions).progress(
message,
);
await Future<void>.delayed(const Duration(milliseconds: 400));
done.complete();
verifyInOrder([
() => stdout.write('${lightGreen.wrap('⠋')} $message!!!'),
() {
stdout.write(
'''\u001b[2K\r${lightGreen.wrap('✓')} $message ${darkGray.wrap('(0.4s)')}\n''',
);
},
]);
},
stdout: () => stdout,
zoneValues: {AnsiCode: true},
);
});

test('writes custom progress animation to stdout', () async {
await _runZoned(
() async {
Expand Down Expand Up @@ -133,6 +160,47 @@ void main() {
);
});

test('writes custom progress animation to stdout w/custom trailing',
() async {
await _runZoned(
() async {
const time = '(0.Xs)';
const message = 'test message';
const progressOptions = ProgressOptions(
animation: ProgressAnimation(frames: ['+', 'x', '*']),
trailing: '!!!',
);
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(
'''${lightGreen.wrap('\b${'\b' * (message.length + 4 + time.length)}x')} $message!!! ${darkGray.wrap('(0.2s)')}''',
);
},
() {
stdout.write(
'''${lightGreen.wrap('\b${'\b' * (message.length + 4 + time.length)}*')} $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('.complete', () {
test('writes lines to stdout', () async {
await _runZoned(
Expand Down
Loading