Index: sdk/lib/_internal/pub/lib/src/progress.dart |
diff --git a/sdk/lib/_internal/pub/lib/src/progress.dart b/sdk/lib/_internal/pub/lib/src/progress.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..059d480ddec659ba6e32ef3fff758c630f4170c8 |
--- /dev/null |
+++ b/sdk/lib/_internal/pub/lib/src/progress.dart |
@@ -0,0 +1,86 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library pub.progress; |
+ |
+import 'dart:async'; |
+import 'dart:io'; |
+ |
+import 'log.dart' as log; |
+ |
+/// A live-updating progress indicator for long-running log entries. |
+class Progress { |
+ /// The timer used to write "..." during a progress log. |
+ Timer _timer; |
+ |
+ /// The [Stopwatch] used to track how long a progress log has been running. |
+ final _stopwatch = new Stopwatch(); |
+ |
+ /// The progress message as it's being incrementally appended. When the |
+ /// progress is done, a single entry will be added to the log for it. |
+ final String _message; |
+ |
+ Progress(this._message) { |
+ _stopwatch.start(); |
+ |
+ if (log.json.enabled) return; |
+ |
+ // Only animate if we're writing to a TTY in human format. |
+ if (stdioType(stdout) == StdioType.TERMINAL) { |
+ _update(); |
+ _timer = new Timer.periodic(new Duration(milliseconds: 100), (_) { |
+ _update(); |
+ }); |
+ } else { |
+ stdout.write("$_message... "); |
+ } |
+ } |
+ |
+ /// Stops the progress indicator. |
+ /// |
+ /// Returns the complete final progress message. |
+ String stop() { |
+ _stopwatch.stop(); |
+ |
+ // If we aren't animating, just log the final time. |
+ if (log.json.enabled) { |
+ // Do nothing. |
+ } else if (_timer == null) { |
+ stdout.writeln(_time); |
+ } else { |
+ _timer.cancel(); |
+ |
+ // Show one final update. |
+ _update(); |
+ stdout.writeln(); |
+ } |
+ |
+ return "$_message... ${_time}"; |
+ } |
+ |
+ /// Gets the current progress time as a parenthesized, formatted string. |
+ String get _time { |
+ var elapsed = _stopwatch.elapsed; |
+ var time = "("; |
+ |
+ // TODO(rnystrom): Move this somewhere reusable. |
+ if (elapsed.inMinutes > 0) { |
+ time += "${elapsed.inMinutes}:"; |
+ } |
+ |
+ var s = elapsed.inSeconds % 59; |
+ var ms = (elapsed.inMilliseconds % 1000) ~/ 100; |
+ return time + "$s.${ms}s)"; |
+ } |
+ |
+ /// Refreshes the progress line. |
+ void _update() { |
+ stdout.write("\r$_message... "); |
+ |
+ // Show the time only once it gets noticeably long. |
+ if (_stopwatch.elapsed.inSeconds > 0) { |
+ stdout.write(log.gray(_time)); |
+ } |
+ } |
+} |