CommandLineToolkit
93 строки · 2.9 Кб
1import Logging
2
3/// Should be used all over the tool to provide consistent CLI experience
4public protocol ConsoleHandler {
5var isAtTTY: Bool { get }
6var isInteractive: Bool { get }
7var verbositySettings: ConsoleVerbositySettings { get }
8
9/// Ask user to input some string
10/// - Parameters:
11/// - title: question to ask
12/// - defaultValue: default value, user can hit enter to select it
13/// - Returns: User's input
14func input(
15title: String,
16defaultValue: String?,
17file: StaticString,
18line: UInt
19) async throws -> String
20
21/// Ask user a question
22/// - Parameters:
23/// - title: question to ask
24/// - defaultAnswer: default answer, yser can just hit enter
25/// - Returns: boolean answer
26func question(
27title: String,
28defaultAnswer: Bool,
29file: StaticString,
30line: UInt
31) async throws -> Bool
32
33/// Select values from a list
34/// - Parameters:
35/// - title: describes what user selects
36/// - values: list of possible values
37/// - options: possible selection options
38/// - Returns: array of selected values
39func select<Value>(
40title: String,
41values: [Selectable<Value>],
42mode: SelectionMode,
43options: SelectionOptions,
44file: StaticString,
45line: UInt
46) async throws -> [Value]
47
48/// Runs a chunk of work in a separate Console trace
49///
50/// This version provides ``TraceProgressUpdator``, so traces operation can report its progress
51///
52/// - Parameters:
53/// - level: Trace level, works the same as level in `Logger`
54/// - name: Name of trace to display
55/// - mode: Mode in which to run trace, specific modes allow to cleanup nested finished traces
56/// - work: Work to be performed inside of a trace
57/// - Returns: Value returned form `work` closure
58func trace<Value: Sendable>(
59level: Logger.Level,
60name: String,
61options: TraceOptions,
62file: StaticString,
63line: UInt,
64work: (TraceProgressUpdator) async throws -> Value
65) async throws -> Value
66
67/// Renders log, definition the same as in `LogHandler`
68func log(
69level: Logger.Level,
70message: Logger.Message,
71metadata: Logger.Metadata,
72source: String,
73file: String,
74function: String,
75line: UInt
76)
77
78/// Creates a log sink and allows to stream logs from underlying command line instrument
79///
80/// Should always run in a trace
81///
82/// - Parameters:
83/// - level: Log level
84/// - name: Name of stream
85/// - Returns: ``LogSink`` — an object which receives new log lines and appends those to the stream
86func logStream(
87level: Logger.Level,
88name: String,
89renderTail: Int,
90file: StaticString,
91line: UInt
92) -> LogSink
93}
94