Skip to content

Commit

Permalink
feat(mason_logger): add trailing to ProgressOptions (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Feb 11, 2024
1 parent d507bde commit 2726ef2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
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

0 comments on commit 2726ef2

Please sign in to comment.