Skip to content

Commit 4412f70

Browse files
committed
adt + formatters
1 parent f106870 commit 4412f70

File tree

10 files changed

+187
-238
lines changed

10 files changed

+187
-238
lines changed

README.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,73 @@ A library for diff-ing strings.
88
"app.tulz" %%% "stringdiff" % "0.1.1"
99
```
1010

11+
### Usage
12+
13+
#### Output with ANSI colors:
1114

1215
```scala
13-
import app.tulz.diff.StringDiff
16+
import app.tulz.diff._
1417

1518
StringDiff("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")
1619
// or
17-
StringDiff.default("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")
20+
StringDiff.withFormat("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")(AnsiColorDiffFormat)
1821
```
1922

2023
![screenshot 1](doc/images/screenshot1.png)
2124

25+
#### Output without colors
26+
2227
```scala
23-
import app.tulz.diff.StringDiff
28+
import app.tulz.diff._
2429

25-
StringDiff.xml("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")
30+
StringDiff.text("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")
31+
// or
32+
StringDiff.withFormat("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")(TextDiffFormat)
2633
```
2734

28-
```xml
29-
<diff><no-match><expected><empty/></expected><actual>prefix </actual></no-match><match>common1 common2 </match><no-match><expected>inside2</expected><actual>inside1</actual></no-match><match> common3 </match><no-match><expected>suffix</expected><actual>common4</actual></no-match></diff>
35+
```
36+
[prefix |∅]common1 common2 [inside1 |inside2 ]common3 [∅|suffix]
3037
```
3138

39+
#### Raw AST
3240

3341
```scala
42+
import app.tulz.diff.StringDiff
3443

44+
StringDiff.raw("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix") // List[DiffBlock]
45+
```
3546

36-
import scala.Console._
37-
38-
val diff = new StringDiff(
39-
beforeAll = "",
40-
beforeNoMatch = "[",
41-
beforeExpected = s"expected: ${YELLOW}",
42-
afterExpected = RESET,
43-
between = ", ",
44-
beforeActual = s"actual: ${RED}",
45-
afterActual = RESET,
46-
empty = s"${MAGENTA}empty${RESET}",
47-
afterNoMatch = "]",
48-
beforeMatch = GREEN,
49-
afterMatch = RESET,
50-
afterAll = ""
47+
```scala
48+
List(
49+
Extra(List("prefix", " ")),
50+
Match(List("common1", " ", "common2", " ")),
51+
Different(List("inside1", " "),List("inside2", " ")),
52+
Match(List("common3", " ")),
53+
Missing(List("suffix"))
5154
)
55+
```
5256

53-
diff("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 common3 suffix")
57+
#### Custom format
5458

55-
// custom
59+
```scala
60+
import app.tulz.diff.StringDiff
61+
import scala.Console._
62+
63+
val customFormat: DiffFormat[String] = (diff: List[DiffBlock]) =>
64+
diff.map {
65+
case DiffBlock.Match(m) => m.mkString
66+
case DiffBlock.Missing(expected) => s"[missing: ${YELLOW}${expected.mkString}${RESET}]"
67+
case DiffBlock.Extra(actual) => s"[extra: ${RED}${actual.mkString}${RESET}]"
68+
case DiffBlock.Different(actual, expected) => s"[expected: ${YELLOW}${expected.mkString}${RESET}, actual: ${RED}${actual.mkString}${RESET}]"
69+
}.mkString
70+
71+
StringDiff.withFormat("prefix common1 common2 inside1 common3 common4", "common1 common2 inside2 inside3 common3 suffix")(customFormat)
72+
StringDiff.withFormat("common1 common2 inside1 inside2 common3 common4 suffix", "prefix common1 common2 inside3 common3")(customFormat)
5673
```
74+
5775
![screenshot 2](doc/images/screenshot2.png)
5876

59-
### More examples
77+
### Examples
6078

6179
```
6280
token1 token2 token3

doc/images/screenshot2.png

-6.2 KB
Binary file not shown.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package app.tulz.diff
2+
3+
import scala.Console._
4+
5+
object AnsiColorDiffFormat extends DiffFormat[String] {
6+
7+
def apply(diff: List[DiffBlock]): String = {
8+
s"${RESET}${diff.map {
9+
case DiffBlock.Match(m) => s"${UNDERLINED}${m.mkString}${RESET}"
10+
case DiffBlock.Missing(expected) => s"[∅|${YELLOW}${UNDERLINED}${expected.mkString}${RESET}]"
11+
case DiffBlock.Extra(actual) => s"[${RED}${UNDERLINED}${actual.mkString}${RESET}|∅]"
12+
case DiffBlock.Different(actual, expected) => s"[${RED}${UNDERLINED}${actual.mkString}${RESET}|${YELLOW}${UNDERLINED}${expected.mkString}${RESET}]"
13+
}.mkString}"
14+
15+
}
16+
}

stringdiff/src/main/scala/app/tulz/diff/DiffBlock.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ sealed trait DiffBlock extends Product with Serializable
55
object DiffBlock {
66

77
final case class Match(s: List[String]) extends DiffBlock
8-
final case class NoMatch(
8+
9+
final case class Different(
910
actual: List[String],
1011
expected: List[String]
1112
) extends DiffBlock
13+
1214
final case class Missing(
1315
expected: List[String]
1416
) extends DiffBlock
17+
1518
final case class Extra(
1619
actual: List[String]
1720
) extends DiffBlock
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package app.tulz.diff
2+
3+
trait DiffFormat[Out] extends (List[DiffBlock] => Out)
4+
5+
object DiffFormat {
6+
7+
val ansi: DiffFormat[String] = AnsiColorDiffFormat
8+
9+
}

0 commit comments

Comments
 (0)