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

fix(mason_logger): progress on android studio terminal #549

Merged
merged 3 commits into from Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
54 changes: 39 additions & 15 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(
'''✓ $message $time\n''',
'''\r✓ test message (0.1s)\n''',
);
},
).called(1);
Expand Down Expand Up @@ -173,33 +175,42 @@ void main() {
});

group('.update', () {
test('writes lines to stdout', () async {
test('writes lines to stdouta', () async {
felangel marked this conversation as resolved.
Show resolved Hide resolved
await runZoned(
() async {
await IOOverrides.runZoned(
() 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(
'''✗ $message $time\n''',
'''\u000D✗ $message $time\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