Skip to content

Commit 240f4ce

Browse files
authored
Fixes to inputrc & prompt sequences (#59)
* Port fixes from xo/inputrc + little fixes/formats - Lint inputrc code for linters to be nicer. - Import formatting. - Remove old dependencies * Try to re-run Windows tests * Fix escaped sequences in prompt mode status * Little formattings
1 parent d0e805e commit 240f4ce

File tree

8 files changed

+340
-232
lines changed

8 files changed

+340
-232
lines changed

emacs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"strings"
88
"unicode"
99

10+
"github.com/rivo/uniseg"
11+
1012
"github.com/reeflective/readline/inputrc"
1113
"github.com/reeflective/readline/internal/color"
1214
"github.com/reeflective/readline/internal/completion"
1315
"github.com/reeflective/readline/internal/keymap"
1416
"github.com/reeflective/readline/internal/strutil"
1517
"github.com/reeflective/readline/internal/term"
16-
"github.com/rivo/uniseg"
1718
)
1819

1920
// standardCommands returns all standard/emacs commands.

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/reeflective/readline
33
go 1.21
44

55
require (
6-
github.com/google/go-cmp v0.5.8
76
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91
87
golang.org/x/sys v0.8.0
98
golang.org/x/term v0.8.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
2-
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
31
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
42
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
53
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91 h1:tnebWN09GYg9OLPss1KXj8txwZc6X6uMr6VFdcGNbHw=

inputrc/inputrc_test.go

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ import (
99
"path"
1010
"path/filepath"
1111
"regexp"
12+
"sort"
1213
"testing"
1314
"unicode"
14-
15-
"github.com/google/go-cmp/cmp"
16-
"golang.org/x/exp/maps"
17-
"golang.org/x/exp/slices"
1815
)
1916

2017
const delimiter = "####----####\n"
2118

22-
func TestConfig(t *testing.T) {
19+
func TestConfig(_ *testing.T) {
2320
var _ Handler = NewDefaultConfig()
2421
}
2522

@@ -82,14 +79,14 @@ func TestEncontrolDecontrol(t *testing.T) {
8279
{'M', '\r'},
8380
}
8481

85-
for i, test := range tests {
86-
c := Encontrol(test.d)
87-
if exp := test.e; c != exp {
88-
t.Errorf("test %d expected %c==%c", i, exp, c)
82+
for idx, test := range tests {
83+
ctrl := Encontrol(test.d)
84+
if exp := test.e; ctrl != exp {
85+
t.Errorf("test %d expected %c==%c", idx, exp, ctrl)
8986
}
90-
c = Decontrol(test.e)
91-
if exp := unicode.ToUpper(test.d); c != exp {
92-
t.Errorf("test %d expected %c==%c", i, exp, c)
87+
ctrl = Decontrol(test.e)
88+
if exp := unicode.ToUpper(test.d); ctrl != exp {
89+
t.Errorf("test %d expected %c==%c", idx, exp, ctrl)
9390
}
9491
}
9592
}
@@ -142,38 +139,39 @@ func TestDecodeKey(t *testing.T) {
142139
{"Meta-tab", "\x1b\t"},
143140
{"Control-Meta-v", string(Encontrol(Enmeta('v')))},
144141
}
145-
for i, test := range tests {
142+
for idx, test := range tests {
146143
r := []rune(test.s)
147-
v, _, err := decodeKey(r, 0, len(r))
144+
val, _, err := decodeKey(r, 0, len(r))
148145
if err != nil {
149146
t.Fatalf("expected no error, got: %v", err)
150147
}
151148
// FIXME: need more tests and stuff, and this skip here is just to
152149
// quiet errors
153-
if i == 3 || i == 4 {
150+
if idx == 3 || idx == 4 {
154151
continue
155152
}
156-
if s, exp := string(v), test.exp; s != exp {
157-
t.Errorf("test %d expected %q==%q", i, exp, s)
153+
if s, exp := val, test.exp; s != exp {
154+
t.Errorf("test %d expected %q==%q", idx, exp, s)
158155
}
159156
}
160157
}
161158

162159
func newConfig() (*Config, map[string][]string) {
163160
cfg := NewDefaultConfig(WithConfigReadFileFunc(readTestdata))
164-
m := make(map[string][]string)
161+
keys := make(map[string][]string)
165162
cfg.Funcs["$custom"] = func(k, v string) error {
166-
m[k] = append(m[k], v)
163+
keys[k] = append(keys[k], v)
167164
return nil
168165
}
169166
cfg.Funcs[""] = func(k, v string) error {
170-
m[k] = append(m[k], v)
167+
keys[k] = append(keys[k], v)
171168
return nil
172169
}
173-
return cfg, m
170+
return cfg, keys
174171
}
175172

176173
func readTest(t *testing.T, name string) [][]byte {
174+
t.Helper()
177175
buf, err := testdata.ReadFile(name)
178176
if err != nil {
179177
t.Fatalf("expected no error, got: %v", err)
@@ -182,32 +180,34 @@ func readTest(t *testing.T, name string) [][]byte {
182180
}
183181

184182
func check(t *testing.T, exp []byte, cfg *Config, m map[string][]string, err error) {
183+
t.Helper()
185184
res := buildResult(t, exp, cfg, m, err)
186-
if diff := cmp.Diff(string(exp), string(res)); diff != "" {
187-
t.Errorf("result does not equal expected:\n%s", diff)
185+
if !bytes.Equal(exp, res) {
186+
t.Errorf("result does not equal expected:\n%s\ngot:\n%s", string(res), string(res))
188187
}
189188
}
190189

191190
func buildOpts(t *testing.T, buf []byte) []Option {
191+
t.Helper()
192192
lines := bytes.Split(bytes.TrimSpace(buf), []byte{'\n'})
193193
var opts []Option
194194
for i := 0; i < len(lines); i++ {
195195
line := bytes.TrimSpace(lines[i])
196-
j := bytes.Index(line, []byte{':'})
197-
if j == -1 {
196+
pos := bytes.Index(line, []byte{':'})
197+
if pos == -1 {
198198
t.Fatalf("invalid line %d: %q", i+1, string(line))
199199
}
200-
switch k := string(bytes.TrimSpace(line[:j])); k {
200+
switch k := string(bytes.TrimSpace(line[:pos])); k {
201201
case "haltOnErr":
202-
opts = append(opts, WithHaltOnErr(parseBool(t, line[j+1:])))
202+
opts = append(opts, WithHaltOnErr(parseBool(t, line[pos+1:])))
203203
case "strict":
204-
opts = append(opts, WithStrict(parseBool(t, line[j+1:])))
204+
opts = append(opts, WithStrict(parseBool(t, line[pos+1:])))
205205
case "app":
206-
opts = append(opts, WithApp(string(bytes.TrimSpace(line[j+1:]))))
206+
opts = append(opts, WithApp(string(bytes.TrimSpace(line[pos+1:]))))
207207
case "term":
208-
opts = append(opts, WithTerm(string(bytes.TrimSpace(line[j+1:]))))
208+
opts = append(opts, WithTerm(string(bytes.TrimSpace(line[pos+1:]))))
209209
case "mode":
210-
opts = append(opts, WithMode(string(bytes.TrimSpace(line[j+1:]))))
210+
opts = append(opts, WithMode(string(bytes.TrimSpace(line[pos+1:]))))
211211
default:
212212
t.Fatalf("unknown param %q", k)
213213
}
@@ -216,77 +216,90 @@ func buildOpts(t *testing.T, buf []byte) []Option {
216216
}
217217

218218
func buildResult(t *testing.T, exp []byte, cfg *Config, custom map[string][]string, err error) []byte {
219+
t.Helper()
219220
m := errRE.FindSubmatch(exp)
220221
switch {
221222
case err != nil && m == nil:
222223
t.Fatalf("expected no error, got: %v", err)
223224
case err != nil:
224-
s := string(m[1])
225-
re, reErr := regexp.Compile(s)
225+
sub := string(m[1])
226+
re, reErr := regexp.Compile(sub)
226227
if reErr != nil {
227-
t.Fatalf("could not compile regexp %q: %v", s, reErr)
228+
t.Fatalf("could not compile regexp %q: %v", sub, reErr)
228229
return nil
229230
}
230231
if !re.MatchString(err.Error()) {
231-
t.Errorf("expected error %q, got: %v", s, err)
232+
t.Errorf("expected error %q, got: %v", sub, err)
232233
}
233234
t.Logf("matched error %q", err)
234235
return exp
235236
}
236237
buf := new(bytes.Buffer)
237238
// add vars
238239
dv := DefaultVars()
239-
vv := make(map[string]interface{})
240+
vars := make(map[string]interface{})
240241
for k, v := range cfg.Vars {
241242
if dv[k] != v {
242-
vv[k] = v
243+
vars[k] = v
243244
}
244245
}
245-
if len(vv) != 0 {
246+
if len(vars) != 0 {
246247
fmt.Fprintln(buf, "vars:")
247-
keys := maps.Keys(vv)
248-
slices.Sort(keys)
248+
var keys []string
249+
for key := range vars {
250+
keys = append(keys, key)
251+
}
252+
sort.Strings(keys)
249253
for _, k := range keys {
250-
fmt.Fprintf(buf, " %s: %v\n", k, vv[k])
254+
fmt.Fprintf(buf, " %s: %v\n", k, vars[k])
251255
}
252256
}
253257
// add binds
254-
db := DefaultBinds()
255-
vb := make(map[string]map[string]string)
258+
defaults := DefaultBinds()
259+
parsed := make(map[string]map[string]string)
256260
for k := range cfg.Binds {
257-
vb[k] = make(map[string]string)
261+
parsed[k] = make(map[string]string)
258262
}
259263
count := 0
260264
for k, m := range cfg.Binds {
261265
for j, v := range m {
262-
if db[k][j] != v {
266+
if defaults[k][j] != v {
263267
if v.Macro {
264-
vb[k][j] = `"` + EscapeMacro(v.Action) + `"`
268+
parsed[k][j] = `"` + EscapeMacro(v.Action) + `"`
265269
} else {
266-
vb[k][j] = Escape(v.Action)
270+
parsed[k][j] = Escape(v.Action)
267271
}
268272
count++
269273
}
270274
}
271275
}
272276
if count != 0 {
273277
fmt.Fprintln(buf, "binds:")
274-
keymaps := maps.Keys(vb)
275-
slices.Sort(keymaps)
278+
var keymaps []string
279+
for key := range parsed {
280+
keymaps = append(keymaps, key)
281+
}
282+
sort.Strings(keymaps)
276283
for _, k := range keymaps {
277-
if len(vb[k]) != 0 {
284+
if len(parsed[k]) != 0 {
278285
fmt.Fprintf(buf, " %s:\n", k)
279-
binds := maps.Keys(vb[k])
280-
slices.Sort(binds)
286+
var binds []string
287+
for key := range parsed[k] {
288+
binds = append(binds, key)
289+
}
290+
sort.Strings(binds)
281291
for _, j := range binds {
282-
fmt.Fprintf(buf, " %s: %s\n", Escape(j), vb[k][j])
292+
fmt.Fprintf(buf, " %s: %s\n", Escape(j), parsed[k][j])
283293
}
284294
}
285295
}
286296
}
287297
if len(custom) != 0 {
288-
types := maps.Keys(custom)
289-
slices.Sort(types)
298+
var types []string
299+
for key := range custom {
300+
types = append(types, key)
301+
}
302+
sort.Strings(types)
290303
for _, typ := range types {
291304
if len(custom[typ]) != 0 {
292305
fmt.Fprintf(buf, "%s:\n", typ)
@@ -303,13 +316,14 @@ func buildResult(t *testing.T, exp []byte, cfg *Config, custom map[string][]stri
303316
var errRE = regexp.MustCompile(`(?im)^\s*error:\s+(.*)$`)
304317

305318
func parseBool(t *testing.T, buf []byte) bool {
306-
switch s := string(bytes.TrimSpace(buf)); s {
319+
t.Helper()
320+
switch val := string(bytes.TrimSpace(buf)); val {
307321
case "true":
308322
return true
309323
case "false":
310324
return false
311325
default:
312-
t.Fatalf("unknown bool value %q", s)
326+
t.Fatalf("unknown bool value %q", val)
313327
}
314328
return false
315329
}

0 commit comments

Comments
 (0)