Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to select line index of input feed and to output cursor line index #1705

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func Run(opts *Options, revision string) {
snapshot, _ := chunkList.Snapshot()
matcher.Reset(snapshot, terminal.Input(), true, !reading, sort)
delay = false
terminal.scy = terminal.cy

case EvtSearchProgress:
switch val := value.(type) {
Expand Down
10 changes: 10 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ type Options struct {
JumpLabels string
Prompt string
Query string
SelIdx int
PrintSelIdx bool
Select1 bool
Exit0 bool
Filter *string
Expand Down Expand Up @@ -230,6 +232,8 @@ func defaultOptions() *Options {
JumpLabels: defaultJumpLabels,
Prompt: "> ",
Query: "",
SelIdx: 0,
PrintSelIdx: false,
Select1: false,
Exit0: false,
Filter: nil,
Expand Down Expand Up @@ -1112,6 +1116,10 @@ func parseOptions(opts *Options, allArgs []string) {
opts.PrintQuery = true
case "--no-print-query":
opts.PrintQuery = false
case "--print-sel-idx":
opts.PrintSelIdx = true
case "--no-print-sel-idx":
opts.PrintSelIdx = false
case "--prompt":
opts.Prompt = nextString(allArgs, &i, "prompt string required")
case "--sync":
Expand Down Expand Up @@ -1174,6 +1182,8 @@ func parseOptions(opts *Options, allArgs []string) {
opts.FuzzyAlgo = parseAlgo(value)
} else if match, value := optString(arg, "-q", "--query="); match {
opts.Query = value
} else if match, value := optString(arg, "--sel-idx="); match {
opts.SelIdx = atoi(value)
} else if match, value := optString(arg, "-f", "--filter="); match {
opts.Filter = &value
} else if match, value := optString(arg, "-d", "--delimiter="); match {
Expand Down
246 changes: 127 additions & 119 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,70 +56,72 @@ var emptyLine = itemLine{}

// Terminal represents terminal input/output
type Terminal struct {
initDelay time.Duration
inlineInfo bool
prompt string
promptLen int
queryLen [2]int
layout layoutType
fullscreen bool
hscroll bool
hscrollOff int
wordRubout string
wordNext string
cx int
cy int
offset int
xoffset int
yanked []rune
input []rune
multi bool
sort bool
toggleSort bool
delimiter Delimiter
expect map[int]string
keymap map[int][]action
pressed string
printQuery bool
history *History
cycle bool
header []string
header0 []string
ansi bool
tabstop int
margin [4]sizeSpec
strong tui.Attr
unicode bool
bordered bool
cleanExit bool
border tui.Window
window tui.Window
pborder tui.Window
pwindow tui.Window
count int
progress int
reading bool
success bool
jumping jumpMode
jumpLabels string
printer func(string)
merger *Merger
selected map[int32]selectedItem
version int64
reqBox *util.EventBox
preview previewOpts
previewer previewer
previewBox *util.EventBox
eventBox *util.EventBox
mutex sync.Mutex
initFunc func()
prevLines []itemLine
suppress bool
startChan chan bool
killChan chan int
slab *util.Slab
theme *tui.ColorTheme
tui tui.Renderer
initDelay time.Duration
inlineInfo bool
prompt string
promptLen int
queryLen [2]int
layout layoutType
fullscreen bool
hscroll bool
hscrollOff int
wordRubout string
wordNext string
cx int
cy int
scy int
offset int
xoffset int
yanked []rune
input []rune
multi bool
sort bool
toggleSort bool
delimiter Delimiter
expect map[int]string
keymap map[int][]action
pressed string
printQuery bool
printSelIdx bool
history *History
cycle bool
header []string
header0 []string
ansi bool
tabstop int
margin [4]sizeSpec
strong tui.Attr
unicode bool
bordered bool
cleanExit bool
border tui.Window
window tui.Window
pborder tui.Window
pwindow tui.Window
count int
progress int
reading bool
success bool
jumping jumpMode
jumpLabels string
printer func(string)
merger *Merger
selected map[int32]selectedItem
version int64
reqBox *util.EventBox
preview previewOpts
previewer previewer
previewBox *util.EventBox
eventBox *util.EventBox
mutex sync.Mutex
initFunc func()
prevLines []itemLine
suppress bool
startChan chan bool
killChan chan int
slab *util.Slab
theme *tui.ColorTheme
tui tui.Renderer
}

type selectedItem struct {
Expand Down Expand Up @@ -368,60 +370,62 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
wordNext = fmt.Sprintf("[^%s]%s|(.$)", sep, sep)
}
t := Terminal{
initDelay: delay,
inlineInfo: opts.InlineInfo,
queryLen: [2]int{0, 0},
layout: opts.Layout,
fullscreen: fullscreen,
hscroll: opts.Hscroll,
hscrollOff: opts.HscrollOff,
wordRubout: wordRubout,
wordNext: wordNext,
cx: len(input),
cy: 0,
offset: 0,
xoffset: 0,
yanked: []rune{},
input: input,
multi: opts.Multi,
sort: opts.Sort > 0,
toggleSort: opts.ToggleSort,
delimiter: opts.Delimiter,
expect: opts.Expect,
keymap: opts.Keymap,
pressed: "",
printQuery: opts.PrintQuery,
history: opts.History,
margin: opts.Margin,
unicode: opts.Unicode,
bordered: opts.Bordered,
cleanExit: opts.ClearOnExit,
strong: strongAttr,
cycle: opts.Cycle,
header: header,
header0: header,
ansi: opts.Ansi,
tabstop: opts.Tabstop,
reading: true,
success: true,
jumping: jumpDisabled,
jumpLabels: opts.JumpLabels,
printer: opts.Printer,
merger: EmptyMerger,
selected: make(map[int32]selectedItem),
reqBox: util.NewEventBox(),
preview: opts.Preview,
previewer: previewer{"", 0, 0, previewBox != nil && !opts.Preview.hidden, false},
previewBox: previewBox,
eventBox: eventBox,
mutex: sync.Mutex{},
suppress: true,
slab: util.MakeSlab(slab16Size, slab32Size),
theme: opts.Theme,
startChan: make(chan bool, 1),
killChan: make(chan int),
tui: renderer,
initFunc: func() { renderer.Init() }}
initDelay: delay,
inlineInfo: opts.InlineInfo,
queryLen: [2]int{0, 0},
layout: opts.Layout,
fullscreen: fullscreen,
hscroll: opts.Hscroll,
hscrollOff: opts.HscrollOff,
wordRubout: wordRubout,
wordNext: wordNext,
cx: len(input),
cy: opts.SelIdx,
scy: opts.SelIdx,
offset: 0,
xoffset: 0,
yanked: []rune{},
input: input,
multi: opts.Multi,
sort: opts.Sort > 0,
toggleSort: opts.ToggleSort,
delimiter: opts.Delimiter,
expect: opts.Expect,
keymap: opts.Keymap,
pressed: "",
printQuery: opts.PrintQuery,
printSelIdx: opts.PrintSelIdx,
history: opts.History,
margin: opts.Margin,
unicode: opts.Unicode,
bordered: opts.Bordered,
cleanExit: opts.ClearOnExit,
strong: strongAttr,
cycle: opts.Cycle,
header: header,
header0: header,
ansi: opts.Ansi,
tabstop: opts.Tabstop,
reading: true,
success: true,
jumping: jumpDisabled,
jumpLabels: opts.JumpLabels,
printer: opts.Printer,
merger: EmptyMerger,
selected: make(map[int32]selectedItem),
reqBox: util.NewEventBox(),
preview: opts.Preview,
previewer: previewer{"", 0, 0, previewBox != nil && !opts.Preview.hidden, false},
previewBox: previewBox,
eventBox: eventBox,
mutex: sync.Mutex{},
suppress: true,
slab: util.MakeSlab(slab16Size, slab32Size),
theme: opts.Theme,
startChan: make(chan bool, 1),
killChan: make(chan int),
tui: renderer,
initFunc: func() { renderer.Init() }}
t.prompt, t.promptLen = t.processTabs([]rune(opts.Prompt), 0)
return &t
}
Expand Down Expand Up @@ -490,6 +494,9 @@ func (t *Terminal) output() bool {
if t.printQuery {
t.printer(string(t.input))
}
if t.printSelIdx {
t.printer(strconv.Itoa(t.cy))
}
if len(t.expect) > 0 {
t.printer(t.pressed)
}
Expand Down Expand Up @@ -2033,7 +2040,7 @@ func (t *Terminal) constrain() {
height := t.maxItems()
diffpos := t.cy - t.offset

t.cy = util.Constrain(t.cy, 0, count-1)
t.cy = util.Constrain(t.scy, 0, count-1)
t.offset = util.Constrain(t.offset, t.cy-height+1, t.cy)
// Adjustment
if count-t.offset < height {
Expand Down Expand Up @@ -2065,6 +2072,7 @@ func (t *Terminal) vmove(o int, allowCycle bool) {

func (t *Terminal) vset(o int) bool {
t.cy = util.Constrain(o, 0, t.merger.Length()-1)
t.scy = t.cy
return t.cy == o
}

Expand Down