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