-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,76 @@ | ||
import Foundation | ||
import Rainbow | ||
|
||
class ProgressStep { | ||
// MARK: - Attributes | ||
|
||
let message: String | ||
let successMessage: String? | ||
let errorMessage: String? | ||
let showSpinner: Bool | ||
let action: (@escaping (String) -> Void) async throws -> Void | ||
let theme: Theme | ||
let terminal: Terminaling | ||
let renderer: Rendering | ||
let standardPipelines: StandardPipelines | ||
var spinner = Spinner() | ||
|
||
init(message: String, successMessage: String?, errorMessage: String?, showSpinner: Bool, action: @escaping (@escaping (String) -> Void) async throws -> Void, theme: Theme, terminal: Terminaling, renderer: Rendering, standardPipelines: StandardPipelines, spinner: Spinner = Spinner()) { | ||
self.message = message | ||
self.successMessage = successMessage | ||
self.errorMessage = errorMessage | ||
self.showSpinner = showSpinner | ||
self.action = action | ||
self.theme = theme | ||
self.terminal = terminal | ||
self.renderer = renderer | ||
self.standardPipelines = standardPipelines | ||
self.spinner = spinner | ||
} | ||
|
||
func run() async throws { | ||
let start = DispatchTime.now() | ||
|
||
defer { spinner.stop() } | ||
|
||
var spinnerIcon: String? | ||
var lastMessage = message | ||
|
||
if showSpinner { | ||
spinner.spin { icon in | ||
spinnerIcon = icon | ||
self.render(message: lastMessage, icon: spinnerIcon ?? "ℹ︎") | ||
} | ||
} | ||
|
||
var _error: Error? = nil | ||
do { | ||
render(message: lastMessage, icon: spinnerIcon ?? "ℹ︎") | ||
try await action({ progressMessage in | ||
lastMessage = progressMessage | ||
self.render(message: lastMessage, icon: spinnerIcon ?? "ℹ︎") | ||
}) | ||
} catch { | ||
_error = error | ||
} | ||
|
||
let elapsedTime = Double(DispatchTime.now().uptimeNanoseconds - start.uptimeNanoseconds) / 1_000_000_000 | ||
let timeString = "[\(String(format: "%.1f", elapsedTime))s]".hexIfColoredTerminal(theme.muted, terminal) | ||
|
||
if _error != nil { | ||
renderer.render("\("⨯".hexIfColoredTerminal(theme.danger, terminal)) \(errorMessage ?? message) \(timeString)", standardPipeline: standardPipelines.output) | ||
} else { | ||
renderer.render("\("✔︎".hexIfColoredTerminal(theme.success, terminal)) \(successMessage ?? message) \(timeString)", standardPipeline: standardPipelines.output) | ||
} | ||
|
||
if let _error = _error { | ||
throw _error | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func render(message: String, icon: String) { | ||
renderer.render("\(icon.hexIfColoredTerminal(theme.primary, terminal)) \(message)", standardPipeline: standardPipelines.output) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,22 @@ | ||
import ArgumentParser | ||
import Foundation | ||
import Noora | ||
|
||
struct ProgressStepCommand: AsyncParsableCommand { | ||
static let configuration = CommandConfiguration( | ||
commandName: "progress-step", | ||
abstract: "A component to shows a progress step" | ||
) | ||
|
||
func run() async throws { | ||
try await Noora().progressStep(message: "Loading manifests", successMessage: "Manifests loaded", errorMessage: "Failed to load manifests") { progress in | ||
sleep(2) | ||
} | ||
try await Noora().progressStep(message: "Processing the graph", successMessage: "Project graph processed", errorMessage: "Failed to process the project graph") { progress in | ||
sleep(2) | ||
} | ||
try await Noora().progressStep(message: "Generating Xcode projects and workspsace", successMessage: "Xcode projects and workspace generated", errorMessage: "Failed to generate Xcode workspace and projects") { progress in | ||
sleep(2) | ||
} | ||
} | ||
} |
This file contains 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