Skip to content

Commit

Permalink
Add format utility to Noora
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Feb 23, 2025
1 parent 625f38f commit 892de5b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/Noora/Noora.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public protocol Noorable {
visibleLines: UInt,
task: @escaping (@escaping (TerminalText) -> Void) async throws -> Void
) async throws

/// Formats the given terminal text using the current theme.
/// - Parameter terminalText: The terminal text to format.
/// - Returns: The formatted text as a String.
func format(_ terminalText: TerminalText) -> String
}

public class Noora: Noorable {
Expand Down Expand Up @@ -340,6 +345,10 @@ public class Noora: Noorable {
standardPipelines: StandardPipelines()
).run()
}

public func format(_ terminalText: TerminalText) -> String {
terminalText.formatted(theme: theme, terminal: terminal)
}
}

extension Noorable {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Noora/NooraMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
)
}

public func format(_ terminalText: TerminalText) -> String {
noora.format(terminalText)
}

private class StandardPipelineEventsRecorder {
var events: [StandardOutputEvent] = []
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/Noora/Utilities/TerminalText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ public struct TerminalText: Equatable, Hashable {
case raw(String)
/// A string that represents a system command (e.g. 'tuist generate')
case command(String)

/// A string with the theme's primary color
case primary(String)
/// A string with the theme's secondary color
case secondary(String)
/// A string with the theme's muted color
case muted(String)
/// A string with the theme's accent color
case accent(String)
/// A string with the theme's danger color
case danger(String)
/// A string with the theme's success color
case success(String)
}

/// Every component of the interpolated string.
Expand All @@ -19,6 +32,12 @@ public struct TerminalText: Equatable, Hashable {
switch component {
case let .raw(rawString): rawString
case let .command(command): "'\(command)'".hexIfColoredTerminal(theme.secondary, terminal)
case let .primary(primary): primary.hexIfColoredTerminal(theme.primary, terminal)
case let .secondary(secondary): secondary.hexIfColoredTerminal(theme.secondary, terminal)
case let .muted(muted): muted.hexIfColoredTerminal(theme.muted, terminal)
case let .accent(accent): accent.hexIfColoredTerminal(theme.accent, terminal)
case let .danger(danger): danger.hexIfColoredTerminal(theme.danger, terminal)
case let .success(success): success.hexIfColoredTerminal(theme.success, terminal)
}
}
.joined()
Expand Down

0 comments on commit 892de5b

Please sign in to comment.