Protocol
Console
Protocol for powering styled Console I/O.
protocol Console : AnyObject, Sendable
Output
Consoles can output stylized text via the ConsoleText struct.
console.output("Hello, " + "world!".consoleText(color: .green))
See ConsoleStyle for all available text style options.
There are also convenience methods for printing common styles.
console.info("Here is some information")
Input
Consoles can also request input from the user.
let answer = console.ask("How are you doing?")
print(answer)
Clear
Consoles can clear previously outputted content using clear(_:).
console.print("Hello!")
console.clear(.line) // delete hello
See pushEphemeral() method for clearing arbitrary chunks of output.
Other
Use the report(error:newLine:) method for reporting errors to the Console.
Get the Console’s current size using the size property.
Topics
Instance Properties
var activityBarWidth: Intvar confirmOverride: Bool?If set, all calls toconfirm(_:)will use this value instead of asking the user.var size: (width: Int, height: Int)The size of theConsolewindow. Used for calculating lines printed and centering text.var stylizedOutputOverride: Bool?If set, overrides aTerminal’s own determination as to whether its output supports color commands. Useful for easily implementing an option of the form--color=no|yes|auto. If the activeConsoleis not specifically aTerminal, has no effect.var supportsANSICommands: BoolIf theConsolesupports ANSI commands such as color and cursor movement.var userInfo: [AnySendableHashable : any Sendable]
Instance Methods
func ask(ConsoleText, isSecure: Bool) -> StringRequests input from the console after displaying a prompt.func center(_:padding:)Centers aStringaccording to this console’ssize.func choose<T>(ConsoleText, from: [T]) -> TPrompts the user to choose an item from the supplied array. The chosen item will be returned.func choose<T>(ConsoleText, from: [T], display: (T) -> ConsoleText) -> TPrompts the user to choose an item from the supplied array. The chosen item will be returned.func clear(ConsoleClear)Clears previously printedConsoleoutput according to theConsoleCleartype given.func clear(lines: Int)Deletes lines that were previously printed to the terminal.func confirm(ConsoleText) -> BoolRequests yes / no confirmation from the user after a prompt.func customActivity(frames: [ConsoleText], success: String, failure: String) -> ActivityIndicator<CustomActivity>Creates an activity indicator with custom frames that are iterated over.func customActivity(frames: [String], success: String, failure: String, color: ConsoleColor) -> ActivityIndicator<CustomActivity>Creates an activity indicator with custom frames that are iterated over.func didOutputLines(count: Int)This method allows theConsoleimplementation to record how many lines have been printed so thatpushEphemeral()andpopEphemeral()knows how many lines to clear.func error(String, newLine: Bool)Outputs aStringto theConsolewithConsoleStyle.errorstyle.func info(String, newLine: Bool)Outputs aStringto theConsolewithConsoleStyle.infostyle.func input() -> StringSeeConsole.input(isSecure:)func input(isSecure: Bool) -> StringReturns aStringof input read from theConsoleuntil a line feed character was found.func loadingBar(title: String, targetQueue: DispatchQueue?) -> ActivityIndicator<LoadingBar>Creates a newLoadingBar-basedActivityIndicator.func output(ConsoleText)Outputs serializedConsoleTextto theConsole.func output(ConsoleText, newLine: Bool)Outputs serializedConsoleTextto theConsole.func output(String, style: ConsoleStyle, newLine: Bool)Outputs aStringto theConsolewith the specifiedConsoleStylestyle.func popEphemeral()Pops a previous ephemeral console state. All text outputted to the console immidiately after the last call topushEphemeral()will be cleared.func print(String, newLine: Bool)Outputs aStringto theConsolewithConsoleStyle.plainstyle.func progressBar(title: String, targetQueue: DispatchQueue?) -> ActivityIndicator<ProgressBar>Creates a newProgressBar-basedActivityIndicator.func pushEphemeral()Pushes a new ephemeral console state. All text outputted to the console immidiately after this call can be cleared by usingpopEphemeral().func report(error: String, newLine: Bool)Outputs an error to theConsole’s error stream.func success(String, newLine: Bool)Outputs aStringto theConsolewithConsoleStyle.successstyle.func wait(seconds: Double)Blocks the current thread for the specified number of seconds.func warning(String, newLine: Bool)Outputs aStringto theConsolewithConsoleStyle.warningstyle.
Relationships
Inherits From
Swift.SendableSwift.SendableMetatype