Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
go-to-k committed Aug 19, 2024
1 parent 00a79ce commit f0f71a4
Showing 1 changed file with 54 additions and 49 deletions.
103 changes: 54 additions & 49 deletions internal/io/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,24 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
u.Cursor--
}

if _, ok := u.Filtered.Choices[u.Cursor]; ok {
if u.Filtered.Cursor == 0 {
u.Filtered.Cursor = len(u.Filtered.Choices) - 1
} else if u.Filtered.Cursor > 0 {
u.Filtered.Cursor--
}
if _, ok := u.Filtered.Choices[u.Cursor]; !ok {
continue
}
if u.Filtered.Cursor == 0 {
u.Filtered.Cursor = len(u.Filtered.Choices) - 1
} else if u.Filtered.Cursor > 0 {
u.Filtered.Cursor--
}

f := u.Filtered
for {
if f.Prev == nil {
break
}
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
f := u.Filtered
for {
if f.Prev == nil {
break
}
break
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
}
break
}

case "down":
Expand All @@ -106,23 +107,24 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
u.Cursor = 0
}

if _, ok := u.Filtered.Choices[u.Cursor]; ok {
if u.Filtered.Cursor < len(u.Filtered.Choices)-1 {
u.Filtered.Cursor++
} else if u.Filtered.Cursor == len(u.Filtered.Choices)-1 {
u.Filtered.Cursor = 0
}
if _, ok := u.Filtered.Choices[u.Cursor]; !ok {
continue
}
if u.Filtered.Cursor < len(u.Filtered.Choices)-1 {
u.Filtered.Cursor++
} else if u.Filtered.Cursor == len(u.Filtered.Choices)-1 {
u.Filtered.Cursor = 0
}

f := u.Filtered
for {
if f.Prev == nil {
break
}
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
f := u.Filtered
for {
if f.Prev == nil {
break
}
break
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
}
break
}

// select or deselect an item
Expand Down Expand Up @@ -160,20 +162,22 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// clear one character from the keyword
case "backspace":
if len(u.Keyword) > 0 {
u.Keyword = u.Keyword[:len(u.Keyword)-1]
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++
if len(u.Keyword) == 0 {
return u, nil
}

u.Keyword = u.Keyword[:len(u.Keyword)-1]
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++
}

// add a character to the keyword
Expand Down Expand Up @@ -206,15 +210,16 @@ func (u *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

if len(u.Filtered.Choices) != 0 {
f := u.Filtered
for {
if f.Prev == nil {
break
}
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
if len(u.Filtered.Choices) == 0 {
return u, nil
}
f := u.Filtered
for {
if f.Prev == nil {
break
}
f.Prev.Cursor = u.Filtered.Cursor
f = f.Prev
}

}
Expand Down

0 comments on commit f0f71a4

Please sign in to comment.