Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mason_logger): only animate progress on terminals (#582)
- Loading branch information
Showing
8 changed files
with
366 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:async'; | ||
import 'dart:io' as io; | ||
|
||
const _asyncRunZoned = runZoned; | ||
|
||
/// This class facilitates overriding [io.stdioType]. | ||
abstract class StdioOverrides { | ||
static final _token = Object(); | ||
|
||
/// Returns the current [StdioOverrides] instance. | ||
/// | ||
/// This will return `null` if the current [Zone] does not contain | ||
/// any [StdioOverrides]. | ||
/// | ||
/// See also: | ||
/// * [StdioOverrides.runZoned] to provide [StdioOverrides] | ||
/// in a fresh [Zone]. | ||
/// | ||
static StdioOverrides? get current { | ||
return Zone.current[_token] as StdioOverrides?; | ||
} | ||
|
||
/// Runs [body] in a fresh [Zone] using the provided overrides. | ||
static R runZoned<R>( | ||
R Function() body, { | ||
io.StdioType Function(dynamic object) Function()? stdioType, | ||
}) { | ||
final overrides = _StdioOverridesScope(stdioType); | ||
return _asyncRunZoned( | ||
body, | ||
zoneValues: {_token: overrides}, | ||
); | ||
} | ||
|
||
/// The [io.stdioType] that will be used for errors within the current [Zone]. | ||
io.StdioType Function(dynamic object) get stdioType => io.stdioType; | ||
} | ||
|
||
class _StdioOverridesScope extends StdioOverrides { | ||
_StdioOverridesScope(this._stdioType); | ||
|
||
final StdioOverrides? _previous = StdioOverrides.current; | ||
final io.StdioType Function(dynamic object) Function()? _stdioType; | ||
|
||
@override | ||
io.StdioType Function(dynamic object) get stdioType { | ||
return _stdioType?.call() ?? _previous?.stdioType ?? super.stdioType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:mason_logger/mason_logger.dart'; | ||
|
||
Future<void> main() async { | ||
final logger = Logger(); | ||
final progress = logger.progress('Calculating'); | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
progress.update('This is taking longer than expected'); | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
progress.update('Almost done'); | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
progress.complete('Done'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
⠋ Calculating...[2K⠙ This is taking longer than expected... (1.0s)[2K⠹ Almost done... (2.0s)[2K✓ Done (3.0s) | ||
|
Oops, something went wrong.