-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
snapshot.go
180 lines (141 loc) · 3.27 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"
"unicode"
"github.com/fatih/color"
"github.com/rivo/tview"
"github.com/sergi/go-diff/diffmatchpatch"
)
var dmp = diffmatchpatch.New()
type Snapshot struct {
id int64
command string
args []string
shell string
shellOpts string
result []byte
start time.Time
end time.Time
exitCode int
errorResult []byte
completed bool
err error
diffPrepared bool
diff []diffmatchpatch.Diff
diffAdditionCount int
diffDeletionCount int
before *Snapshot
finish chan<- struct{}
}
//nolint:lll
// NewSnapshot returns Snapshot object.
func NewSnapshot(id int64, command string, args []string, shell string, shellOpts string, before *Snapshot, finish chan<- struct{}) *Snapshot {
return &Snapshot{
id: id,
command: command,
args: args,
shell: shell,
shellOpts: shellOpts,
before: before,
finish: finish,
}
}
func (s *Snapshot) compareFromBefore() error {
if s.before != nil && !s.before.completed {
return errNotCompletedYet
}
var beforeResult string
if s.before == nil {
beforeResult = ""
} else {
beforeResult = string(s.before.result)
}
s.diff = dmp.DiffCleanupSemantic(dmp.DiffMain(beforeResult, string(s.result), false))
addition := 0
deletion := 0
for _, diff := range s.diff {
//nolint:exhaustive
switch diff.Type {
case diffmatchpatch.DiffInsert:
addition += len(diff.Text)
case diffmatchpatch.DiffDelete:
deletion += len(diff.Text)
}
}
s.diffAdditionCount = addition
s.diffDeletionCount = deletion
s.diffPrepared = true
return nil
}
func (s *Snapshot) prepareCommand(commands []string) *exec.Cmd {
var command *exec.Cmd
if runtime.GOOS == "windows" {
cmdStr := strings.Join(commands, " ")
compSec := os.Getenv("COMSPEC")
command = exec.Command(compSec, "/c", cmdStr)
} else {
var args []string
args = append(args, strings.Fields(s.shellOpts)...)
args = append(args, "-c")
args = append(args, strings.Join(commands, " "))
command = exec.Command(s.shell, args...) //nolint:gosec
}
return command
}
func isWhiteString(str string) bool {
for _, c := range str {
if !unicode.IsSpace(c) {
return false
}
}
return true
}
func (s *Snapshot) render(w io.Writer, isShowDiff bool, query string) error {
src := string(s.result)
if isWhiteString(src) {
src = string(s.errorResult)
_, err := io.WriteString(w, fmt.Sprintf(`[red]%s[-:-:-]`, src))
return err
}
if isShowDiff {
var err error
if !s.diffPrepared {
err = s.compareFromBefore()
}
if err == nil {
src = DiffPrettyText(s.diff)
}
}
if query != "" {
src = strings.ReplaceAll(src, query, color.New(color.BgYellow, color.FgBlack).Sprintf(query))
}
src = tview.Escape(src)
_, err := io.Copy(tview.ANSIWriter(w), strings.NewReader(src))
return err
}
func DiffPrettyText(diffs []diffmatchpatch.Diff) string {
var buff bytes.Buffer
for _, diff := range diffs {
text := diff.Text
switch diff.Type {
case diffmatchpatch.DiffInsert:
for _, c := range text {
if unicode.IsSpace(c) {
_, _ = buff.WriteRune(c)
} else {
_, _ = buff.WriteString(color.New(color.BgGreen).Sprintf(string(c)))
}
}
case diffmatchpatch.DiffEqual:
_, _ = buff.WriteString(text)
}
}
return buff.String()
}