Skip to content

Commit

Permalink
refactor for runes
Browse files Browse the repository at this point in the history
  • Loading branch information
go-to-k committed Aug 22, 2024
1 parent 6097636 commit e0c3464
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions internal/io/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

f := u.Filtered
for {
if f.Prev == nil {
break
}
for f.Prev != nil {
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
}
Expand Down Expand Up @@ -117,10 +114,7 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

f := u.Filtered
for {
if f.Prev == nil {
break
}
for f.Prev != nil {
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
}
Expand Down Expand Up @@ -165,24 +159,12 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// clear one character from the keyword
case tea.KeyBackspace:
if len(u.Keyword) == 0 {
return u, nil
}
u.backspace()

keywordRunes := []rune(u.Keyword)
keywordRunes = keywordRunes[:len(keywordRunes)-1]
u.Keyword = string(keywordRunes)
u.Filtered = u.Filtered.Prev
cnt := 0
for i := range u.Choices {
if _, ok := u.Filtered.Choices[i]; !ok {
continue
}
if cnt == u.Filtered.Cursor {
u.Cursor = i
break
}
cnt++
// clear the keyword
case tea.KeyCtrlW:
for u.Keyword != "" {
u.backspace()
}

// add a character to the keyword
Expand Down Expand Up @@ -216,6 +198,28 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return u, nil
}

func (u *UI) backspace() {
if len(u.Keyword) == 0 {
return
}

keywordRunes := []rune(u.Keyword)
keywordRunes = keywordRunes[:len(keywordRunes)-1]
u.Keyword = string(keywordRunes)
u.Filtered = u.Filtered.Prev
cnt := 0
for i := range u.Choices {
if _, ok := u.Filtered.Choices[i]; !ok {
continue
}
if cnt == u.Filtered.Cursor {
u.Cursor = i
break
}
cnt++
}
}

func (u *UI) addCharacter(c string) {
u.Keyword += c
u.Filtered = &Filtered{
Expand Down Expand Up @@ -250,10 +254,7 @@ func (u *UI) addCharacter(c string) {
return
}
f := u.Filtered
for {
if f.Prev == nil {
break
}
for f.Prev != nil {
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
}
Expand Down

0 comments on commit e0c3464

Please sign in to comment.