|
| 1 | +///| |
| 2 | +fn JsonEdit::stringify(self : JsonEdit, terminal~ : Bool) -> String { |
| 3 | + output_colorful_text_for_terminal.val = terminal |
| 4 | + let output = Array::new() |
| 5 | + substringify(None, self, output, " ", 0) |
| 6 | + return output.join("\n") |
| 7 | +} |
1 | 8 |
|
| 9 | +///| |
| 10 | +fn json_substringify( |
| 11 | + key : String?, |
| 12 | + json : Json, |
| 13 | + output : Array[String], |
| 14 | + tag : String, |
| 15 | + indent : Int, |
| 16 | +) -> Unit { |
| 17 | + let prefix = if key is Some(key) { "\{key}: " } else { "" } |
| 18 | + let subindent = indent + 2 |
| 19 | + match json { |
| 20 | + Object(object) => { |
| 21 | + output.push("\{String::make(indent, ' ')}\{prefix}{") |
| 22 | + for subkey, subvalue in object.iterator2() { |
| 23 | + json_substringify(Some(subkey), subvalue, output, tag, subindent) |
| 24 | + } |
| 25 | + output.push_line(tag, "\{String::make(indent, ' ')}}") |
| 26 | + } |
| 27 | + Array(arr) => { |
| 28 | + output.push_line(tag, "\{String::make(indent, ' ')}\{prefix}[") |
| 29 | + for subvalue in arr { |
| 30 | + json_substringify(None, subvalue, output, tag, subindent) |
| 31 | + } |
| 32 | + } |
| 33 | + json => |
| 34 | + output.push_line( |
| 35 | + tag, |
| 36 | + String::make(indent, ' ') + prefix + json.to_string(), |
| 37 | + ) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +///| |
| 42 | +fn substringify( |
| 43 | + key : String?, |
| 44 | + jsonedit : JsonEdit, |
| 45 | + output : Array[String], |
| 46 | + tag : String, |
| 47 | + indent : Int, |
| 48 | +) -> Unit { |
| 49 | + let prefix = if key is Some(key) { "\{key}: " } else { "" } |
| 50 | + let subindent = indent + 2 |
| 51 | + match jsonedit { |
| 52 | + Replace(old~, new~) => { |
| 53 | + json_substringify(key, old, output, "-", indent) |
| 54 | + json_substringify(key, new, output, "-", indent) |
| 55 | + } |
| 56 | + Array(edits) => { |
| 57 | + output.push_line(tag, "\{String::make(indent, ' ')}\{prefix}[") |
| 58 | + for edit in edits { |
| 59 | + match edit { |
| 60 | + None => output.push_line(tag, "\{String::make(subindent, ' ')}...") |
| 61 | + Some(Modification(change)) => |
| 62 | + substringify(None, change, output, " ", subindent) |
| 63 | + Some(NoChange(json)) => |
| 64 | + json_substringify(None, json, output, " ", subindent) |
| 65 | + Some(Delete(json)) => |
| 66 | + json_substringify(None, json, output, "-", subindent) |
| 67 | + Some(Insert(json)) => |
| 68 | + json_substringify(None, json, output, "+", subindent) |
| 69 | + } |
| 70 | + } |
| 71 | + output.push_line(tag, "\{String::make(indent, ' ')}]") |
| 72 | + } |
| 73 | + Object(diff, added~, deleted~) => { |
| 74 | + output.push_line(tag, "\{String::make(indent, ' ')}\{prefix}{") |
| 75 | + for key, value in deleted.iterator2() { |
| 76 | + json_substringify(Some(key), value, output, "-", subindent) |
| 77 | + } |
| 78 | + for key, value in added.iterator2() { |
| 79 | + json_substringify(Some(key), value, output, "+", subindent) |
| 80 | + } |
| 81 | + for key, value in diff.iterator2() { |
| 82 | + substringify(Some(key), value, output, " ", subindent) |
| 83 | + } |
| 84 | + output.push_line(tag, "\{String::make(indent, ' ')}}") |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +///| |
| 90 | +let output_colorful_text_for_terminal : Ref[Bool] = Ref::new(false) |
| 91 | + |
| 92 | +///| |
| 93 | +fn Array::push_line(self : Array[String], tag : String, line : String) -> Unit { |
| 94 | + let tagged_line = "\{tag}\{line}" |
| 95 | + if output_colorful_text_for_terminal.val { |
| 96 | + match tag { |
| 97 | + "+" => |
| 98 | + // green |
| 99 | + self.push("\u{1b}[32m\{tag}\{line}\u{1b}[0m") |
| 100 | + "-" => |
| 101 | + // red |
| 102 | + self.push("\u{1b}[31m\{tag}\{line}\u{1b}[0m") |
| 103 | + _ => self.push(tagged_line) |
| 104 | + } |
| 105 | + } else { |
| 106 | + self.push(tagged_line) |
| 107 | + } |
| 108 | +} |
0 commit comments