Files
helder/Sources/HelderCore/Schema/ArtisanConsole.swift
T

42 lines
1.5 KiB
Swift

import Foundation
/// How a line in the Schema view's artisan console is painted.
public enum ArtisanLineKind: String, Sendable, Equatable {
case command
case output
case error
case success
}
public struct ArtisanConsoleLine: Sendable, Equatable {
public var text: String
public var kind: ArtisanLineKind
public init(text: String, kind: ArtisanLineKind) {
self.text = text
self.kind = kind
}
}
public enum ArtisanOutputClassifier {
/// `$ php artisan helder:schema`, the echo before a run.
public static let commandEcho = "$ " + SchemaPaths.commandLine
/// Stderr and `ERROR`/`Exception` lines are errors; Laravel's `INFO`/`DONE` blocks and `Wrote …` are successes.
///
/// PHP writes warnings to stderr too, but there they still mean the run went wrong for the user.
public static func classify(_ line: String, isError: Bool) -> ArtisanLineKind {
if isError { return .error }
let trimmed = line.trimmingCharacters(in: .whitespaces)
if trimmed.hasPrefix("ERROR") || trimmed.hasPrefix("FAIL") || line.contains("Exception") { return .error }
if trimmed.hasPrefix("INFO") || trimmed.hasPrefix("DONE") || trimmed.hasPrefix("Wrote ") || trimmed.hasPrefix("Schema dumped") {
return .success
}
return .output
}
public static func line(_ text: String, isError: Bool) -> ArtisanConsoleLine {
ArtisanConsoleLine(text: text, kind: classify(text, isError: isError))
}
}