2015-11-03 01:22:39 +03:00
|
|
|
private func unified(term: Term, source: String) -> String {
|
2015-11-03 01:26:23 +03:00
|
|
|
return term.cata { info, syntax -> (String, Range<Int>?) in
|
2015-11-03 01:24:22 +03:00
|
|
|
switch syntax {
|
|
|
|
case .Leaf:
|
2015-11-03 01:26:23 +03:00
|
|
|
return (String(source.utf16[info.range]), info.range)
|
2015-11-03 01:26:28 +03:00
|
|
|
case let .Indexed(i):
|
|
|
|
return (unified(info.range, children: i, source: source), info.range)
|
2015-11-03 01:26:38 +03:00
|
|
|
case let .Fixed(f):
|
|
|
|
return (unified(info.range, children: f, source: source), info.range)
|
2015-11-03 01:28:21 +03:00
|
|
|
case let .Keyed(k):
|
2015-11-03 01:29:43 +03:00
|
|
|
return (unified(info.range, children: k.values.sort(isOrderedBefore), source: source), info.range)
|
2015-11-03 01:22:39 +03:00
|
|
|
}
|
2015-11-03 01:26:23 +03:00
|
|
|
}.0
|
2015-11-03 01:22:39 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 01:29:35 +03:00
|
|
|
private func isOrderedBefore(a: (String, Range<Int>?), _ b: (String, Range<Int>?)) -> Bool {
|
|
|
|
if let a = a.1, b = b.1 {
|
|
|
|
return a.startIndex < b.startIndex
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-03 01:39:42 +03:00
|
|
|
private var isTTY = isatty(STDOUT_FILENO) != 0
|
2015-11-03 01:39:47 +03:00
|
|
|
private var isDumb = String.fromCString(getenv("TERM")).map { !$0.hasPrefix("dumb") } ?? false
|
2015-11-03 01:41:56 +03:00
|
|
|
private var shouldFormat = isTTY && !isDumb
|
2015-11-03 01:39:42 +03:00
|
|
|
|
2015-11-03 02:02:37 +03:00
|
|
|
private struct Attribute {
|
|
|
|
let colour: Colour
|
|
|
|
let style: Style
|
|
|
|
|
|
|
|
enum Colour: Int {
|
|
|
|
case Black = 30
|
|
|
|
case Red
|
|
|
|
case Green
|
|
|
|
case Yellow
|
|
|
|
case Blue
|
|
|
|
case Purple
|
|
|
|
case Cyan
|
|
|
|
case White
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Style: Int {
|
|
|
|
case Normal = 0
|
|
|
|
case Bold = 1
|
|
|
|
case Underline = 4
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrap(string: String) -> String {
|
|
|
|
return shouldFormat
|
|
|
|
? "\u{001B}[\(style);\(colour)m\(string)\u{001B}[0m"
|
|
|
|
: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 01:15:27 +03:00
|
|
|
private func unified(patch: Patch<Term>, before: String, after: String) -> String {
|
2015-11-03 01:30:41 +03:00
|
|
|
return (patch.state.before.map { "{-\(unified($0, source: before))-}" } ?? "")
|
|
|
|
+ (patch.state.after.map { "{+\(unified($0, source: after))+}" } ?? "")
|
2015-11-02 22:21:34 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 00:07:34 +03:00
|
|
|
private func range(patch: Patch<Term>) -> Range<Int>? {
|
2015-11-03 00:07:57 +03:00
|
|
|
return patch.state.after?.extract.range
|
2015-11-03 00:02:23 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 00:14:03 +03:00
|
|
|
private func unified(range: Range<Int>, children: [(String, Range<Int>?)], source: String) -> String {
|
|
|
|
var previous = range.startIndex
|
|
|
|
var out: String = ""
|
|
|
|
for (string, range) in children {
|
|
|
|
if let range = range {
|
|
|
|
out += String(source.utf16[previous..<range.startIndex])
|
|
|
|
previous = range.endIndex
|
|
|
|
}
|
|
|
|
out += string
|
|
|
|
}
|
|
|
|
return out + String(source.utf16[previous..<range.endIndex])
|
|
|
|
}
|
|
|
|
|
2015-11-02 22:19:10 +03:00
|
|
|
func unified(diff: Diff, before: String, after: String) -> String {
|
2015-11-03 01:15:27 +03:00
|
|
|
return diff.map { (unified($0, before: before, after: after), range($0)) }.cata { info, syntax in
|
2015-11-03 00:02:58 +03:00
|
|
|
switch syntax {
|
|
|
|
case .Leaf:
|
|
|
|
return (String(after.utf16[info.1.range]), info.1.range)
|
|
|
|
case let .Indexed(i):
|
2015-11-03 00:15:34 +03:00
|
|
|
return (unified(info.1.range, children: i, source: after), info.1.range)
|
2015-11-03 00:02:58 +03:00
|
|
|
case let .Fixed(f):
|
2015-11-03 00:15:13 +03:00
|
|
|
return (unified(info.1.range, children: f, source: after), info.1.range)
|
2015-11-03 00:02:58 +03:00
|
|
|
case let .Keyed(k):
|
2015-11-03 01:29:43 +03:00
|
|
|
return (unified(info.1.range, children: k.values.sort(isOrderedBefore), source: after), info.1.range)
|
2015-11-03 00:02:58 +03:00
|
|
|
}
|
|
|
|
}.0
|
2015-11-02 22:04:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
import Doubt
|