Skip to content

Commit

Permalink
Fixed candidate movement when up/down
Browse files Browse the repository at this point in the history
Fixed an issue where candidate movement was being reset
when going up/down.

Some inputs have been changed so
that the current value is always added to the candidates.
The values specified by CLI options, etc.,
were not retained in the history.
  • Loading branch information
noborus committed Oct 10, 2023
1 parent 3fb7dba commit efd0832
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
17 changes: 15 additions & 2 deletions oviewer/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package oviewer

import (
"context"
"sync"

"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
Expand Down Expand Up @@ -263,27 +264,37 @@ type Eventer interface {

// candidate represents a input candidate list.
type candidate struct {
mux sync.Mutex
list []string
p int
}

func (c *candidate) toLast(str string) {
if str == "" {
return
}
c.mux.Lock()
defer c.mux.Unlock()
c.list = toLast(c.list, str)
c.p = 0
}

func (c *candidate) toAddTop(str string) {
c.mux.Lock()
defer c.mux.Unlock()
c.list = toAddTop(c.list, str)
c.p = 0
}

func (c *candidate) toAddLast(str string) {
c.mux.Lock()
defer c.mux.Unlock()
c.list = toAddLast(c.list, str)
c.p = 0
}

// up returns the previous candidate.
func (c *candidate) up() string {
c.mux.Lock()
defer c.mux.Unlock()
if len(c.list) == 0 {
return ""
}
Expand All @@ -299,6 +310,8 @@ func (c *candidate) up() string {

// down returns the next candidate.
func (c *candidate) down() string {
c.mux.Lock()
defer c.mux.Unlock()
if len(c.list) == 0 {
return ""
}
Expand Down
7 changes: 6 additions & 1 deletion oviewer/input_delimiter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package oviewer

import "github.com/gdamore/tcell/v2"
import (
"github.com/gdamore/tcell/v2"
)

// setDelimiterMode sets the inputMode to Delimiter.
func (root *Root) setDelimiterMode() {
input := root.input
input.value = ""
input.cursorX = 0

input.DelimiterCandidate.toLast(root.Doc.ColumnDelimiter)

input.Event = newDelimiterEvent(input.DelimiterCandidate)
}

Expand Down
7 changes: 6 additions & 1 deletion oviewer/input_jumptarget.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package oviewer

import "github.com/gdamore/tcell/v2"
import (
"github.com/gdamore/tcell/v2"
)

// setJumpTargetMode sets the inputMode to JumpTarget.
func (root *Root) setJumpTargetMode() {
input := root.input
input.value = ""
input.cursorX = 0

input.JumpTargetCandidate.toLast(root.Doc.JumpTarget)

input.Event = newJumpTargetEvent(input.JumpTargetCandidate)
}

Expand Down
2 changes: 2 additions & 0 deletions oviewer/input_multicolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func (root *Root) setMultiColorMode() {
input.value = ""
input.cursorX = 0

old := root.Doc.MultiColorWords
input.MultiColorCandidate.toLast(strings.Join(old, " "))
list := root.searchCandidates(searchCandidateListLen)
str := strings.Join(list, " ")
input.MultiColorCandidate.toLast(str)
Expand Down
17 changes: 16 additions & 1 deletion oviewer/input_search.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package oviewer

import "github.com/gdamore/tcell/v2"
import (
"github.com/gdamore/tcell/v2"
)

// setSearchMode sets the inputMode to Search.
func (root *Root) setSearchMode() {
input := root.input
input.value = ""
input.cursorX = 0

if root.searcher != nil {
input.SearchCandidate.toLast(root.searcher.String())
}

input.Event = newSearchEvent(input.SearchCandidate)
root.OriginPos = root.Doc.topLN
}
Expand Down Expand Up @@ -69,6 +76,11 @@ func (root *Root) setBackSearchMode() {
input := root.input
input.value = ""
input.cursorX = 0

if root.searcher != nil {
input.SearchCandidate.toLast(root.searcher.String())
}

input.Event = newBackSearchEvent(input.SearchCandidate)
root.OriginPos = root.Doc.topLN
}
Expand Down Expand Up @@ -116,6 +128,9 @@ func (e *eventInputBackSearch) Down(str string) string {

// searchCandidates returns the list of search candidates.
func (root *Root) searchCandidates(n int) []string {
root.input.SearchCandidate.mux.Lock()
defer root.input.SearchCandidate.mux.Unlock()

listLen := len(root.input.SearchCandidate.list)
start := max(0, listLen-n)
return root.input.SearchCandidate.list[start:listLen]
Expand Down
3 changes: 3 additions & 0 deletions oviewer/input_section_delimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ func (root *Root) setSectionDelimiterMode() {
input := root.input
input.value = ""
input.cursorX = 0

input.SectionDelmCandidate.toLast(root.Doc.SectionDelimiter)

input.Event = newSectionDelimiterEvent(input.SectionDelmCandidate)
}

Expand Down
8 changes: 7 additions & 1 deletion oviewer/input_tabwidth.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package oviewer

import "github.com/gdamore/tcell/v2"
import (
"strconv"

"github.com/gdamore/tcell/v2"
)

// setTabWidthMode sets the inputMode to TabWidth.
func (root *Root) setTabWidthMode() {
input := root.input
input.value = ""
input.cursorX = 0

input.TabWidthCandidate.toLast(strconv.Itoa(root.Doc.TabWidth))

input.Event = newTabWidthEvent(input.TabWidthCandidate)
}

Expand Down

0 comments on commit efd0832

Please sign in to comment.