MCPcopy
hub / github.com/mum4k/termdash / Diff

Function Diff

private/faketerm/diff.go:41–111  ·  view source on GitHub ↗

Diff compares the two terminals, returning an empty string if there is not difference. If a difference is found, returns a human readable description of the differences.

(want, got *Terminal)

Source from the content-addressed store, hash-verified

39// difference. If a difference is found, returns a human readable description
40// of the differences.
41func Diff(want, got *Terminal) string {
42 if reflect.DeepEqual(want.BackBuffer(), got.BackBuffer()) {
43 return ""
44 }
45
46 var b strings.Builder
47 b.WriteString("found differences between the two fake terminals.\n")
48 b.WriteString(" got:\n")
49 b.WriteString(got.String())
50 b.WriteString(" want:\n")
51 b.WriteString(want.String())
52 b.WriteString(" diff (unexpected cells highlighted with rune '࿃')\n")
53 b.WriteString(" note - this excludes cell options:\n")
54
55 size := got.Size()
56 var optDiffs []*optDiff
57 cellsDiffer := false
58 for row := 0; row < size.Y; row++ {
59 for col := 0; col < size.X; col++ {
60 p := image.Point{col, row}
61 partial, err := got.BackBuffer().IsPartial(p)
62 if err != nil {
63 panic(fmt.Errorf("unable to determine if point %v is a partial rune: %v", p, err))
64 }
65
66 gotCell := got.BackBuffer()[col][row]
67 wantCell := want.BackBuffer()[col][row]
68 r := gotCell.Rune
69 if r != wantCell.Rune {
70 r = '࿃'
71 cellsDiffer = true
72 } else if r == 0 && !partial {
73 r = ' '
74 }
75 b.WriteRune(r)
76
77 if !reflect.DeepEqual(gotCell.Opts, wantCell.Opts) {
78 optDiffs = append(optDiffs, &optDiff{
79 point: image.Point{col, row},
80 got: gotCell.Opts,
81 want: wantCell.Opts,
82 })
83 }
84 }
85 b.WriteRune('\n')
86 }
87
88 if len(optDiffs) > 0 {
89 b.WriteString(" Found differences in options on some of the cells:\n")
90 for _, od := range optDiffs {
91 if diff := pretty.Compare(od.want, od.got); diff != "" {
92 b.WriteString(fmt.Sprintf("cell %v, diff (-want +got):\n%s\n", od.point, diff))
93 }
94 }
95 }
96
97 if cellsDiffer {
98 b.WriteString(" Found differences in some of the cell runes:\n")

Callers 15

TestRunFunction · 0.92
TestControllerFunction · 0.92
TestNewFunction · 0.92
TestKeyboardFunction · 0.92
TestMouseFunction · 0.92
TestUpdateFunction · 0.92
TestDrawWidgetFunction · 0.92
TestBuilderFunction · 0.92
TestHVFunction · 0.92
TestDiagonalFunction · 0.92
TestDrawFunction · 0.92

Calls 4

BackBufferMethod · 0.80
IsPartialMethod · 0.80
SizeMethod · 0.65
StringMethod · 0.45

Tested by 15

TestRunFunction · 0.74
TestControllerFunction · 0.74
TestNewFunction · 0.74
TestKeyboardFunction · 0.74
TestMouseFunction · 0.74
TestUpdateFunction · 0.74
TestDrawWidgetFunction · 0.74
TestBuilderFunction · 0.74
TestHVFunction · 0.74
TestDiagonalFunction · 0.74
TestDrawFunction · 0.74