Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit78c0dd7

Browse files
jflan-dd0xTim
andauthored
Don't animate ActivityIndicator when outputting to a file (#202)
* Don't animate ActivityIndicator when outputting to a file* Tweak comment* Trigger CI---------Co-authored-by: Tim <0xtimc@gmail.com>
1 parent479a6af commit78c0dd7

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

‎Sources/ConsoleKitTerminal/Activity/ActivityBar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension ActivityBar {
2525
publicfunc outputActivityIndicator(to console:anyConsole, state:ActivityIndicatorState){
2626
letbar:ConsoleText
2727
switch state{
28-
case.ready: bar="[]"
28+
case.ready: bar="[...]"
2929
case.active(let tick): bar=renderActiveBar(tick: tick, width: console.activityBarWidth)
3030
case.success: bar="[Done]".consoleText(.success)
3131
case.failure: bar="[Failed]".consoleText(.error)

‎Sources/ConsoleKitTerminal/Activity/ActivityIndicator.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public final class ActivityIndicator<A>: Sendable where A: ActivityIndicatorType
4848

4949
/// The `Console` this `ActivityIndicator` is running on.
5050
privateletconsole:anyConsole
51-
52-
/// Current state.
53-
privateletstate:NIOLockedValueBox<ActivityIndicatorState>
5451

5552
/// The queue on which to handle timer events
5653
privateletqueue:DispatchQueue
@@ -74,10 +71,14 @@ public final class ActivityIndicator<A>: Sendable where A: ActivityIndicatorType
7471
/// Creates a new `ActivityIndicator`. Use `ActivityIndicatorType.newActivity(for:)`.
7572
init(activity:A, console:anyConsole, targetQueue:DispatchQueue?=nil){
7673
self.console= console
77-
self.state=NIOLockedValueBox(.ready)
7874
self._activity=NIOLockedValueBox(activity)
7975
self.queue=DispatchQueue(label:"codes.vapor.consolekit.activityindicator", target: targetQueue)
80-
self._timer=NIOLockedValueBox(DispatchSource.makeTimerSource(flags:[], queue:self.queue)as!DispatchSource)
76+
77+
lettimer=DispatchSource.makeTimerSource(flags:[], queue:self.queue)as!DispatchSource
78+
// Activate the timer in case the activity indicator is never started
79+
timer.activate()
80+
self._timer=NIOLockedValueBox(timer)
81+
8182
self.stopGroup=DispatchGroup()
8283
}
8384

@@ -90,6 +91,12 @@ public final class ActivityIndicator<A>: Sendable where A: ActivityIndicatorType
9091
/// - refreshRate: The time interval (specified in milliseconds) to use
9192
/// when updating the activity.
9293
publicfunc start(refreshRate:Int=40){
94+
guard console.supportsANSICommandselse{
95+
// Skip animations if the console does not support ANSI commands
96+
self.activity.outputActivityIndicator(to:self.console, state:.ready)
97+
return
98+
}
99+
93100
self.timer.schedule(
94101
deadline:DispatchTime.now(),
95102
repeating:.milliseconds(refreshRate),
@@ -113,8 +120,6 @@ public final class ActivityIndicator<A>: Sendable where A: ActivityIndicatorType
113120
}
114121
self.stopGroup.leave()
115122
}
116-
117-
self.timer.resume()
118123
}
119124

120125
/// Stops the `ActivityIndicator`, yielding a failed / error appearance.

‎Sources/ConsoleKitTerminal/Terminal/Console.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
/// Protocol for powering styled Console I/O.
24
///
35
/// # Output
@@ -76,4 +78,19 @@ public protocol Console: AnyObject, Sendable {
7678
func report(error:String, newLine:Bool)
7779

7880
varuserInfo:[AnySendableHashable:anySendable]{getset}
81+
82+
/// If the `Console` supports ANSI commands such as color and cursor movement.
83+
varsupportsANSICommands:Bool{get}
84+
}
85+
86+
extensionConsole{
87+
publicvarsupportsANSICommands:Bool{
88+
#if Xcode
89+
// Xcode output does not support ANSI commands
90+
returnfalse
91+
#else
92+
// If STDOUT is not an interactive terminal then omit ANSI commands
93+
returnisatty(STDOUT_FILENO)>0
94+
#endif
95+
}
7996
}

‎Sources/ConsoleKitTerminal/Terminal/Terminal.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public final class Terminal: Console, Sendable {
2222
iflet stylizeOverride=self.stylizedOutputOverride{
2323
return stylizeOverride
2424
}
25-
#if Xcode
26-
returnfalse
27-
#else
28-
returnisatty(STDOUT_FILENO)>0
29-
#endif
25+
return supportsANSICommands
3026
}
3127

3228
/// Create a new Terminal.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp