Refactored highlight in the command-line and added JsonIOS date oversight for pretty printed

This commit is contained in:
Alexis Bridoux 2021-05-02 17:49:32 +02:00
parent eac87daded
commit a25c562d54
7 changed files with 45 additions and 13 deletions

View File

@ -101,3 +101,17 @@ infix operator <|>: SequencePrecedence
public func <|><A>(lhs: Parser<A>, rhs: Parser<A>) -> Parser<A> { public func <|><A>(lhs: Parser<A>, rhs: Parser<A>) -> Parser<A> {
lhs.or(rhs) lhs.or(rhs)
} }
infix operator <+>: SequencePrecedence
public func <+><A>(lhs: Parser<A>, rhs: Parser<A>) -> Parser<[A]> {
curry { [$0, $1] } <^> lhs <*> rhs
}
public func <+><A>(lhs: Parser<[A]>, rhs: Parser<A>) -> Parser<[A]> {
curry { $0 + [$1] } <^> lhs <*> rhs
}
public func <+><A>(lhs: Parser<[A]>, rhs: Parser<[A]>) -> Parser<[A]> {
curry { $0 + $1 } <^> lhs <*> rhs
}

View File

@ -94,6 +94,23 @@ public extension Parser {
} }
} }
/// Match characters until one of the forbidden ones is encountered
static func string(forbiddenCharacters isForbidden: @escaping (Character) -> Bool) -> Parser<String> {
Parser<String> { input in
guard !input.isEmpty else { return nil }
var remainder = input
var currentString = ""
while let char = remainder.first {
if isForbidden(char) { break }
currentString += String(char)
remainder = remainder.dropFirst()
}
return currentString.isEmpty ? nil : (currentString, remainder)
}
}
/// Match characters until the last ones equal the forbidden string, or if the character matches one of the forbidden ones /// Match characters until the last ones equal the forbidden string, or if the character matches one of the forbidden ones
/// ///
/// ### Examples /// ### Examples

View File

@ -76,6 +76,7 @@ extension CodableFormats {
private static let encoder: JSONEncoder = { private static let encoder: JSONEncoder = {
let encoder = JSONEncoder() let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601 encoder.dateEncodingStrategy = .iso8601
encoder.outputFormatting = .prettyPrinted
return encoder return encoder
}() }()

View File

@ -71,8 +71,8 @@ struct CSVCommand: ParsableCommand {
} else { } else {
var output = try explorer.exportString() var output = try explorer.exportString()
let colorInjector = try self.colorInjector(for: dataFormat) let highlight = try self.colorInjector(for: dataFormat)
output = colorise ? colorInjector.inject(in: output) : output output = colorise ? highlight(output) : output
print(output) print(output)
} }
} }

View File

@ -43,7 +43,7 @@ extension ParsableCommand {
extension ParsableCommand { extension ParsableCommand {
func colorInjector(for format: Scout.DataFormat) throws -> TextInjector { func colorInjector(for format: Scout.DataFormat) throws -> (String) -> String {
switch format { switch format {
case .json: case .json:
@ -51,28 +51,28 @@ extension ParsableCommand {
if let colors = try getColorFile()?.json { if let colors = try getColorFile()?.json {
jsonInjector.delegate = JSONInjectorColorDelegate(colors: colors) jsonInjector.delegate = JSONInjectorColorDelegate(colors: colors)
} }
return jsonInjector return jsonInjector.inject
case .plist: case .plist:
let plistInjector = PlistInjector(type: .terminal) let plistInjector = PlistInjector(type: .terminal)
if let colors = try getColorFile()?.plist { if let colors = try getColorFile()?.plist {
plistInjector.delegate = PlistInjectorColorDelegate(colors: colors) plistInjector.delegate = PlistInjectorColorDelegate(colors: colors)
} }
return plistInjector return plistInjector.inject
case .yaml: case .yaml:
let yamlInjector = YAMLInjector(type: .terminal) let yamlInjector = YAMLInjector(type: .terminal)
if let colors = try getColorFile()?.yaml { if let colors = try getColorFile()?.yaml {
yamlInjector.delegate = YAMLInjectorColorDelegate(colors: colors) yamlInjector.delegate = YAMLInjectorColorDelegate(colors: colors)
} }
return yamlInjector return yamlInjector.inject
case .xml: case .xml:
let xmlInjector = XMLEnhancedInjector(type: .terminal) let xmlInjector = XMLEnhancedInjector(type: .terminal)
if let colors = try getColorFile()?.xml { if let colors = try getColorFile()?.xml {
xmlInjector.delegate = XMLInjectorColorDelegate(colors: colors) xmlInjector.delegate = XMLInjectorColorDelegate(colors: colors)
} }
return xmlInjector return xmlInjector.inject
} }
} }

View File

@ -126,8 +126,8 @@ extension SADCommand {
func printOutput(output: String, with format: Scout.DataFormat) throws { func printOutput(output: String, with format: Scout.DataFormat) throws {
if colorise { if colorise {
let injector = try colorInjector(for: format) let highlight = try colorInjector(for: format)
print(injector.inject(in: output)) print(highlight(output))
} else { } else {
print(output) print(output)
} }

View File

@ -75,13 +75,13 @@ struct ReadCommand: PathExplorerInputCommand, ExportCommand {
print(value) print(value)
case .noExport: case .noExport:
let colorInjector = try self.colorInjector(for: P.format) let highlight = try self.colorInjector(for: P.format)
let output = colorise ? colorInjector.inject(in: value) : value let output = colorise ? highlight(value) : value
print(output) print(output)
case .dataFormat(let format): case .dataFormat(let format):
let colorInjector = try self.colorInjector(for: format) let highlight = try self.colorInjector(for: format)
let output = colorise ? colorInjector.inject(in: value) : value let output = colorise ? highlight(value) : value
print(output) print(output)
} }
} }