Skip to content

Commit cb40d60

Browse files
committed
chore: upgrade to gods v2
gods v2 uses go generics rather than interfaces which simplifies the code considerably
1 parent d8bab8e commit cb40d60

File tree

5 files changed

+111
-132
lines changed

5 files changed

+111
-132
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.23.4
44

55
require (
66
github.com/containerd/console v1.0.3
7-
github.com/emirpasic/gods v1.18.1
87
github.com/gin-gonic/gin v1.10.0
98
github.com/golang/protobuf v1.5.4 // indirect
109
github.com/google/uuid v1.6.0
@@ -18,6 +17,7 @@ require (
1817
require (
1918
github.com/agnivade/levenshtein v1.1.1
2019
github.com/d4l3k/go-bfloat16 v0.0.0-20211005043715-690c3bdd05f1
20+
github.com/emirpasic/gods/v2 v2.0.0-alpha
2121
github.com/google/go-cmp v0.6.0
2222
github.com/mattn/go-runewidth v0.0.14
2323
github.com/nlpodyssey/gopickle v0.3.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
4242
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4343
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
4444
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
45-
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
46-
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
45+
github.com/emirpasic/gods/v2 v2.0.0-alpha h1:dwFlh8pBg1VMOXWGipNMRt8v96dKAIvBehtCt6OtunU=
46+
github.com/emirpasic/gods/v2 v2.0.0-alpha/go.mod h1:W0y4M2dtBB9U5z3YlghmpuUhiaZT2h6yoeE+C1sCp6A=
4747
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
4848
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
4949
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=

readline/buffer.go

+92-107
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/emirpasic/gods/lists/arraylist"
7+
"github.com/emirpasic/gods/v2/lists/arraylist"
88
"github.com/mattn/go-runewidth"
99
"golang.org/x/term"
1010
)
1111

1212
type Buffer struct {
1313
DisplayPos int
1414
Pos int
15-
Buf *arraylist.List
15+
Buf *arraylist.List[rune]
1616
// LineHasSpace is an arraylist of bools to keep track of whether a line has a space at the end
17-
LineHasSpace *arraylist.List
17+
LineHasSpace *arraylist.List[bool]
1818
Prompt *Prompt
1919
LineWidth int
2020
Width int
@@ -33,8 +33,8 @@ func NewBuffer(prompt *Prompt) (*Buffer, error) {
3333
b := &Buffer{
3434
DisplayPos: 0,
3535
Pos: 0,
36-
Buf: arraylist.New(),
37-
LineHasSpace: arraylist.New(),
36+
Buf: arraylist.New[rune](),
37+
LineHasSpace: arraylist.New[bool](),
3838
Prompt: prompt,
3939
Width: width,
4040
Height: height,
@@ -46,40 +46,33 @@ func NewBuffer(prompt *Prompt) (*Buffer, error) {
4646

4747
func (b *Buffer) GetLineSpacing(line int) bool {
4848
hasSpace, _ := b.LineHasSpace.Get(line)
49-
50-
if hasSpace == nil {
51-
return false
52-
}
53-
54-
return hasSpace.(bool)
49+
return hasSpace
5550
}
5651

5752
func (b *Buffer) MoveLeft() {
5853
if b.Pos > 0 {
5954
// asserts that we retrieve a rune
60-
if e, ok := b.Buf.Get(b.Pos - 1); ok {
61-
if r, ok := e.(rune); ok {
62-
rLength := runewidth.RuneWidth(r)
63-
64-
if b.DisplayPos%b.LineWidth == 0 {
65-
fmt.Print(CursorUp + CursorBOL + CursorRightN(b.Width))
66-
if rLength == 2 {
67-
fmt.Print(CursorLeft)
68-
}
55+
if r, ok := b.Buf.Get(b.Pos - 1); ok {
56+
rLength := runewidth.RuneWidth(r)
6957

70-
line := b.DisplayPos/b.LineWidth - 1
71-
hasSpace := b.GetLineSpacing(line)
72-
if hasSpace {
73-
b.DisplayPos -= 1
74-
fmt.Print(CursorLeft)
75-
}
76-
} else {
77-
fmt.Print(CursorLeftN(rLength))
58+
if b.DisplayPos%b.LineWidth == 0 {
59+
fmt.Print(CursorUp + CursorBOL + CursorRightN(b.Width))
60+
if rLength == 2 {
61+
fmt.Print(CursorLeft)
7862
}
7963

80-
b.Pos -= 1
81-
b.DisplayPos -= rLength
64+
line := b.DisplayPos/b.LineWidth - 1
65+
hasSpace := b.GetLineSpacing(line)
66+
if hasSpace {
67+
b.DisplayPos -= 1
68+
fmt.Print(CursorLeft)
69+
}
70+
} else {
71+
fmt.Print(CursorLeftN(rLength))
8272
}
73+
74+
b.Pos -= 1
75+
b.DisplayPos -= rLength
8376
}
8477
}
8578
}
@@ -107,24 +100,22 @@ func (b *Buffer) MoveLeftWord() {
107100

108101
func (b *Buffer) MoveRight() {
109102
if b.Pos < b.Buf.Size() {
110-
if e, ok := b.Buf.Get(b.Pos); ok {
111-
if r, ok := e.(rune); ok {
112-
rLength := runewidth.RuneWidth(r)
113-
b.Pos += 1
114-
hasSpace := b.GetLineSpacing(b.DisplayPos / b.LineWidth)
115-
b.DisplayPos += rLength
116-
117-
if b.DisplayPos%b.LineWidth == 0 {
118-
fmt.Print(CursorDown + CursorBOL + CursorRightN(len(b.Prompt.prompt())))
119-
} else if (b.DisplayPos-rLength)%b.LineWidth == b.LineWidth-1 && hasSpace {
120-
fmt.Print(CursorDown + CursorBOL + CursorRightN(len(b.Prompt.prompt())+rLength))
121-
b.DisplayPos += 1
122-
} else if b.LineHasSpace.Size() > 0 && b.DisplayPos%b.LineWidth == b.LineWidth-1 && hasSpace {
123-
fmt.Print(CursorDown + CursorBOL + CursorRightN(len(b.Prompt.prompt())))
124-
b.DisplayPos += 1
125-
} else {
126-
fmt.Print(CursorRightN(rLength))
127-
}
103+
if r, ok := b.Buf.Get(b.Pos); ok {
104+
rLength := runewidth.RuneWidth(r)
105+
b.Pos += 1
106+
hasSpace := b.GetLineSpacing(b.DisplayPos / b.LineWidth)
107+
b.DisplayPos += rLength
108+
109+
if b.DisplayPos%b.LineWidth == 0 {
110+
fmt.Print(CursorDown + CursorBOL + CursorRightN(len(b.Prompt.prompt())))
111+
} else if (b.DisplayPos-rLength)%b.LineWidth == b.LineWidth-1 && hasSpace {
112+
fmt.Print(CursorDown + CursorBOL + CursorRightN(len(b.Prompt.prompt())+rLength))
113+
b.DisplayPos += 1
114+
} else if b.LineHasSpace.Size() > 0 && b.DisplayPos%b.LineWidth == b.LineWidth-1 && hasSpace {
115+
fmt.Print(CursorDown + CursorBOL + CursorRightN(len(b.Prompt.prompt())))
116+
b.DisplayPos += 1
117+
} else {
118+
fmt.Print(CursorRightN(rLength))
128119
}
129120
}
130121
}
@@ -182,10 +173,8 @@ func (b *Buffer) MoveToEnd() {
182173
func (b *Buffer) DisplaySize() int {
183174
sum := 0
184175
for i := range b.Buf.Size() {
185-
if e, ok := b.Buf.Get(i); ok {
186-
if r, ok := e.(rune); ok {
187-
sum += runewidth.RuneWidth(r)
188-
}
176+
if r, ok := b.Buf.Get(i); ok {
177+
sum += runewidth.RuneWidth(r)
189178
}
190179
}
191180

@@ -257,11 +246,9 @@ func (b *Buffer) countRemainingLineWidth(place int) int {
257246
for place <= b.LineWidth {
258247
counter += 1
259248
sum += prevLen
260-
if e, ok := b.Buf.Get(b.Pos + counter); ok {
261-
if r, ok := e.(rune); ok {
262-
place += runewidth.RuneWidth(r)
263-
prevLen = len(string(r))
264-
}
249+
if r, ok := b.Buf.Get(b.Pos + counter); ok {
250+
place += runewidth.RuneWidth(r)
251+
prevLen = len(string(r))
265252
} else {
266253
break
267254
}
@@ -346,64 +333,62 @@ func (b *Buffer) drawRemaining() {
346333

347334
func (b *Buffer) Remove() {
348335
if b.Buf.Size() > 0 && b.Pos > 0 {
349-
if e, ok := b.Buf.Get(b.Pos - 1); ok {
350-
if r, ok := e.(rune); ok {
351-
rLength := runewidth.RuneWidth(r)
352-
hasSpace := b.GetLineSpacing(b.DisplayPos/b.LineWidth - 1)
353-
354-
if b.DisplayPos%b.LineWidth == 0 {
355-
// if the user backspaces over the word boundary, do this magic to clear the line
356-
// and move to the end of the previous line
357-
fmt.Print(CursorBOL + ClearToEOL + CursorUp + CursorBOL + CursorRightN(b.Width))
358-
359-
if b.DisplaySize()%b.LineWidth < (b.DisplaySize()-rLength)%b.LineWidth {
360-
b.LineHasSpace.Remove(b.DisplayPos/b.LineWidth - 1)
361-
}
336+
if r, ok := b.Buf.Get(b.Pos - 1); ok {
337+
rLength := runewidth.RuneWidth(r)
338+
hasSpace := b.GetLineSpacing(b.DisplayPos/b.LineWidth - 1)
362339

363-
if hasSpace {
364-
b.DisplayPos -= 1
365-
fmt.Print(CursorLeft)
366-
}
340+
if b.DisplayPos%b.LineWidth == 0 {
341+
// if the user backspaces over the word boundary, do this magic to clear the line
342+
// and move to the end of the previous line
343+
fmt.Print(CursorBOL + ClearToEOL + CursorUp + CursorBOL + CursorRightN(b.Width))
367344

368-
if rLength == 2 {
369-
fmt.Print(CursorLeft + " " + CursorLeftN(2))
370-
} else {
371-
fmt.Print(" " + CursorLeft)
372-
}
373-
} else if (b.DisplayPos-rLength)%b.LineWidth == 0 && hasSpace {
374-
fmt.Print(CursorBOL + ClearToEOL + CursorUp + CursorBOL + CursorRightN(b.Width))
345+
if b.DisplaySize()%b.LineWidth < (b.DisplaySize()-rLength)%b.LineWidth {
346+
b.LineHasSpace.Remove(b.DisplayPos/b.LineWidth - 1)
347+
}
375348

376-
if b.Pos == b.Buf.Size() {
377-
b.LineHasSpace.Remove(b.DisplayPos/b.LineWidth - 1)
378-
}
349+
if hasSpace {
379350
b.DisplayPos -= 1
351+
fmt.Print(CursorLeft)
352+
}
353+
354+
if rLength == 2 {
355+
fmt.Print(CursorLeft + " " + CursorLeftN(2))
380356
} else {
381-
fmt.Print(CursorLeftN(rLength))
382-
for range rLength {
383-
fmt.Print(" ")
384-
}
385-
fmt.Print(CursorLeftN(rLength))
357+
fmt.Print(" " + CursorLeft)
386358
}
359+
} else if (b.DisplayPos-rLength)%b.LineWidth == 0 && hasSpace {
360+
fmt.Print(CursorBOL + ClearToEOL + CursorUp + CursorBOL + CursorRightN(b.Width))
387361

388-
var eraseExtraLine bool
389-
if (b.DisplaySize()-1)%b.LineWidth == 0 || (rLength == 2 && ((b.DisplaySize()-2)%b.LineWidth == 0)) || b.DisplaySize()%b.LineWidth == 0 {
390-
eraseExtraLine = true
362+
if b.Pos == b.Buf.Size() {
363+
b.LineHasSpace.Remove(b.DisplayPos/b.LineWidth - 1)
364+
}
365+
b.DisplayPos -= 1
366+
} else {
367+
fmt.Print(CursorLeftN(rLength))
368+
for range rLength {
369+
fmt.Print(" ")
391370
}
371+
fmt.Print(CursorLeftN(rLength))
372+
}
392373

393-
b.Pos -= 1
394-
b.DisplayPos -= rLength
395-
b.Buf.Remove(b.Pos)
396-
397-
if b.Pos < b.Buf.Size() {
398-
b.drawRemaining()
399-
// this erases a line which is left over when backspacing in the middle of a line and there
400-
// are trailing characters which go over the line width boundary
401-
if eraseExtraLine {
402-
remainingLines := (b.DisplaySize() - b.DisplayPos) / b.LineWidth
403-
fmt.Print(CursorDownN(remainingLines+1) + CursorBOL + ClearToEOL)
404-
place := b.DisplayPos % b.LineWidth
405-
fmt.Print(CursorUpN(remainingLines+1) + CursorRightN(place+len(b.Prompt.prompt())))
406-
}
374+
var eraseExtraLine bool
375+
if (b.DisplaySize()-1)%b.LineWidth == 0 || (rLength == 2 && ((b.DisplaySize()-2)%b.LineWidth == 0)) || b.DisplaySize()%b.LineWidth == 0 {
376+
eraseExtraLine = true
377+
}
378+
379+
b.Pos -= 1
380+
b.DisplayPos -= rLength
381+
b.Buf.Remove(b.Pos)
382+
383+
if b.Pos < b.Buf.Size() {
384+
b.drawRemaining()
385+
// this erases a line which is left over when backspacing in the middle of a line and there
386+
// are trailing characters which go over the line width boundary
387+
if eraseExtraLine {
388+
remainingLines := (b.DisplaySize() - b.DisplayPos) / b.LineWidth
389+
fmt.Print(CursorDownN(remainingLines+1) + CursorBOL + ClearToEOL)
390+
place := b.DisplayPos % b.LineWidth
391+
fmt.Print(CursorUpN(remainingLines+1) + CursorRightN(place+len(b.Prompt.prompt())))
407392
}
408393
}
409394
}
@@ -536,7 +521,7 @@ func (b *Buffer) StringNM(n, m int) string {
536521
}
537522
for cnt := n; cnt < m; cnt++ {
538523
c, _ := b.Buf.Get(cnt)
539-
s += string(c.(rune))
524+
s += string(c)
540525
}
541526
return s
542527
}

readline/history.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package readline
33
import (
44
"bufio"
55
"errors"
6+
"fmt"
67
"io"
78
"os"
89
"path/filepath"
910
"strings"
1011

11-
"github.com/emirpasic/gods/lists/arraylist"
12+
"github.com/emirpasic/gods/v2/lists/arraylist"
1213
)
1314

1415
type History struct {
15-
Buf *arraylist.List
16+
Buf *arraylist.List[string]
1617
Autosave bool
1718
Pos int
1819
Limit int
@@ -22,7 +23,7 @@ type History struct {
2223

2324
func NewHistory() (*History, error) {
2425
h := &History{
25-
Buf: arraylist.New(),
26+
Buf: arraylist.New[string](),
2627
Limit: 100, // resizeme
2728
Autosave: true,
2829
Enabled: true,
@@ -73,14 +74,14 @@ func (h *History) Init() error {
7374
continue
7475
}
7576

76-
h.Add([]rune(line))
77+
h.Add(line)
7778
}
7879

7980
return nil
8081
}
8182

82-
func (h *History) Add(l []rune) {
83-
h.Buf.Add(l)
83+
func (h *History) Add(s string) {
84+
h.Buf.Add(s)
8485
h.Compact()
8586
h.Pos = h.Size()
8687
if h.Autosave {
@@ -101,22 +102,18 @@ func (h *History) Clear() {
101102
h.Buf.Clear()
102103
}
103104

104-
func (h *History) Prev() []rune {
105-
var line []rune
105+
func (h *History) Prev() (line string) {
106106
if h.Pos > 0 {
107107
h.Pos -= 1
108108
}
109-
v, _ := h.Buf.Get(h.Pos)
110-
line, _ = v.([]rune)
109+
line, _ = h.Buf.Get(h.Pos)
111110
return line
112111
}
113112

114-
func (h *History) Next() []rune {
115-
var line []rune
113+
func (h *History) Next() (line string) {
116114
if h.Pos < h.Buf.Size() {
117115
h.Pos += 1
118-
v, _ := h.Buf.Get(h.Pos)
119-
line, _ = v.([]rune)
116+
line, _ = h.Buf.Get(h.Pos)
120117
}
121118
return line
122119
}
@@ -140,11 +137,8 @@ func (h *History) Save() error {
140137

141138
buf := bufio.NewWriter(f)
142139
for cnt := range h.Size() {
143-
v, _ := h.Buf.Get(cnt)
144-
line, _ := v.([]rune)
145-
if _, err := buf.WriteString(string(line) + "\n"); err != nil {
146-
return err
147-
}
140+
line, _ := h.Buf.Get(cnt)
141+
fmt.Fprintln(buf, line)
148142
}
149143
buf.Flush()
150144
f.Close()

0 commit comments

Comments
 (0)