-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsvi.go
200 lines (172 loc) · 4.21 KB
/
csvi.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package sqlbless
import (
"errors"
"io"
"strings"
"github.com/hymkor/csvi"
"github.com/hymkor/csvi/uncsv"
)
type _QuitCsvi struct{}
func (_QuitCsvi) Size() (int, int, error) {
return 80, 25, nil
}
func (_QuitCsvi) Calibrate() error {
return nil
}
func (_QuitCsvi) GetKey() (string, error) {
return "q", nil
}
func (_QuitCsvi) ReadLine(io.Writer, string, string, csvi.Candidate) (string, error) {
return "", nil
}
func (_QuitCsvi) GetFilename(io.Writer, string, string) (string, error) {
return "", nil
}
func (_QuitCsvi) Close() error {
return nil
}
type getKeyAndSize interface {
GetKey() (string, error)
Size() (int, int, error)
}
type _AutoCsvi struct {
Tty getKeyAndSize
}
func (_AutoCsvi) Calibrate() error {
return nil
}
func (ac _AutoCsvi) Size() (int, int, error) {
return ac.Tty.Size()
}
func (ac _AutoCsvi) GetKey() (string, error) {
return ac.Tty.GetKey()
}
func (ac _AutoCsvi) readline() (string, error) {
var buffer strings.Builder
for {
c, err := ac.Tty.GetKey()
if err != nil {
return "", err
}
if c == "\r" || c == "\n" {
return buffer.String(), nil
}
buffer.WriteString(c)
}
}
func (ac _AutoCsvi) ReadLine(io.Writer, string, string, csvi.Candidate) (string, error) {
return ac.readline()
}
func (ac _AutoCsvi) GetFilename(io.Writer, string, string) (string, error) {
return ac.readline()
}
func (_AutoCsvi) Close() error {
return nil
}
const (
titlePrefix = "【"
titleSuffix = "】"
)
func csvPager(title string, ss *Session, automatic bool, csvWriteTo func(pOut io.Writer) error, out io.Writer) error {
cfg := &csvi.Config{
Titles: []string{toOneLine(title, titlePrefix, titleSuffix)},
ReadOnly: true,
}
if automatic {
cfg.Pilot = _QuitCsvi{}
}
_, err := callCsvi(ss, cfg, csvWriteTo, out)
return err
}
func csvEdit(title string, ss *Session, validate func(*csvi.CellValidatedEvent) (string, error), tty getKeyAndSize, csvWriteTo func(pOut io.Writer) error, out io.Writer) (*csvi.Result, error) {
applyChange := false
setNull := func(e *csvi.KeyEventArgs) (*csvi.CommandResult, error) {
if ss.DumpConfig.PrintType {
if e.CursorRow.Index() < 3 {
return &csvi.CommandResult{}, nil
}
} else {
if e.CursorRow.Index() < 1 {
return &csvi.CommandResult{}, nil
}
}
ce := &csvi.CellValidatedEvent{
Text: ss.DumpConfig.Null,
Row: e.CursorRow.Index(),
Col: e.CursorCol,
}
if _, err := validate(ce); err != nil {
return &csvi.CommandResult{Message: err.Error()}, nil
}
e.CursorRow.Replace(e.CursorCol, ss.DumpConfig.Null, &uncsv.Mode{Comma: byte(ss.DumpConfig.Comma)})
return &csvi.CommandResult{}, nil
}
cfg := &csvi.Config{
Titles: []string{
toOneLine(title, titlePrefix, titleSuffix),
"\"c\": Apply changes & quit, \"q\": Discard changes & quit",
},
KeyMap: map[string]func(*csvi.KeyEventArgs) (*csvi.CommandResult, error){
"c": func(app *csvi.KeyEventArgs) (*csvi.CommandResult, error) {
if app.YesNo("Apply changes and quit ? [y/n] ") {
io.WriteString(app, "y\n")
applyChange = true
return &csvi.CommandResult{Quit: true}, nil
}
return &csvi.CommandResult{}, nil
},
"x": setNull,
"d": setNull,
},
OnCellValidated: validate,
}
if tty != nil {
cfg.Pilot = &_AutoCsvi{Tty: tty}
}
result, err := callCsvi(ss, cfg, csvWriteTo, out)
if applyChange {
return result, err
}
return nil, err
}
func toOneLine(s, prefix, suffix string) string {
s = strings.TrimSpace(s)
var buf strings.Builder
buf.WriteString(prefix)
var lastc rune
quote := false
for _, c := range s {
if c <= ' ' {
if lastc > ' ' || quote {
buf.WriteRune(' ')
}
} else {
buf.WriteRune(c)
}
if c == '\'' {
quote = !quote
}
lastc = c
}
buf.WriteString(suffix)
return buf.String()
}
func callCsvi(ss *Session, cfg *csvi.Config, csvWriteTo func(pOut io.Writer) error, out io.Writer) (*csvi.Result, error) {
var err1 error
pIn, pOut := io.Pipe()
go func() {
err1 = csvWriteTo(tee(pOut, ss.spool))
pOut.Close()
}()
cfg.Mode = &uncsv.Mode{Comma: byte(ss.DumpConfig.Comma)}
if ss.DumpConfig.PrintType {
cfg.HeaderLines = 3
} else {
cfg.HeaderLines = 1
}
cfg.FixColumn = true
cfg.ProtectHeader = true
result, err2 := cfg.Edit(pIn, out)
pIn.Close()
return result, errors.Join(err1, err2)
}