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))
Tip
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
activityBarWidthconfirmOverrideIf set, all calls toconfirm(_:)will use this value instead of asking the user.sizeThe size of theConsolewindow. Used for calculating lines printed and centering text.stylizedOutputOverridesupportsANSICommandsIf theConsolesupports ANSI commands such as color and cursor movement.userInfo
Instance Methods
ask(_:isSecure:)Requests input from the console after displaying a prompt.center(_:padding:)Centers aStringaccording to this console’ssize.choose(_:from:)Prompts the user to choose an item from the supplied array. The chosen item will be returned.choose(_:from:display:)Prompts the user to choose an item from the supplied array. The chosen item will be returned.clear(_:)Clears previously printedConsoleoutput according to theConsoleCleartype given.clear(lines:)Deletes lines that were previously printed to the terminal.confirm(_:)Requests yes / no confirmation from the user after a prompt.customActivity(title:titleAfterIndicator:frames:success:failure:)Creates an activity indicator with custom frames that are iterated over.customActivity(title:titleAfterIndicator:frames:success:failure:color:)Creates an activity indicator with custom frames that are iterated over.didOutputLines(count:)This method allows theConsoleimplementation to record how many lines have been printed so thatpushEphemeral()andpopEphemeral()knows how many lines to clear.error(_:newLine:)info(_:newLine:)input()Seeinput(isSecure:)input(isSecure:)Returns aStringof input read from theConsoleuntil a line feed character was found.loadingBar(title:)Creates a newLoadingBar-basedActivityIndicator.output(_:)Outputs serializedConsoleTextto theConsole.output(_:newLine:)Outputs serializedConsoleTextto theConsole.output(_:style:newLine:)popEphemeral()Pops a previous ephemeral console state. All text outputted to the console immidiately after the last call topushEphemeral()will be cleared.print(_:newLine:)progressBar(title:)Creates a newProgressBar-basedActivityIndicator.pushEphemeral()Pushes a new ephemeral console state. All text outputted to the console immidiately after this call can be cleared by usingpopEphemeral().report(error:newLine:)Outputs an error to theConsole’s error stream.success(_:newLine:)warning(_:newLine:)
Relationships
Inherits From
Swift.SendableSwift.SendableMetatype
Conforming Types
See Also
Terminal
TerminalGeneric console that uses a mixture of Swift and C standard libraries to fulfill protocol requirements.ConsoleColorSupported colors for creating aConsoleStyleforConsoleText.ConsoleStyleRepresentation of a style for outputting to a Console in different colors with differing attributes. A few suggested default styles are provided.ConsoleClearSupported methods for clearing theConsole.ConsoleTextA collection ofConsoleTextFragments. Represents stylized text that can be outputted to aConsole.ConsoleTextFragment+(_:_:)Appends aConsoleTextto anotherConsoleText.+=(_:_:)Appends aConsoleTextto anotherConsoleTextin-place.StringProtocol