Skip to content

Commit

Permalink
Simplify text formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Nov 24, 2023
1 parent 5ff789a commit 478e0b6
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 502 deletions.
18 changes: 9 additions & 9 deletions Sources/Backend/FileManagement/FileVerificationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ enum FileVerificationError: FormattedError {
case isNotDirectory(String)
case invalidPathExtension(String, FileType?)

var errorMessage: FormattedText {
var errorMessage: String {
switch self {
case .alreadyExists(let path):
return "'\(path, color: .yellow)' already exists"
return "'\(path.formatted(color: .yellow))' already exists"
case .doesNotExist(let path):
return "No such file or directory '\(path, color: .yellow)'"
return "No such file or directory '\(path.formatted(color: .yellow))'"
case .isDirectory(let path):
return "'\(path, color: .yellow)' is a directory"
return "'\(path.formatted(color: .yellow))' is a directory"
case .isNotDirectory(let path):
return "'\(path, color: .yellow)' is not a directory"
return "'\(path.formatted(color: .yellow))' is not a directory"
case .invalidPathExtension(let pathExtension, let outputType):
let start: FormattedText = "Invalid path extension '\(pathExtension, color: .yellow, style: .bold)'"
let start = "Invalid path extension '\(pathExtension.formatted(color: .yellow, style: .bold))'"
guard let outputType else {
return start
}
if let type = outputType.preferredFilenameExtension {
return start + " for expected output type '\(type, color: .cyan, style: .bold)'"
return start + " for expected output type '\(type.formatted(color: .cyan, style: .bold))'"
}
return start + " for unknown output type"
}
}

var fix: FormattedText? {
var fix: String? {
if case .invalidPathExtension(_, let outputType) = self {
if let type = outputType?.preferredFilenameExtension {
return "Use path extension '\(type, color: .green, style: .bold)'"
return "Use path extension '\(type.formatted(color: .green, style: .bold))'"
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions Sources/Backend/ImageProcessing/Iconset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ extension Iconset {
enum ValidationError: String, FormattedError {
case invalidDimensions = "Image width and height must be equal."

var errorMessage: FormattedText {
"\("Invalid icon", color: .red) \(rawValue, style: .bold)"
var errorMessage: String {
"Invalid icon".formatted(color: .red) + " - " + rawValue.formatted(style: .bold)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Backend/ImageProcessing/ImageProcessingError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum ImageProcessingError: String, FormattedError {
case pdfDocumentError = "Error with PDF document."
case svgCreationError = "Error creating image data from SVG."

var errorMessage: FormattedText {
"\("Could not process image", color: .red) \(rawValue, style: .bold)"
var errorMessage: String {
"Could not process image".formatted(color: .red) + " - " + rawValue.formatted(style: .bold)
}
}
10 changes: 2 additions & 8 deletions Sources/Backend/Runners/MainRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,9 @@ public struct MainRunner: Runner {

func exit(with error: Error) -> Never {
let box = FormattedErrorBox(error: error)
let errorText = FormattedText("error:", color: .red, style: .bold)
.appending(" ")
.appending(box.errorMessage)
print(errorText)
print("error:".formatted(color: .red, style: .bold) + " " + box.errorMessage)
if let fix = box.fix {
let fixText = FormattedText("fix:", color: .green, style: .bold)
.appending(" ")
.appending(fix)
print(fixText)
print("fix:".formatted(color: .green, style: .bold) + " " + fix)
}
Darwin.exit(EXIT_FAILURE)
}
Expand Down
26 changes: 10 additions & 16 deletions Sources/Backend/Utilities/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ import Foundation
/// to a command line interface.
public protocol FormattedError: Error, TextOutputStreamable {
/// The formatted error message to display.
///
/// If one of either standard output or standard error does not point to
/// a terminal, the message is displayed without formatting.
var errorMessage: FormattedText { get }
var errorMessage: String { get }

/// An optional formatted message to display that describes how the user
/// can remedy this error.
///
/// If one of either standard output or standard error does not point to
/// a terminal, the message is displayed without formatting.
var fix: FormattedText? { get }
var fix: String? { get }
}

extension FormattedError {
public var fix: FormattedText? { nil }
public var fix: String? { nil }
}

extension FormattedError {
Expand All @@ -40,14 +34,14 @@ extension FormattedError {
struct FormattedErrorBox: FormattedError {
let error: any Error

var errorMessage: FormattedText {
var errorMessage: String {
if let error = error as? FormattedError {
return error.errorMessage
}
return FormattedText(contentsOf: error.localizedDescription)
return error.localizedDescription
}

var fix: FormattedText? {
var fix: String? {
if let error = error as? FormattedError {
return error.fix
}
Expand All @@ -66,12 +60,12 @@ struct ContextualDataError: FormattedError {
/// A string describing the context in which this error occurred.
let context: String

var errorMessage: FormattedText {
var message = FormattedText(context + ":", color: .yellow, style: .bold)
var errorMessage: String {
var message = "\(context):".formatted(color: .yellow, style: .bold) + " "
if let string = String(data: data, encoding: .utf8) {
message.append(" \("An error occurred with the following data:", color: .red) \(string)")
message += "An error occurred with the following data:".formatted(color: .red) + " " + string
} else {
message.append(" An unknown error occurred")
message += "An unknown error occurred"
}
return message
}
Expand Down
56 changes: 34 additions & 22 deletions Sources/Backend/Utilities/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,51 @@
// MARK: - CustomStringConvertible

extension CustomStringConvertible {
/// Returns a formatted text instance that formats a textual representation of
/// this value using the given color and style.
/// Returns a textual representation of this value, formatted using
/// the given color and style.
///
/// - Parameters:
/// - color: The color to use in the resulting formatted text instance.
/// - style: The style to use in the resulting formatted text instance.
/// - color: The color to use in the resulting formatted text.
/// - style: The style to use in the resulting formatted text.
///
/// - Returns: A formatted text instance that formats a textual representation
/// of this value using the given color and style.
func formatted(color: TextOutputColor, style: TextOutputStyle) -> FormattedText {
FormattedText(self, color: color, style: style)
/// - Returns: A textual representation of this value, formatted using
/// the given color and style.
public func formatted(color: TextOutputColor, style: TextOutputStyle) -> String {
if OutputHandle.standardOutput.isTerminal && OutputHandle.standardError.isTerminal {
return style.onCode + color.onCode + String(describing: self) + color.offCode + style.offCode
} else {
return String(describing: self)
}
}

/// Returns a formatted text instance that formats a textual representation of
/// this value using the given color.
/// Returns a textual representation of this value, formatted using
/// the given color.
///
/// - Parameter color: The color to use in the resulting formatted text instance.
/// - Parameter color: The color to use in the resulting formatted text.
///
/// - Returns: A formatted text instance that formats a textual representation
/// of this value using the given color.
func formatted(color: TextOutputColor) -> FormattedText {
FormattedText(self, color: color)
/// - Returns: A textual representation of this value, formatted using
/// the given color.
public func formatted(color: TextOutputColor) -> String {
if OutputHandle.standardOutput.isTerminal && OutputHandle.standardError.isTerminal {
return color.onCode + String(describing: self) + color.offCode
} else {
return String(describing: self)
}
}

/// Returns a formatted text instance that formats a textual representation of
/// this value using the given style.
/// Returns a textual representation of this value, formatted using
/// the given style.
///
/// - Parameter style: The style to use in the resulting formatted text instance.
/// - Parameter style: The style to use in the resulting formatted text.
///
/// - Returns: A formatted text instance that formats a textual representation
/// of this value using the given style.
func formatted(style: TextOutputStyle) -> FormattedText {
FormattedText(self, style: style)
/// - Returns: A textual representation of this value, formatted using
/// the given style.
public func formatted(style: TextOutputStyle) -> String {
if OutputHandle.standardOutput.isTerminal && OutputHandle.standardError.isTerminal {
return style.onCode + String(describing: self) + style.offCode
} else {
return String(describing: self)
}
}
}

Expand Down
Loading

0 comments on commit 478e0b6

Please sign in to comment.