String outputs a previously diff'd set of strings, showing differences between them, highlighted by colors. The output format of this function is NOT guaranteed consistent, and may be changed at any time by the library authors. It's meant solely for human consumption.
(diffs Differences)
| 154 | // changed at any time by the library authors. It's meant solely for human |
| 155 | // consumption. |
| 156 | func (c *OutputFormat) String(diffs Differences) string { |
| 157 | var buf bytes.Buffer |
| 158 | count := 0 |
| 159 | fmt.Fprintf(&buf, "%s", c.start) |
| 160 | fmt.Fprintf(&buf, "00000000 ") |
| 161 | for i := 0; i < len(diffs); i++ { |
| 162 | diff := diffs[i] |
| 163 | color := c.color(diff) |
| 164 | reset := "" |
| 165 | if color != "" { |
| 166 | reset = c.reset |
| 167 | } |
| 168 | fmt.Fprint(&buf, color) |
| 169 | for _, b := range diff.From { |
| 170 | fmt.Fprintf(&buf, " %02x", b) |
| 171 | count++ |
| 172 | switch count % 16 { |
| 173 | case 0: |
| 174 | fmt.Fprintf(&buf, "%v\n%08x%v ", reset, count, color) |
| 175 | case 8: |
| 176 | fmt.Fprintf(&buf, " ") |
| 177 | } |
| 178 | } |
| 179 | fmt.Fprint(&buf, reset) |
| 180 | } |
| 181 | fmt.Fprintf(&buf, "\n\n00000000 ") |
| 182 | count = 0 |
| 183 | for i := 0; i < len(diffs); i++ { |
| 184 | diff := diffs[i] |
| 185 | str := diff.From |
| 186 | if diff.Replace { |
| 187 | str = diff.To |
| 188 | } |
| 189 | color := c.color(diff) |
| 190 | reset := "" |
| 191 | if color != "" { |
| 192 | reset = c.reset |
| 193 | } |
| 194 | fmt.Fprint(&buf, color) |
| 195 | for _, b := range str { |
| 196 | fmt.Fprintf(&buf, " %02x", b) |
| 197 | count++ |
| 198 | switch count % 16 { |
| 199 | case 0: |
| 200 | fmt.Fprintf(&buf, "%v\n%08x%v ", reset, count, color) |
| 201 | case 8: |
| 202 | fmt.Fprintf(&buf, " ") |
| 203 | } |
| 204 | } |
| 205 | fmt.Fprint(&buf, reset) |
| 206 | } |
| 207 | fmt.Fprint(&buf, "\n") |
| 208 | fmt.Fprintf(&buf, "%s", c.finish) |
| 209 | return buf.String() |
| 210 | } |