OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pub.progress; | 5 library pub.progress; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'log.dart' as log; | 10 import 'log.dart' as log; |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 | 12 |
13 /// A live-updating progress indicator for long-running log entries. | 13 /// A live-updating progress indicator for long-running log entries. |
14 class Progress { | 14 class Progress { |
15 /// The timer used to write "..." during a progress log. | 15 /// The timer used to write "..." during a progress log. |
16 Timer _timer; | 16 Timer _timer; |
17 | 17 |
18 /// The [Stopwatch] used to track how long a progress log has been running. | 18 /// The [Stopwatch] used to track how long a progress log has been running. |
19 final _stopwatch = new Stopwatch(); | 19 final _stopwatch = new Stopwatch(); |
20 | 20 |
21 /// The progress message as it's being incrementally appended. When the | 21 /// The progress message as it's being incrementally appended. When the |
22 /// progress is done, a single entry will be added to the log for it. | 22 /// progress is done, a single entry will be added to the log for it. |
23 final String _message; | 23 final String _message; |
24 | 24 |
25 Progress(this._message) { | 25 /// Whether this should log messages at [log.Level.FINE] rather than |
26 /// [log.Level.MESSAGE]. | |
27 final bool _fine; | |
28 | |
29 /// Gets the current progress time as a parenthesized, formatted string. | |
30 String get _time => "(${niceDuration(_stopwatch.elapsed)})"; | |
31 | |
32 Progress(this._message, {bool fine: false}) | |
33 : _fine = fine { | |
26 _stopwatch.start(); | 34 _stopwatch.start(); |
27 | 35 |
28 if (log.json.enabled) return; | 36 bool animate = !log.isLevelVisible(log.Level.FINE) && |
Bob Nystrom
2014/06/05 20:18:16
Document this.
nweiz
2014/06/05 21:58:19
Done.
nweiz
2014/06/05 21:58:19
Done.
| |
37 stdioType(stdout) == StdioType.TERMINAL; | |
38 (animate || _fine ? log.fine : log.message)("$_message..."); | |
29 | 39 |
30 // Only animate if we're writing to a TTY in human format. | 40 if (!animate || log.json.enabled) return; |
31 if (stdioType(stdout) == StdioType.TERMINAL) { | 41 |
42 _update(); | |
43 _timer = new Timer.periodic(new Duration(milliseconds: 100), (_) { | |
32 _update(); | 44 _update(); |
33 _timer = new Timer.periodic(new Duration(milliseconds: 100), (_) { | 45 }); |
34 _update(); | |
35 }); | |
36 } else { | |
37 stdout.write("$_message... "); | |
38 } | |
39 } | 46 } |
40 | 47 |
41 /// Stops the progress indicator. | 48 /// Stops the progress indicator. |
42 /// | 49 /// |
43 /// Returns the complete final progress message. | 50 /// Returns the complete final progress message. |
Bob Nystrom
2014/06/05 20:18:16
Delete this.
nweiz
2014/06/05 21:58:19
Done.
| |
44 String stop() { | 51 void stop() { |
45 _stopwatch.stop(); | 52 _stopwatch.stop(); |
46 | 53 |
47 // If we aren't animating, just log the final time. | 54 // If we aren't animating, just log the final time. This is always |
Bob Nystrom
2014/06/05 20:18:16
Won't this get logged even if we are animating?
nweiz
2014/06/05 21:58:19
Yes, it's a little confusingly written. I'll rewor
| |
48 if (log.json.enabled) { | 55 // [log.fine] because for the most part normal users don't care about the |
49 // Do nothing. | 56 // precise time information beyond what's shown in the animation. |
50 } else if (_timer == null) { | 57 log.fine("$_message finished $_time."); |
51 stdout.writeln(_time); | |
52 } else { | |
53 _timer.cancel(); | |
54 | 58 |
55 // Show one final update. | 59 if (_timer == null) return; |
56 _update(); | 60 _timer.cancel(); |
57 stdout.writeln(); | |
58 } | |
59 | 61 |
60 return "$_message... ${_time}"; | 62 // Show one final update. |
63 _update(); | |
64 stdout.writeln(); | |
61 } | 65 } |
62 | 66 |
63 /// Gets the current progress time as a parenthesized, formatted string. | 67 /// Stop animating the progress indicator. |
64 String get _time => "(${niceDuration(_stopwatch.elapsed)})"; | 68 /// |
69 /// This will continue running the stopwatch so that the full time can be | |
70 /// logged in [stop]. | |
71 void stopAnimating() { | |
72 if (_timer == null) return; | |
73 stdout.writeln("\r$_message..."); | |
Bob Nystrom
2014/06/05 20:18:16
What's this for?
nweiz
2014/06/05 21:58:19
It prevents there being an old, inaccurate time be
Bob Nystrom
2014/06/06 20:18:53
OK, document that.
It's confusing because the cod
nweiz
2014/06/09 19:55:34
Done.
| |
74 _timer.cancel(); | |
75 _timer = null; | |
76 } | |
65 | 77 |
66 /// Refreshes the progress line. | 78 /// Refreshes the progress line. |
67 void _update() { | 79 void _update() { |
68 stdout.write("\r$_message... "); | 80 stdout.write("\r$_message... "); |
69 | 81 |
70 // Show the time only once it gets noticeably long. | 82 // Show the time only once it gets noticeably long. |
71 if (_stopwatch.elapsed.inSeconds > 0) { | 83 if (_stopwatch.elapsed.inSeconds > 0) { |
72 stdout.write(log.gray(_time)); | 84 stdout.write(log.gray(_time)); |
73 } | 85 } |
74 } | 86 } |
75 } | 87 } |
OLD | NEW |