Skip to content

Commit

Permalink
fix(mason_logger): improve clear line mechanism for Progress API (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
renancaraujo committed Oct 19, 2022
1 parent 8641f26 commit 68b98c7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
29 changes: 13 additions & 16 deletions packages/mason_logger/lib/src/progress.dart
Expand Up @@ -72,54 +72,51 @@ class Progress {
void complete([String? update]) {
_stopwatch.stop();
_write(
'''$_clearLn${lightGreen.wrap('✓')} ${update ?? _message} $_time\n''',
'''$_clearLine${lightGreen.wrap('✓')} ${update ?? _message} $_time\n''',
);
_timer.cancel();
}

/// End the progress and mark it as failed.
void fail([String? update]) {
_timer.cancel();
_write('$_clearLn${red.wrap('✗')} ${update ?? _message} $_time\n');
_write('$_clearLine${red.wrap('✗')} ${update ?? _message} $_time\n');
_stopwatch.stop();
}

/// Update the progress message.
void update(String update) {
_write(_clearLn);
_write(_clearLine);
_message = update;
_onTick(_timer);
}

/// Cancel the progress and remove the written line.
void cancel() {
_timer.cancel();
_write(_clearLn);
_write(_clearLine);
_stopwatch.stop();
}

String get _clearLine {
return '\u001b[2K' // clear current line
'\r'; // bring cursor to the start of the current line
}

void _onTick(Timer _) {
_index++;
final frames = _options.animation.frames;
final char = frames.isEmpty ? '' : frames[_index % frames.length];
final prefix = char.isEmpty
? _clearMessageLength
: '${lightGreen.wrap('$_clearMessageLength$char')} ';
_write('$prefix$_message... $_time');
final prefix = char.isEmpty ? char : '${lightGreen.wrap(char)} ';

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

void _write(Object? object) {
void _write(String object) {
if (_level.index > Level.info.index) return;
_stdout.write(object);
}

String get _clearMessageLength {
final length = _message.length + 4 + _time.length;
return '\b${'\b' * length}';
}

String get _clearLn => '$_clearMessageLength\u001b[2K';

String get _time {
final elapsedTime = _stopwatch.elapsed.inMilliseconds;
final displayInMilliseconds = elapsedTime < 100;
Expand Down
52 changes: 38 additions & 14 deletions packages/mason_logger/test/src/progress_test.dart
Expand Up @@ -122,7 +122,6 @@ void main() {
() async {
await IOOverrides.runZoned(
() async {
const time = '(0.1s)';
const message = 'test message';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
Expand All @@ -131,17 +130,20 @@ void main() {
() {
stdout.write(
any(
that: contains(
'''⠙ $message... ''',
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m test message... \[90m\(8\dms\)\[0m',
),
),
),
);
},
).called(1);

verify(
() {
stdout.write(
'''[2K[92m✓[0m $message [90m$time[0m\n''',
'''[2K\r[92m✓[0m test message [90m(0.1s)[0m\n''',
);
},
).called(1);
Expand Down Expand Up @@ -180,26 +182,35 @@ void main() {
() async {
const message = 'message';
const update = 'update';
const time = '(0.1s)';
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.update(update);
await Future<void>.delayed(const Duration(milliseconds: 100));

verify(
() {
stdout.write(
any(
that: contains(
'''⠙ $message... ''',
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m message... \[90m\(8\dms\)\[0m',
),
),
),
);
},
).called(1);

verify(
() {
stdout.write(
'''⠹ $update... $time''',
any(
that: matches(
RegExp(
r'\[2K\u000D\[92m⠹\[0m update... \[90m\(0\.1s\)\[0m',
),
),
),
);
},
).called(1);
Expand Down Expand Up @@ -243,21 +254,25 @@ void main() {
final progress = Logger().progress(message);
await Future<void>.delayed(const Duration(milliseconds: 100));
progress.fail();

verify(
() {
stdout.write(
any(
that: contains(
'''⠙ $message... ''',
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m test message... \[90m\(8\dms\)\[0m',
),
),
),
);
},
).called(1);

verify(
() {
stdout.write(
'''[2K[31m✗[0m $message [90m$time[0m\n''',
'''[2K\u000D[31m✗[0m $message [90m$time[0m\n''',
);
},
).called(1);
Expand Down Expand Up @@ -302,17 +317,26 @@ void main() {
() {
stdout.write(
any(
that: contains(
'''⠙ $message... ''',
that: matches(
RegExp(
r'\[2K\u000D\[92m⠙\[0m test message... \[90m\(8\dms\)\[0m',
),
),
),
);
},
).called(1);

verify(
() {
stdout.write(
'''''',
any(
that: matches(
RegExp(
r'\[2K\u000D',
),
),
),
);
},
).called(1);
Expand Down

0 comments on commit 68b98c7

Please sign in to comment.