chore: upgrade to gods v2

gods v2 uses go generics rather than interfaces which simplifies the
code considerably
This commit is contained in:
Michael Yang 2024-12-21 00:02:50 -08:00
parent d8bab8ea44
commit cb40d60469
5 changed files with 109 additions and 130 deletions

2
go.mod
View file

@ -4,7 +4,6 @@ go 1.23.4
require ( require (
github.com/containerd/console v1.0.3 github.com/containerd/console v1.0.3
github.com/emirpasic/gods v1.18.1
github.com/gin-gonic/gin v1.10.0 github.com/gin-gonic/gin v1.10.0
github.com/golang/protobuf v1.5.4 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
@ -18,6 +17,7 @@ require (
require ( require (
github.com/agnivade/levenshtein v1.1.1 github.com/agnivade/levenshtein v1.1.1
github.com/d4l3k/go-bfloat16 v0.0.0-20211005043715-690c3bdd05f1 github.com/d4l3k/go-bfloat16 v0.0.0-20211005043715-690c3bdd05f1
github.com/emirpasic/gods/v2 v2.0.0-alpha
github.com/google/go-cmp v0.6.0 github.com/google/go-cmp v0.6.0
github.com/mattn/go-runewidth v0.0.14 github.com/mattn/go-runewidth v0.0.14
github.com/nlpodyssey/gopickle v0.3.0 github.com/nlpodyssey/gopickle v0.3.0

4
go.sum
View file

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

View file

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/emirpasic/gods/lists/arraylist" "github.com/emirpasic/gods/v2/lists/arraylist"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
"golang.org/x/term" "golang.org/x/term"
) )
@ -12,9 +12,9 @@ import (
type Buffer struct { type Buffer struct {
DisplayPos int DisplayPos int
Pos int Pos int
Buf *arraylist.List Buf *arraylist.List[rune]
// LineHasSpace is an arraylist of bools to keep track of whether a line has a space at the end // LineHasSpace is an arraylist of bools to keep track of whether a line has a space at the end
LineHasSpace *arraylist.List LineHasSpace *arraylist.List[bool]
Prompt *Prompt Prompt *Prompt
LineWidth int LineWidth int
Width int Width int
@ -33,8 +33,8 @@ func NewBuffer(prompt *Prompt) (*Buffer, error) {
b := &Buffer{ b := &Buffer{
DisplayPos: 0, DisplayPos: 0,
Pos: 0, Pos: 0,
Buf: arraylist.New(), Buf: arraylist.New[rune](),
LineHasSpace: arraylist.New(), LineHasSpace: arraylist.New[bool](),
Prompt: prompt, Prompt: prompt,
Width: width, Width: width,
Height: height, Height: height,
@ -46,19 +46,13 @@ func NewBuffer(prompt *Prompt) (*Buffer, error) {
func (b *Buffer) GetLineSpacing(line int) bool { func (b *Buffer) GetLineSpacing(line int) bool {
hasSpace, _ := b.LineHasSpace.Get(line) hasSpace, _ := b.LineHasSpace.Get(line)
return hasSpace
if hasSpace == nil {
return false
}
return hasSpace.(bool)
} }
func (b *Buffer) MoveLeft() { func (b *Buffer) MoveLeft() {
if b.Pos > 0 { if b.Pos > 0 {
// asserts that we retrieve a rune // asserts that we retrieve a rune
if e, ok := b.Buf.Get(b.Pos - 1); ok { if r, ok := b.Buf.Get(b.Pos - 1); ok {
if r, ok := e.(rune); ok {
rLength := runewidth.RuneWidth(r) rLength := runewidth.RuneWidth(r)
if b.DisplayPos%b.LineWidth == 0 { if b.DisplayPos%b.LineWidth == 0 {
@ -82,7 +76,6 @@ func (b *Buffer) MoveLeft() {
} }
} }
} }
}
func (b *Buffer) MoveLeftWord() { func (b *Buffer) MoveLeftWord() {
if b.Pos > 0 { if b.Pos > 0 {
@ -107,8 +100,7 @@ func (b *Buffer) MoveLeftWord() {
func (b *Buffer) MoveRight() { func (b *Buffer) MoveRight() {
if b.Pos < b.Buf.Size() { if b.Pos < b.Buf.Size() {
if e, ok := b.Buf.Get(b.Pos); ok { if r, ok := b.Buf.Get(b.Pos); ok {
if r, ok := e.(rune); ok {
rLength := runewidth.RuneWidth(r) rLength := runewidth.RuneWidth(r)
b.Pos += 1 b.Pos += 1
hasSpace := b.GetLineSpacing(b.DisplayPos / b.LineWidth) hasSpace := b.GetLineSpacing(b.DisplayPos / b.LineWidth)
@ -128,7 +120,6 @@ func (b *Buffer) MoveRight() {
} }
} }
} }
}
func (b *Buffer) MoveRightWord() { func (b *Buffer) MoveRightWord() {
if b.Pos < b.Buf.Size() { if b.Pos < b.Buf.Size() {
@ -182,12 +173,10 @@ func (b *Buffer) MoveToEnd() {
func (b *Buffer) DisplaySize() int { func (b *Buffer) DisplaySize() int {
sum := 0 sum := 0
for i := range b.Buf.Size() { for i := range b.Buf.Size() {
if e, ok := b.Buf.Get(i); ok { if r, ok := b.Buf.Get(i); ok {
if r, ok := e.(rune); ok {
sum += runewidth.RuneWidth(r) sum += runewidth.RuneWidth(r)
} }
} }
}
return sum return sum
} }
@ -257,11 +246,9 @@ func (b *Buffer) countRemainingLineWidth(place int) int {
for place <= b.LineWidth { for place <= b.LineWidth {
counter += 1 counter += 1
sum += prevLen sum += prevLen
if e, ok := b.Buf.Get(b.Pos + counter); ok { if r, ok := b.Buf.Get(b.Pos + counter); ok {
if r, ok := e.(rune); ok {
place += runewidth.RuneWidth(r) place += runewidth.RuneWidth(r)
prevLen = len(string(r)) prevLen = len(string(r))
}
} else { } else {
break break
} }
@ -346,8 +333,7 @@ func (b *Buffer) drawRemaining() {
func (b *Buffer) Remove() { func (b *Buffer) Remove() {
if b.Buf.Size() > 0 && b.Pos > 0 { if b.Buf.Size() > 0 && b.Pos > 0 {
if e, ok := b.Buf.Get(b.Pos - 1); ok { if r, ok := b.Buf.Get(b.Pos - 1); ok {
if r, ok := e.(rune); ok {
rLength := runewidth.RuneWidth(r) rLength := runewidth.RuneWidth(r)
hasSpace := b.GetLineSpacing(b.DisplayPos/b.LineWidth - 1) hasSpace := b.GetLineSpacing(b.DisplayPos/b.LineWidth - 1)
@ -408,7 +394,6 @@ func (b *Buffer) Remove() {
} }
} }
} }
}
func (b *Buffer) Delete() { func (b *Buffer) Delete() {
if b.Buf.Size() > 0 && b.Pos < b.Buf.Size() { if b.Buf.Size() > 0 && b.Pos < b.Buf.Size() {
@ -536,7 +521,7 @@ func (b *Buffer) StringNM(n, m int) string {
} }
for cnt := n; cnt < m; cnt++ { for cnt := n; cnt < m; cnt++ {
c, _ := b.Buf.Get(cnt) c, _ := b.Buf.Get(cnt)
s += string(c.(rune)) s += string(c)
} }
return s return s
} }

View file

@ -3,16 +3,17 @@ package readline
import ( import (
"bufio" "bufio"
"errors" "errors"
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/emirpasic/gods/lists/arraylist" "github.com/emirpasic/gods/v2/lists/arraylist"
) )
type History struct { type History struct {
Buf *arraylist.List Buf *arraylist.List[string]
Autosave bool Autosave bool
Pos int Pos int
Limit int Limit int
@ -22,7 +23,7 @@ type History struct {
func NewHistory() (*History, error) { func NewHistory() (*History, error) {
h := &History{ h := &History{
Buf: arraylist.New(), Buf: arraylist.New[string](),
Limit: 100, // resizeme Limit: 100, // resizeme
Autosave: true, Autosave: true,
Enabled: true, Enabled: true,
@ -73,14 +74,14 @@ func (h *History) Init() error {
continue continue
} }
h.Add([]rune(line)) h.Add(line)
} }
return nil return nil
} }
func (h *History) Add(l []rune) { func (h *History) Add(s string) {
h.Buf.Add(l) h.Buf.Add(s)
h.Compact() h.Compact()
h.Pos = h.Size() h.Pos = h.Size()
if h.Autosave { if h.Autosave {
@ -101,22 +102,18 @@ func (h *History) Clear() {
h.Buf.Clear() h.Buf.Clear()
} }
func (h *History) Prev() []rune { func (h *History) Prev() (line string) {
var line []rune
if h.Pos > 0 { if h.Pos > 0 {
h.Pos -= 1 h.Pos -= 1
} }
v, _ := h.Buf.Get(h.Pos) line, _ = h.Buf.Get(h.Pos)
line, _ = v.([]rune)
return line return line
} }
func (h *History) Next() []rune { func (h *History) Next() (line string) {
var line []rune
if h.Pos < h.Buf.Size() { if h.Pos < h.Buf.Size() {
h.Pos += 1 h.Pos += 1
v, _ := h.Buf.Get(h.Pos) line, _ = h.Buf.Get(h.Pos)
line, _ = v.([]rune)
} }
return line return line
} }
@ -140,11 +137,8 @@ func (h *History) Save() error {
buf := bufio.NewWriter(f) buf := bufio.NewWriter(f)
for cnt := range h.Size() { for cnt := range h.Size() {
v, _ := h.Buf.Get(cnt) line, _ := h.Buf.Get(cnt)
line, _ := v.([]rune) fmt.Fprintln(buf, line)
if _, err := buf.WriteString(string(line) + "\n"); err != nil {
return err
}
} }
buf.Flush() buf.Flush()
f.Close() f.Close()

View file

@ -120,11 +120,11 @@ func (i *Instance) Readline() (string, error) {
if i.History.Pos == i.History.Size() { if i.History.Pos == i.History.Size() {
currentLineBuf = []rune(buf.String()) currentLineBuf = []rune(buf.String())
} }
buf.Replace(i.History.Prev()) buf.Replace([]rune(i.History.Prev()))
} }
case KeyDown: case KeyDown:
if i.History.Pos < i.History.Size() { if i.History.Pos < i.History.Size() {
buf.Replace(i.History.Next()) buf.Replace([]rune(i.History.Next()))
if i.History.Pos == i.History.Size() { if i.History.Pos == i.History.Size() {
buf.Replace(currentLineBuf) buf.Replace(currentLineBuf)
} }
@ -220,7 +220,7 @@ func (i *Instance) Readline() (string, error) {
case CharEnter, CharCtrlJ: case CharEnter, CharCtrlJ:
output := buf.String() output := buf.String()
if output != "" { if output != "" {
i.History.Add([]rune(output)) i.History.Add(output)
} }
buf.MoveToEnd() buf.MoveToEnd()
fmt.Println() fmt.Println()