mirror of
https://github.com/phranck/TUIkit.git
synced 2026-06-20 09:54:37 +00:00
BREAKING CHANGE: Package name changed due to name collision with existing rensbreur/SwiftTUI package. Changes: - Rename package from SwiftTUI to TUIKit in Package.swift - Rename Sources/SwiftTUI to Sources/TUIKit - Rename Sources/SwiftTUIExample to Sources/TUIKitExample - Rename Tests/SwiftTUITests to Tests/TUIKitTests - Rename SwiftTUI.swift to TUIKit.swift - Update all imports: import SwiftTUI -> import TUIKit - Update all code references: SwiftTUI.renderToBuffer -> TUIKit.renderToBuffer - Update documentation comments - Rename swiftTUIVersion to tuiKitVersion All 181 tests passing.
175 lines
4.3 KiB
Swift
175 lines
4.3 KiB
Swift
//
|
|
// Text.swift
|
|
// TUIKit
|
|
//
|
|
// A view for displaying text in the terminal.
|
|
//
|
|
|
|
/// A view that displays text in the terminal.
|
|
///
|
|
/// `Text` is one of the most fundamental views in TUIKit. It displays
|
|
/// a string in the terminal and supports various formatting options.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```swift
|
|
/// Text("Hello, World!")
|
|
///
|
|
/// Text("Bold")
|
|
/// .bold()
|
|
///
|
|
/// Text("Colored")
|
|
/// .foregroundColor(.red)
|
|
/// ```
|
|
public struct Text: View {
|
|
/// The text to display.
|
|
public let content: String
|
|
|
|
/// The style of the text (color, formatting, etc.).
|
|
public var style: TextStyle
|
|
|
|
/// Creates a text view with the specified string.
|
|
///
|
|
/// - Parameter content: The text to display.
|
|
public init(_ content: String) {
|
|
self.content = content
|
|
self.style = TextStyle()
|
|
}
|
|
|
|
/// Creates a text view with a verbatim string.
|
|
///
|
|
/// - Parameter verbatim: The text to display verbatim.
|
|
public init(verbatim: String) {
|
|
self.content = verbatim
|
|
self.style = TextStyle()
|
|
}
|
|
|
|
public var body: Never {
|
|
fatalError("Text is a primitive view and renders directly")
|
|
}
|
|
}
|
|
|
|
// MARK: - Text Modifiers
|
|
|
|
extension Text {
|
|
/// Sets the text color.
|
|
///
|
|
/// - Parameter color: The desired foreground color.
|
|
/// - Returns: A new text with the applied color.
|
|
public func foregroundColor(_ color: Color) -> Text {
|
|
var copy = self
|
|
copy.style.foregroundColor = color
|
|
return copy
|
|
}
|
|
|
|
/// Sets the background color.
|
|
///
|
|
/// - Parameter color: The desired background color.
|
|
/// - Returns: A new text with the applied background color.
|
|
public func backgroundColor(_ color: Color) -> Text {
|
|
var copy = self
|
|
copy.style.backgroundColor = color
|
|
return copy
|
|
}
|
|
|
|
/// Makes the text bold.
|
|
///
|
|
/// - Returns: A new text with bold formatting.
|
|
public func bold() -> Text {
|
|
var copy = self
|
|
copy.style.isBold = true
|
|
return copy
|
|
}
|
|
|
|
/// Makes the text italic.
|
|
///
|
|
/// - Returns: A new text with italic formatting.
|
|
public func italic() -> Text {
|
|
var copy = self
|
|
copy.style.isItalic = true
|
|
return copy
|
|
}
|
|
|
|
/// Underlines the text.
|
|
///
|
|
/// - Returns: A new text with underline formatting.
|
|
public func underline() -> Text {
|
|
var copy = self
|
|
copy.style.isUnderlined = true
|
|
return copy
|
|
}
|
|
|
|
/// Strikes through the text.
|
|
///
|
|
/// - Returns: A new text with strikethrough formatting.
|
|
public func strikethrough() -> Text {
|
|
var copy = self
|
|
copy.style.isStrikethrough = true
|
|
return copy
|
|
}
|
|
|
|
/// Dims the text (reduced intensity).
|
|
///
|
|
/// - Returns: A new text with dimmed appearance.
|
|
public func dim() -> Text {
|
|
var copy = self
|
|
copy.style.isDim = true
|
|
return copy
|
|
}
|
|
|
|
/// Makes the text blink (if supported by the terminal).
|
|
///
|
|
/// - Returns: A new text with blink effect.
|
|
public func blink() -> Text {
|
|
var copy = self
|
|
copy.style.isBlink = true
|
|
return copy
|
|
}
|
|
|
|
/// Inverts foreground and background colors.
|
|
///
|
|
/// - Returns: A new text with inverted colors.
|
|
public func inverted() -> Text {
|
|
var copy = self
|
|
copy.style.isInverted = true
|
|
return copy
|
|
}
|
|
}
|
|
|
|
// MARK: - TextStyle
|
|
|
|
/// The style of a text view.
|
|
///
|
|
/// Contains all formatting options like color, bold, etc.
|
|
public struct TextStyle: Sendable {
|
|
/// The foreground color of the text.
|
|
public var foregroundColor: Color?
|
|
|
|
/// The background color of the text.
|
|
public var backgroundColor: Color?
|
|
|
|
/// Whether the text is bold.
|
|
public var isBold: Bool = false
|
|
|
|
/// Whether the text is italic.
|
|
public var isItalic: Bool = false
|
|
|
|
/// Whether the text is underlined.
|
|
public var isUnderlined: Bool = false
|
|
|
|
/// Whether the text is strikethrough.
|
|
public var isStrikethrough: Bool = false
|
|
|
|
/// Whether the text is dimmed.
|
|
public var isDim: Bool = false
|
|
|
|
/// Whether the text blinks.
|
|
public var isBlink: Bool = false
|
|
|
|
/// Whether foreground and background colors are inverted.
|
|
public var isInverted: Bool = false
|
|
|
|
/// Creates a default TextStyle with no formatting.
|
|
public init() {}
|
|
}
|