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> {
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
///
/// ### Examples

View File

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

View File

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

View File

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

View File

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