improve the logger ui a bit for elapsed times (#7960)
* improve the logger ui a bit for elapsed times * invert param * update daemon.dart
This commit is contained in:
@@ -9,6 +9,7 @@ import 'package:stack_trace/stack_trace.dart';
|
||||
|
||||
import 'io.dart';
|
||||
import 'platform.dart';
|
||||
import 'utils.dart';
|
||||
|
||||
final AnsiTerminal terminal = new AnsiTerminal();
|
||||
|
||||
@@ -38,11 +39,11 @@ abstract class Logger {
|
||||
///
|
||||
/// [message] is the message to display to the user; [progressId] provides an ID which can be
|
||||
/// used to identify this type of progress (`hot.reload`, `hot.restart`, ...).
|
||||
Status startProgress(String message, { String progressId });
|
||||
Status startProgress(String message, { String progressId, bool expectSlowOperation: false });
|
||||
}
|
||||
|
||||
class Status {
|
||||
void stop({ bool showElapsedTime: true }) { }
|
||||
void stop() { }
|
||||
void cancel() { }
|
||||
}
|
||||
|
||||
@@ -81,13 +82,13 @@ class StdoutLogger extends Logger {
|
||||
void printTrace(String message) { }
|
||||
|
||||
@override
|
||||
Status startProgress(String message, { String progressId }) {
|
||||
Status startProgress(String message, { String progressId, bool expectSlowOperation: false }) {
|
||||
if (_status != null) {
|
||||
// Ignore nested progresses; return a no-op status object.
|
||||
return new Status();
|
||||
} else {
|
||||
if (supportsColor) {
|
||||
_status = new _AnsiStatus(message, () { _status = null; });
|
||||
_status = new _AnsiStatus(message, expectSlowOperation, () { _status = null; });
|
||||
return _status;
|
||||
} else {
|
||||
printStatus(message);
|
||||
@@ -124,7 +125,7 @@ class BufferLogger extends Logger {
|
||||
void printTrace(String message) => _trace.writeln(message);
|
||||
|
||||
@override
|
||||
Status startProgress(String message, { String progressId }) {
|
||||
Status startProgress(String message, { String progressId, bool expectSlowOperation: false }) {
|
||||
printStatus(message);
|
||||
return new Status();
|
||||
}
|
||||
@@ -156,7 +157,7 @@ class VerboseLogger extends Logger {
|
||||
}
|
||||
|
||||
@override
|
||||
Status startProgress(String message, { String progressId }) {
|
||||
Status startProgress(String message, { String progressId, bool expectSlowOperation: false }) {
|
||||
printStatus(message);
|
||||
return new Status();
|
||||
}
|
||||
@@ -249,10 +250,10 @@ class AnsiTerminal {
|
||||
}
|
||||
|
||||
class _AnsiStatus extends Status {
|
||||
_AnsiStatus(this.message, this.onFinish) {
|
||||
_AnsiStatus(this.message, this.expectSlowOperation, this.onFinish) {
|
||||
stopwatch = new Stopwatch()..start();
|
||||
|
||||
stdout.write('${message.padRight(51)} ');
|
||||
stdout.write('${message.padRight(52)} ');
|
||||
stdout.write('${_progress[0]}');
|
||||
|
||||
timer = new Timer.periodic(new Duration(milliseconds: 100), _callback);
|
||||
@@ -261,6 +262,7 @@ class _AnsiStatus extends Status {
|
||||
static final List<String> _progress = <String>['-', r'\', '|', r'/', '-', r'\', '|', '/'];
|
||||
|
||||
final String message;
|
||||
final bool expectSlowOperation;
|
||||
final _FinishCallback onFinish;
|
||||
Stopwatch stopwatch;
|
||||
Timer timer;
|
||||
@@ -273,17 +275,18 @@ class _AnsiStatus extends Status {
|
||||
}
|
||||
|
||||
@override
|
||||
void stop({ bool showElapsedTime: true }) {
|
||||
void stop() {
|
||||
onFinish();
|
||||
|
||||
if (!live)
|
||||
return;
|
||||
live = false;
|
||||
|
||||
if (showElapsedTime) {
|
||||
print('\b\b\b\b\b${stopwatch.elapsedMilliseconds.toString().padLeft(3)}ms');
|
||||
if (expectSlowOperation) {
|
||||
double seconds = stopwatch.elapsedMilliseconds / Duration.MILLISECONDS_PER_SECOND;
|
||||
print('\b\b\b\b\b${seconds.toStringAsFixed(1).padLeft(4)}s');
|
||||
} else {
|
||||
print('\b ');
|
||||
print('\b\b\b\b\b${printWithSeparators(stopwatch.elapsedMilliseconds).toString().padLeft(3)}ms');
|
||||
}
|
||||
|
||||
timer.cancel();
|
||||
|
||||
@@ -54,6 +54,19 @@ String toTitleCase(String str) {
|
||||
/// Return the plural of the given word (`cat(s)`).
|
||||
String pluralize(String word, int count) => count == 1 ? word : word + 's';
|
||||
|
||||
/// Return the value printed with commas every 3 digits.
|
||||
String printWithSeparators(int value) {
|
||||
String str = '$value';
|
||||
|
||||
int index = 3;
|
||||
while (index < str.length) {
|
||||
str = str.substring(0, str.length - index) + ',' + str.substring(str.length - index);
|
||||
index += 4;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/// Return the name of an enum item.
|
||||
String getEnumName(dynamic enumItem) {
|
||||
String name = '$enumItem';
|
||||
|
||||
Reference in New Issue
Block a user