-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP: New and user selectable progress bars #7897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rauhul
wants to merge
1
commit into
main
Choose a base branch
from
rauhul/blast-progress-bar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
195 changes: 195 additions & 0 deletions
195
Sources/Basics/ProgressAnimation/Concrete/BlastProgressAnimation.swift
This file contains hidden or 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,195 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
extension FormatStyle where Self == Duration.UnitsFormatStyle { | ||
static var blast: Self { | ||
.units( | ||
allowed: [.hours, .minutes, .seconds, .milliseconds], | ||
width: .narrow, | ||
maximumUnitCount: 2, | ||
fractionalPart: .init(lengthLimits: 0...2)) | ||
} | ||
} | ||
|
||
class BlastProgressAnimation { | ||
// Dependencies | ||
var terminal: TerminalController | ||
|
||
// Configuration | ||
var interactive: Bool | ||
var verbose: Bool | ||
var header: String? | ||
|
||
// Internal state | ||
var mostRecentTask: String | ||
var drawnLines: Int | ||
var state: ProgressState | ||
|
||
required init( | ||
stream: any WritableByteStream, | ||
coloring: TerminalColoring, | ||
interactive: Bool, | ||
verbose: Bool, | ||
header: String? | ||
) { | ||
self.terminal = TerminalController( | ||
stream: stream, | ||
coloring: coloring) | ||
self.interactive = interactive | ||
self.verbose = verbose | ||
self.header = header | ||
self.mostRecentTask = "" | ||
self.drawnLines = 0 | ||
self.state = .init() | ||
} | ||
} | ||
|
||
extension BlastProgressAnimation: ProgressAnimationProtocol { | ||
func update( | ||
id: Int, | ||
name: String, | ||
event: ProgressTaskState, | ||
at time: ContinuousClock.Instant | ||
) { | ||
let update = self.state.update( | ||
id: id, | ||
name: name, | ||
state: event, | ||
at: time) | ||
guard let (task, state) = update else { return } | ||
|
||
if self.interactive { | ||
self._clear() | ||
} | ||
|
||
if self.verbose, case .completed(let duration) = state { | ||
self._draw(task: task, duration: duration) | ||
self.terminal.newLine() | ||
} | ||
|
||
if self.interactive { | ||
self._draw() | ||
} else if case .started = state { | ||
// For the non-interactive case, only re-draw the status bar when a | ||
// new task starts | ||
self._drawStates() | ||
self.terminal.write(" ") | ||
self.terminal.write(task.name) | ||
self.terminal.newLine() | ||
} | ||
|
||
self._flush() | ||
} | ||
|
||
func interleave(_ bytes: some Collection<UInt8>) { | ||
if self.interactive { | ||
self._clear() | ||
} | ||
self.terminal.write(bytes) | ||
if self.interactive { | ||
self._draw() | ||
} | ||
self._flush() | ||
} | ||
|
||
func complete(_ message: String?) { | ||
self._complete(message) | ||
self._flush() | ||
} | ||
} | ||
|
||
extension BlastProgressAnimation { | ||
func _draw(state: ProgressTaskState) { | ||
self.terminal.text(styles: .foregroundColor(state.visualColor), .bold) | ||
self.terminal.write(state.visualSymbol) | ||
} | ||
|
||
func _draw(task: ProgressTask, duration: ContinuousClock.Duration?) { | ||
self.terminal.write(" ") | ||
self._draw(state: task.state) | ||
self.terminal.text(styles: .reset) | ||
self.terminal.write(" ") | ||
self.terminal.write(task.name) | ||
if let duration { | ||
self.terminal.text(styles: .foregroundColor(.white), .bold) | ||
self.terminal.write(" (\(duration.formatted(.blast)))") | ||
self.terminal.text(styles: .reset) | ||
} | ||
} | ||
|
||
func _draw(state: ProgressTaskState, count: Int, last: Bool) { | ||
self.terminal.text(styles: .notItalicNorBold, .foregroundColor(state.visualColor)) | ||
self.terminal.write(state.visualSymbol) | ||
self.terminal.write(" \(count)") | ||
self.terminal.text(styles: .defaultForegroundColor, .bold) | ||
if !last { | ||
self.terminal.write(", ") | ||
} | ||
} | ||
|
||
func _drawStates() { | ||
self.terminal.text(styles: .bold) | ||
self.terminal.write("(") | ||
self._draw(state: .discovered, count: self.state.counts.pending, last: false) | ||
self._draw(state: .started, count: self.state.counts.running, last: false) | ||
self._draw(state: .completed(.succeeded), count: self.state.counts.succeeded, last: false) | ||
self._draw(state: .completed(.failed), count: self.state.counts.failed, last: false) | ||
self._draw(state: .completed(.cancelled), count: self.state.counts.cancelled, last: false) | ||
self._draw(state: .completed(.skipped), count: self.state.counts.skipped, last: true) | ||
self.terminal.write(")") | ||
self.terminal.text(styles: .reset) | ||
} | ||
|
||
func _drawMessage(_ message: String?) { | ||
if let message { | ||
self.terminal.write(" ") | ||
self.terminal.write(message) | ||
} | ||
} | ||
|
||
func _draw() { | ||
assert(self.drawnLines == 0) | ||
self._drawStates() | ||
self._drawMessage(self.header) | ||
self.drawnLines += 1 | ||
let tasks = self.state.tasks.values.filter { $0.state == .started }.sorted() | ||
for task in tasks { | ||
self.terminal.newLine() | ||
self._draw(task: task, duration: nil) | ||
self.drawnLines += 1 | ||
} | ||
} | ||
|
||
func _complete(_ message: String?) { | ||
self._clear() | ||
self._drawStates() | ||
self._drawMessage(message ?? self.header) | ||
self.terminal.newLine() | ||
} | ||
|
||
func _clear() { | ||
guard self.drawnLines > 0 else { return } | ||
self.terminal.eraseLine(.entire) | ||
self.terminal.carriageReturn() | ||
for _ in 1..<self.drawnLines { | ||
self.terminal.moveCursorPrevious(lines: 1) | ||
self.terminal.eraseLine(.entire) | ||
} | ||
self.drawnLines = 0 | ||
} | ||
|
||
func _flush() { | ||
self.terminal.flush() | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
Sources/Basics/ProgressAnimation/Concrete/NinjaMultiLineProgressAnimation.swift
This file contains hidden or 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,72 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import protocol TSCBasic.WritableByteStream | ||
|
||
/// A multi-line ninja-like progress animation. | ||
final class NinjaMultiLineProgressAnimation { | ||
// Dependencies | ||
var terminal: TerminalController | ||
|
||
// Internal state | ||
var text: String | ||
var state: ProgressState | ||
|
||
required init( | ||
stream: any WritableByteStream, | ||
coloring: TerminalColoring, | ||
interactive: Bool, | ||
verbose: Bool, | ||
header: String? | ||
) { | ||
self.terminal = TerminalController( | ||
stream: stream, | ||
coloring: coloring) | ||
self.text = "" | ||
self.state = .init() | ||
} | ||
} | ||
|
||
extension NinjaMultiLineProgressAnimation: ProgressAnimationProtocol { | ||
func update( | ||
id: Int, | ||
name: String, | ||
event: ProgressTaskState, | ||
at time: ContinuousClock.Instant | ||
) { | ||
let update = self.state.update( | ||
id: id, | ||
name: name, | ||
state: event, | ||
at: time) | ||
guard let (task, _) = update else { return } | ||
guard self.text != task.name else { return } | ||
self.text = task.name | ||
self.terminal.write( | ||
"[\(self.state.counts.completed)/\(self.state.counts.total)] ") | ||
self.terminal.write(self.text) | ||
self.terminal.newLine() | ||
self.terminal.flush() | ||
} | ||
|
||
func interleave(_ bytes: some Collection<UInt8>) { | ||
self.terminal.write(bytes) | ||
self.terminal.flush() | ||
} | ||
|
||
func complete(_ message: String?) { | ||
if let message { | ||
self.terminal.write(message) | ||
self.terminal.flush() | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
Sources/Basics/ProgressAnimation/Concrete/NinjaRedrawingProgressAnimation.swift
This file contains hidden or 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,105 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A redrawing ninja-like progress animation. | ||
final class NinjaRedrawingProgressAnimation { | ||
// Dependencies | ||
var terminal: TerminalController | ||
|
||
// Internal state | ||
var text: String | ||
var hasDisplayedProgress: Bool | ||
var state: ProgressState | ||
|
||
required init( | ||
stream: any WritableByteStream, | ||
coloring: TerminalColoring, | ||
interactive: Bool, | ||
verbose: Bool, | ||
header: String? | ||
) { | ||
self.terminal = TerminalController( | ||
stream: stream, | ||
coloring: coloring) | ||
self.text = "" | ||
self.hasDisplayedProgress = false | ||
self.state = .init() | ||
} | ||
} | ||
|
||
extension NinjaRedrawingProgressAnimation: ProgressAnimationProtocol { | ||
func update( | ||
id: Int, | ||
name: String, | ||
event: ProgressTaskState, | ||
at time: ContinuousClock.Instant | ||
) { | ||
let update = self.state.update( | ||
id: id, | ||
name: name, | ||
state: event, | ||
at: time) | ||
guard let (task, _) = update else { return } | ||
self.text = task.name | ||
|
||
self._clear() | ||
self._draw() | ||
self._flush() | ||
} | ||
|
||
func interleave(_ bytes: some Collection<UInt8>) { | ||
self._clear() | ||
self.terminal.write(bytes) | ||
self._draw() | ||
self._flush() | ||
} | ||
|
||
func complete(_ message: String?) { | ||
self._complete(message) | ||
self._flush() | ||
} | ||
} | ||
|
||
extension NinjaRedrawingProgressAnimation { | ||
func _draw() { | ||
assert(!self.hasDisplayedProgress) | ||
let progressText = "[\(self.state.counts.completed)/\(self.state.counts.total)] \(self.text)" | ||
// FIXME: self.terminal.width | ||
let width = 80 | ||
if progressText.utf8.count > width { | ||
let suffix = "…" | ||
self.terminal.write(String(progressText.prefix(width - suffix.utf8.count))) | ||
self.terminal.write(suffix) | ||
} else { | ||
self.terminal.write(progressText) | ||
} | ||
self.hasDisplayedProgress = true | ||
} | ||
|
||
func _complete(_ message: String?) { | ||
#warning("TODO") | ||
if self.hasDisplayedProgress { | ||
self.terminal.newLine() | ||
} | ||
} | ||
|
||
func _clear() { | ||
guard self.hasDisplayedProgress else { return } | ||
self.terminal.eraseLine(.entire) | ||
self.terminal.carriageReturn() | ||
self.hasDisplayedProgress = false | ||
} | ||
|
||
func _flush() { | ||
self.terminal.flush() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this class can be
final
?