Skip to content

Commit

Permalink
Refactor removing symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 18, 2023
1 parent 4183502 commit e550719
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions pkg/cli/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
type Editor struct {
History *History
buffer []rune
pos int
}

func NewEditor(history *History) *Editor {
return &Editor{
History: history,
buffer: make([]rune, 0),
pos: 0,
}
}

Expand Down Expand Up @@ -49,32 +51,14 @@ func (ed *Editor) EditRequest(keyStream <-chan keyboard.KeyEvent, initBuffer str
fmt.Print(" ")

ed.buffer = append(ed.buffer, ' ')
ed.pos++
case keyboard.KeyEnter:
fmt.Print("\n")

ed.buffer = append(ed.buffer, '\n')
ed.pos++
case keyboard.KeyBackspace, keyboard.KeyDelete, MacOSDeleteKey:
if len(ed.buffer) == 0 {
continue
}

if ed.buffer[len(ed.buffer)-1] == '\n' {
ed.buffer = ed.buffer[:len(ed.buffer)-1]

fmt.Print(LineUp)

startPrevLine := LastIndexOf(ed.buffer, '\n')
if startPrevLine == -1 {
startPrevLine = 0
} else {
startPrevLine++
}

fmt.Print(string(ed.buffer[startPrevLine:]))
} else {
fmt.Print("\b \b")
ed.buffer = ed.buffer[:len(ed.buffer)-1]
}
ed.removeSymbol()
case keyboard.KeyArrowUp:
historyIndex++
req := ed.History.GetRequst(historyIndex)
Expand All @@ -88,6 +72,7 @@ func (ed *Editor) EditRequest(keyStream <-chan keyboard.KeyEvent, initBuffer str

fmt.Print(req)
ed.buffer = []rune(req)
ed.pos = len(ed.buffer) - 1
case keyboard.KeyArrowDown:
historyIndex--
req := ed.History.GetRequst(historyIndex)
Expand All @@ -101,6 +86,7 @@ func (ed *Editor) EditRequest(keyStream <-chan keyboard.KeyEvent, initBuffer str

fmt.Print(req)
ed.buffer = []rune(req)
ed.pos = len(ed.buffer) - 1
default:
if e.Key > 0 {
continue
Expand All @@ -109,6 +95,7 @@ func (ed *Editor) EditRequest(keyStream <-chan keyboard.KeyEvent, initBuffer str
fmt.Print(string(e.Rune))

ed.buffer = append(ed.buffer, e.Rune)
ed.pos++
}
}

Expand All @@ -126,6 +113,36 @@ func (ed *Editor) clearInput() {
}
}

func (ed *Editor) removeSymbol() {
if ed.pos < 1 || ed.pos > len(ed.buffer) {
return
}

ed.pos--
symbol := ed.buffer[ed.pos]
buffer := ed.buffer[:ed.pos]
if ed.pos < (len(ed.buffer) - 1) {

Check failure on line 124 in pkg/cli/editor.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

only one cuddle assignment allowed before if statement (wsl)
buffer = append(buffer, ed.buffer[ed.pos+1:]...)
}
ed.buffer = buffer

Check failure on line 127 in pkg/cli/editor.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

assignments should only be cuddled with other assignments (wsl)

if symbol == '\n' {

Check failure on line 129 in pkg/cli/editor.go

View workflow job for this annotation

GitHub Actions / tests (1.21.x)

unnecessary leading newline (whitespace)

fmt.Print(LineUp)

startPrevLine := LastIndexOf(ed.buffer, '\n')
if startPrevLine == -1 {
startPrevLine = 0
} else {
startPrevLine++
}

fmt.Print(string(ed.buffer[startPrevLine:]))
} else {
fmt.Print("\b \b")
}
}

func LastIndexOf(buffer []rune, search rune) int {
for i := len(buffer) - 1; i >= 0; i-- {
if buffer[i] == search {
Expand Down

0 comments on commit e550719

Please sign in to comment.