2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
2017-01-11 13:13:40 +00:00
|
|
|
"bufio"
|
2015-01-18 07:59:04 +00:00
|
|
|
"bytes"
|
2015-01-01 19:49:30 +00:00
|
|
|
"fmt"
|
2017-01-11 13:13:40 +00:00
|
|
|
"io"
|
2015-01-01 19:49:30 +00:00
|
|
|
"os"
|
2015-01-23 11:30:50 +00:00
|
|
|
"os/signal"
|
2015-01-01 19:49:30 +00:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2016-11-06 17:15:34 +00:00
|
|
|
"strconv"
|
2015-01-18 07:59:04 +00:00
|
|
|
"strings"
|
2015-01-01 19:49:30 +00:00
|
|
|
"sync"
|
2015-01-23 11:30:50 +00:00
|
|
|
"syscall"
|
2015-01-01 19:49:30 +00:00
|
|
|
"time"
|
2015-01-12 03:56:17 +00:00
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
"github.com/junegunn/fzf/src/tui"
|
2015-01-12 03:56:17 +00:00
|
|
|
"github.com/junegunn/fzf/src/util"
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
// import "github.com/pkg/profile"
|
|
|
|
|
2016-10-03 05:16:10 +00:00
|
|
|
var placeholder *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
2019-02-18 16:12:57 +00:00
|
|
|
placeholder = regexp.MustCompile("\\\\?(?:{[+s]*[0-9,-.]*}|{q}|{\\+?n})")
|
2016-10-03 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 17:06:52 +00:00
|
|
|
type jumpMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
jumpDisabled jumpMode = iota
|
|
|
|
jumpEnabled
|
|
|
|
jumpAcceptEnabled
|
|
|
|
)
|
|
|
|
|
2016-09-24 17:02:00 +00:00
|
|
|
type previewer struct {
|
|
|
|
text string
|
|
|
|
lines int
|
|
|
|
offset int
|
|
|
|
enabled bool
|
2019-03-29 09:28:45 +00:00
|
|
|
more bool
|
2016-09-24 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
type itemLine struct {
|
2017-01-07 17:29:31 +00:00
|
|
|
current bool
|
|
|
|
selected bool
|
|
|
|
label string
|
2017-02-23 17:30:11 +00:00
|
|
|
queryLen int
|
2017-01-15 17:26:36 +00:00
|
|
|
width int
|
2017-01-07 17:29:31 +00:00
|
|
|
result Result
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var emptyLine = itemLine{}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Terminal represents terminal input/output
|
2015-01-01 19:49:30 +00:00
|
|
|
type Terminal struct {
|
2016-01-16 09:07:50 +00:00
|
|
|
initDelay time.Duration
|
2015-04-21 14:50:53 +00:00
|
|
|
inlineInfo bool
|
2015-01-01 19:49:30 +00:00
|
|
|
prompt string
|
2017-05-26 10:02:49 +00:00
|
|
|
promptLen int
|
2018-06-25 10:07:12 +00:00
|
|
|
queryLen [2]int
|
2018-06-09 16:41:50 +00:00
|
|
|
layout layoutType
|
2017-04-28 13:58:08 +00:00
|
|
|
fullscreen bool
|
2015-04-16 03:56:01 +00:00
|
|
|
hscroll bool
|
2016-03-01 18:06:21 +00:00
|
|
|
hscrollOff int
|
2017-01-15 10:42:28 +00:00
|
|
|
wordRubout string
|
|
|
|
wordNext string
|
2015-01-01 19:49:30 +00:00
|
|
|
cx int
|
|
|
|
cy int
|
|
|
|
offset int
|
2018-06-25 10:07:12 +00:00
|
|
|
xoffset int
|
2015-01-01 19:49:30 +00:00
|
|
|
yanked []rune
|
|
|
|
input []rune
|
|
|
|
multi bool
|
2015-04-16 05:44:41 +00:00
|
|
|
sort bool
|
2015-05-20 12:25:15 +00:00
|
|
|
toggleSort bool
|
2016-10-03 05:16:10 +00:00
|
|
|
delimiter Delimiter
|
2015-06-18 15:31:48 +00:00
|
|
|
expect map[int]string
|
2017-01-21 17:32:49 +00:00
|
|
|
keymap map[int][]action
|
2015-06-18 15:31:48 +00:00
|
|
|
pressed string
|
2015-01-01 19:49:30 +00:00
|
|
|
printQuery bool
|
2015-06-13 15:43:44 +00:00
|
|
|
history *History
|
2015-06-16 14:14:57 +00:00
|
|
|
cycle bool
|
2015-07-21 15:19:37 +00:00
|
|
|
header []string
|
2015-09-15 10:04:53 +00:00
|
|
|
header0 []string
|
2015-08-28 12:23:10 +00:00
|
|
|
ansi bool
|
2017-01-07 16:30:31 +00:00
|
|
|
tabstop int
|
2016-06-11 10:59:12 +00:00
|
|
|
margin [4]sizeSpec
|
2016-11-15 14:57:32 +00:00
|
|
|
strong tui.Attr
|
2019-03-28 17:11:03 +00:00
|
|
|
unicode bool
|
2017-02-04 12:51:22 +00:00
|
|
|
bordered bool
|
2017-07-18 11:50:38 +00:00
|
|
|
cleanExit bool
|
2017-02-04 12:51:22 +00:00
|
|
|
border tui.Window
|
2017-01-07 16:30:31 +00:00
|
|
|
window tui.Window
|
2017-02-04 12:51:22 +00:00
|
|
|
pborder tui.Window
|
2017-01-07 16:30:31 +00:00
|
|
|
pwindow tui.Window
|
2015-01-01 19:49:30 +00:00
|
|
|
count int
|
|
|
|
progress int
|
|
|
|
reading bool
|
2017-06-30 16:13:15 +00:00
|
|
|
success bool
|
2016-05-17 17:06:52 +00:00
|
|
|
jumping jumpMode
|
|
|
|
jumpLabels string
|
2016-09-17 19:52:47 +00:00
|
|
|
printer func(string)
|
2015-01-09 16:06:08 +00:00
|
|
|
merger *Merger
|
2016-01-12 18:07:42 +00:00
|
|
|
selected map[int32]selectedItem
|
2017-07-27 16:39:25 +00:00
|
|
|
version int64
|
2015-01-12 03:56:17 +00:00
|
|
|
reqBox *util.EventBox
|
2016-06-11 10:59:12 +00:00
|
|
|
preview previewOpts
|
2016-09-24 17:02:00 +00:00
|
|
|
previewer previewer
|
2016-06-11 10:59:12 +00:00
|
|
|
previewBox *util.EventBox
|
2015-01-12 03:56:17 +00:00
|
|
|
eventBox *util.EventBox
|
2015-01-01 19:49:30 +00:00
|
|
|
mutex sync.Mutex
|
|
|
|
initFunc func()
|
2017-01-07 16:30:31 +00:00
|
|
|
prevLines []itemLine
|
2015-01-07 03:46:45 +00:00
|
|
|
suppress bool
|
2015-02-17 15:08:17 +00:00
|
|
|
startChan chan bool
|
2018-09-27 06:27:08 +00:00
|
|
|
killChan chan int
|
2016-09-07 00:58:18 +00:00
|
|
|
slab *util.Slab
|
2016-10-24 00:44:56 +00:00
|
|
|
theme *tui.ColorTheme
|
2017-01-07 16:30:31 +00:00
|
|
|
tui tui.Renderer
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 02:07:04 +00:00
|
|
|
type selectedItem struct {
|
|
|
|
at time.Time
|
2016-10-03 05:16:10 +00:00
|
|
|
item *Item
|
2015-01-17 02:07:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 07:05:54 +00:00
|
|
|
type byTimeOrder []selectedItem
|
2015-01-17 02:07:04 +00:00
|
|
|
|
2015-03-22 07:05:54 +00:00
|
|
|
func (a byTimeOrder) Len() int {
|
2015-01-17 02:07:04 +00:00
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
2015-03-22 07:05:54 +00:00
|
|
|
func (a byTimeOrder) Swap(i, j int) {
|
2015-01-17 02:07:04 +00:00
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
2015-03-22 07:05:54 +00:00
|
|
|
func (a byTimeOrder) Less(i, j int) bool {
|
2015-01-17 02:07:04 +00:00
|
|
|
return a[i].at.Before(a[j].at)
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
const (
|
2015-01-12 03:56:17 +00:00
|
|
|
reqPrompt util.EventType = iota
|
2015-01-11 18:01:24 +00:00
|
|
|
reqInfo
|
2015-07-21 18:21:20 +00:00
|
|
|
reqHeader
|
2015-01-11 18:01:24 +00:00
|
|
|
reqList
|
2016-05-17 17:06:52 +00:00
|
|
|
reqJump
|
2015-01-11 18:01:24 +00:00
|
|
|
reqRefresh
|
2017-04-28 13:58:08 +00:00
|
|
|
reqReinit
|
2015-01-11 18:01:24 +00:00
|
|
|
reqRedraw
|
|
|
|
reqClose
|
2016-05-12 15:51:15 +00:00
|
|
|
reqPrintQuery
|
2016-06-11 10:59:12 +00:00
|
|
|
reqPreviewEnqueue
|
|
|
|
reqPreviewDisplay
|
2016-09-24 17:02:00 +00:00
|
|
|
reqPreviewRefresh
|
2015-01-11 18:01:24 +00:00
|
|
|
reqQuit
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
2017-01-21 17:32:49 +00:00
|
|
|
type action struct {
|
|
|
|
t actionType
|
|
|
|
a string
|
|
|
|
}
|
|
|
|
|
2015-05-20 12:25:15 +00:00
|
|
|
type actionType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
actIgnore actionType = iota
|
|
|
|
actInvalid
|
|
|
|
actRune
|
|
|
|
actMouse
|
|
|
|
actBeginningOfLine
|
|
|
|
actAbort
|
|
|
|
actAccept
|
2017-12-01 17:27:02 +00:00
|
|
|
actAcceptNonEmpty
|
2015-05-20 12:25:15 +00:00
|
|
|
actBackwardChar
|
|
|
|
actBackwardDeleteChar
|
|
|
|
actBackwardWord
|
2015-07-23 12:05:33 +00:00
|
|
|
actCancel
|
2015-05-20 12:25:15 +00:00
|
|
|
actClearScreen
|
|
|
|
actDeleteChar
|
2015-08-02 04:06:15 +00:00
|
|
|
actDeleteCharEOF
|
2015-05-20 12:25:15 +00:00
|
|
|
actEndOfLine
|
|
|
|
actForwardChar
|
|
|
|
actForwardWord
|
|
|
|
actKillLine
|
|
|
|
actKillWord
|
|
|
|
actUnixLineDiscard
|
|
|
|
actUnixWordRubout
|
|
|
|
actYank
|
|
|
|
actBackwardKillWord
|
2015-06-09 14:44:54 +00:00
|
|
|
actSelectAll
|
|
|
|
actDeselectAll
|
2015-05-20 16:35:35 +00:00
|
|
|
actToggle
|
2015-06-09 14:44:54 +00:00
|
|
|
actToggleAll
|
2015-05-20 12:25:15 +00:00
|
|
|
actToggleDown
|
|
|
|
actToggleUp
|
2016-01-13 17:35:43 +00:00
|
|
|
actToggleIn
|
|
|
|
actToggleOut
|
2015-05-20 12:25:15 +00:00
|
|
|
actDown
|
|
|
|
actUp
|
|
|
|
actPageUp
|
|
|
|
actPageDown
|
2017-01-16 02:58:13 +00:00
|
|
|
actHalfPageUp
|
|
|
|
actHalfPageDown
|
2016-05-17 17:06:52 +00:00
|
|
|
actJump
|
|
|
|
actJumpAccept
|
2016-05-12 15:51:15 +00:00
|
|
|
actPrintQuery
|
2017-12-01 04:08:55 +00:00
|
|
|
actReplaceQuery
|
2015-05-20 12:25:15 +00:00
|
|
|
actToggleSort
|
2016-06-11 10:59:12 +00:00
|
|
|
actTogglePreview
|
2017-02-18 14:49:00 +00:00
|
|
|
actTogglePreviewWrap
|
2016-09-24 17:02:00 +00:00
|
|
|
actPreviewUp
|
|
|
|
actPreviewDown
|
2016-09-24 19:12:44 +00:00
|
|
|
actPreviewPageUp
|
|
|
|
actPreviewPageDown
|
2015-06-13 15:43:44 +00:00
|
|
|
actPreviousHistory
|
|
|
|
actNextHistory
|
2015-06-14 03:25:08 +00:00
|
|
|
actExecute
|
2017-01-27 08:46:56 +00:00
|
|
|
actExecuteSilent
|
|
|
|
actExecuteMulti // Deprecated
|
2017-04-28 13:58:08 +00:00
|
|
|
actSigStop
|
2017-05-22 08:07:05 +00:00
|
|
|
actTop
|
2015-05-20 12:25:15 +00:00
|
|
|
)
|
|
|
|
|
2018-03-30 02:47:46 +00:00
|
|
|
type placeholderFlags struct {
|
|
|
|
plus bool
|
|
|
|
preserveSpace bool
|
2019-02-18 16:12:57 +00:00
|
|
|
number bool
|
2018-06-09 16:40:22 +00:00
|
|
|
query bool
|
2018-03-30 02:47:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:32:49 +00:00
|
|
|
func toActions(types ...actionType) []action {
|
|
|
|
actions := make([]action, len(types))
|
|
|
|
for idx, t := range types {
|
|
|
|
actions[idx] = action{t: t, a: ""}
|
|
|
|
}
|
|
|
|
return actions
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultKeymap() map[int][]action {
|
|
|
|
keymap := make(map[int][]action)
|
|
|
|
keymap[tui.Invalid] = toActions(actInvalid)
|
|
|
|
keymap[tui.Resize] = toActions(actClearScreen)
|
|
|
|
keymap[tui.CtrlA] = toActions(actBeginningOfLine)
|
|
|
|
keymap[tui.CtrlB] = toActions(actBackwardChar)
|
|
|
|
keymap[tui.CtrlC] = toActions(actAbort)
|
|
|
|
keymap[tui.CtrlG] = toActions(actAbort)
|
|
|
|
keymap[tui.CtrlQ] = toActions(actAbort)
|
|
|
|
keymap[tui.ESC] = toActions(actAbort)
|
|
|
|
keymap[tui.CtrlD] = toActions(actDeleteCharEOF)
|
|
|
|
keymap[tui.CtrlE] = toActions(actEndOfLine)
|
|
|
|
keymap[tui.CtrlF] = toActions(actForwardChar)
|
|
|
|
keymap[tui.CtrlH] = toActions(actBackwardDeleteChar)
|
|
|
|
keymap[tui.BSpace] = toActions(actBackwardDeleteChar)
|
|
|
|
keymap[tui.Tab] = toActions(actToggleDown)
|
|
|
|
keymap[tui.BTab] = toActions(actToggleUp)
|
|
|
|
keymap[tui.CtrlJ] = toActions(actDown)
|
|
|
|
keymap[tui.CtrlK] = toActions(actUp)
|
|
|
|
keymap[tui.CtrlL] = toActions(actClearScreen)
|
|
|
|
keymap[tui.CtrlM] = toActions(actAccept)
|
|
|
|
keymap[tui.CtrlN] = toActions(actDown)
|
|
|
|
keymap[tui.CtrlP] = toActions(actUp)
|
|
|
|
keymap[tui.CtrlU] = toActions(actUnixLineDiscard)
|
|
|
|
keymap[tui.CtrlW] = toActions(actUnixWordRubout)
|
|
|
|
keymap[tui.CtrlY] = toActions(actYank)
|
2017-04-28 13:58:08 +00:00
|
|
|
if !util.IsWindows() {
|
|
|
|
keymap[tui.CtrlZ] = toActions(actSigStop)
|
|
|
|
}
|
2017-01-21 17:32:49 +00:00
|
|
|
|
|
|
|
keymap[tui.AltB] = toActions(actBackwardWord)
|
|
|
|
keymap[tui.SLeft] = toActions(actBackwardWord)
|
|
|
|
keymap[tui.AltF] = toActions(actForwardWord)
|
|
|
|
keymap[tui.SRight] = toActions(actForwardWord)
|
|
|
|
keymap[tui.AltD] = toActions(actKillWord)
|
|
|
|
keymap[tui.AltBS] = toActions(actBackwardKillWord)
|
|
|
|
|
|
|
|
keymap[tui.Up] = toActions(actUp)
|
|
|
|
keymap[tui.Down] = toActions(actDown)
|
|
|
|
keymap[tui.Left] = toActions(actBackwardChar)
|
|
|
|
keymap[tui.Right] = toActions(actForwardChar)
|
|
|
|
|
|
|
|
keymap[tui.Home] = toActions(actBeginningOfLine)
|
|
|
|
keymap[tui.End] = toActions(actEndOfLine)
|
|
|
|
keymap[tui.Del] = toActions(actDeleteChar)
|
|
|
|
keymap[tui.PgUp] = toActions(actPageUp)
|
|
|
|
keymap[tui.PgDn] = toActions(actPageDown)
|
|
|
|
|
2018-02-15 10:56:11 +00:00
|
|
|
keymap[tui.SUp] = toActions(actPreviewUp)
|
|
|
|
keymap[tui.SDown] = toActions(actPreviewDown)
|
|
|
|
|
2017-01-21 17:32:49 +00:00
|
|
|
keymap[tui.Rune] = toActions(actRune)
|
|
|
|
keymap[tui.Mouse] = toActions(actMouse)
|
|
|
|
keymap[tui.DoubleClick] = toActions(actAccept)
|
2017-11-30 17:11:20 +00:00
|
|
|
keymap[tui.LeftClick] = toActions(actIgnore)
|
|
|
|
keymap[tui.RightClick] = toActions(actToggle)
|
2015-05-20 12:25:15 +00:00
|
|
|
return keymap
|
|
|
|
}
|
|
|
|
|
2017-08-09 14:25:32 +00:00
|
|
|
func trimQuery(query string) []rune {
|
|
|
|
return []rune(strings.Replace(query, "\t", " ", -1))
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// NewTerminal returns new Terminal object
|
2015-01-12 03:56:17 +00:00
|
|
|
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
2017-08-09 14:25:32 +00:00
|
|
|
input := trimQuery(opts.Query)
|
2015-09-15 10:04:53 +00:00
|
|
|
var header []string
|
2018-06-09 16:41:50 +00:00
|
|
|
switch opts.Layout {
|
|
|
|
case layoutDefault, layoutReverseList:
|
2015-09-15 10:04:53 +00:00
|
|
|
header = reverseStringArray(opts.Header)
|
2018-06-09 16:41:50 +00:00
|
|
|
default:
|
|
|
|
header = opts.Header
|
2015-09-15 10:04:53 +00:00
|
|
|
}
|
2016-01-16 09:07:50 +00:00
|
|
|
var delay time.Duration
|
|
|
|
if opts.Tac {
|
|
|
|
delay = initialDelayTac
|
|
|
|
} else {
|
|
|
|
delay = initialDelay
|
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
var previewBox *util.EventBox
|
|
|
|
if len(opts.Preview.command) > 0 {
|
|
|
|
previewBox = util.NewEventBox()
|
|
|
|
}
|
2016-11-15 14:57:32 +00:00
|
|
|
strongAttr := tui.Bold
|
|
|
|
if !opts.Bold {
|
|
|
|
strongAttr = tui.AttrRegular
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
var renderer tui.Renderer
|
2017-04-28 13:58:08 +00:00
|
|
|
fullscreen := opts.Height.size == 0 || opts.Height.percent && opts.Height.size == 100
|
|
|
|
if fullscreen {
|
2017-03-04 05:09:36 +00:00
|
|
|
if tui.HasFullscreenRenderer() {
|
|
|
|
renderer = tui.NewFullscreenRenderer(opts.Theme, opts.Black, opts.Mouse)
|
|
|
|
} else {
|
|
|
|
renderer = tui.NewLightRenderer(opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit,
|
|
|
|
true, func(h int) int { return h })
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-07 16:30:31 +00:00
|
|
|
maxHeightFunc := func(termHeight int) int {
|
|
|
|
var maxHeight int
|
|
|
|
if opts.Height.percent {
|
2017-02-04 12:51:22 +00:00
|
|
|
maxHeight = util.Max(int(opts.Height.size*float64(termHeight)/100.0), opts.MinHeight)
|
2017-01-07 16:30:31 +00:00
|
|
|
} else {
|
2017-02-04 12:51:22 +00:00
|
|
|
maxHeight = int(opts.Height.size)
|
|
|
|
}
|
|
|
|
|
|
|
|
effectiveMinHeight := minHeight
|
|
|
|
if previewBox != nil && (opts.Preview.position == posUp || opts.Preview.position == posDown) {
|
|
|
|
effectiveMinHeight *= 2
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
if opts.InlineInfo {
|
2017-02-04 12:51:22 +00:00
|
|
|
effectiveMinHeight -= 1
|
|
|
|
}
|
|
|
|
if opts.Bordered {
|
|
|
|
effectiveMinHeight += 2
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2017-02-04 12:51:22 +00:00
|
|
|
return util.Min(termHeight, util.Max(maxHeight, effectiveMinHeight))
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2017-03-04 05:09:36 +00:00
|
|
|
renderer = tui.NewLightRenderer(opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit, false, maxHeightFunc)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2017-01-15 10:42:28 +00:00
|
|
|
wordRubout := "[^[:alnum:]][[:alnum:]]"
|
|
|
|
wordNext := "[[:alnum:]][^[:alnum:]]|(.$)"
|
|
|
|
if opts.FileWord {
|
|
|
|
sep := regexp.QuoteMeta(string(os.PathSeparator))
|
|
|
|
wordRubout = fmt.Sprintf("%s[^%s]", sep, sep)
|
|
|
|
wordNext = fmt.Sprintf("[^%s]%s|(.$)", sep, sep)
|
|
|
|
}
|
2017-05-26 10:02:49 +00:00
|
|
|
t := Terminal{
|
2016-01-16 09:07:50 +00:00
|
|
|
initDelay: delay,
|
2015-04-21 14:50:53 +00:00
|
|
|
inlineInfo: opts.InlineInfo,
|
2018-06-25 10:07:12 +00:00
|
|
|
queryLen: [2]int{0, 0},
|
2018-06-09 16:41:50 +00:00
|
|
|
layout: opts.Layout,
|
2017-04-28 13:58:08 +00:00
|
|
|
fullscreen: fullscreen,
|
2015-04-16 03:56:01 +00:00
|
|
|
hscroll: opts.Hscroll,
|
2016-03-01 18:06:21 +00:00
|
|
|
hscrollOff: opts.HscrollOff,
|
2017-01-15 10:42:28 +00:00
|
|
|
wordRubout: wordRubout,
|
|
|
|
wordNext: wordNext,
|
2015-01-24 04:26:33 +00:00
|
|
|
cx: len(input),
|
2015-01-01 19:49:30 +00:00
|
|
|
cy: 0,
|
|
|
|
offset: 0,
|
2018-06-25 10:07:12 +00:00
|
|
|
xoffset: 0,
|
2015-01-01 19:49:30 +00:00
|
|
|
yanked: []rune{},
|
|
|
|
input: input,
|
|
|
|
multi: opts.Multi,
|
2015-04-16 05:44:41 +00:00
|
|
|
sort: opts.Sort > 0,
|
2015-03-31 13:05:02 +00:00
|
|
|
toggleSort: opts.ToggleSort,
|
2016-10-03 05:16:10 +00:00
|
|
|
delimiter: opts.Delimiter,
|
2015-03-28 17:59:32 +00:00
|
|
|
expect: opts.Expect,
|
2015-05-20 12:25:15 +00:00
|
|
|
keymap: opts.Keymap,
|
2015-06-18 15:31:48 +00:00
|
|
|
pressed: "",
|
2015-01-01 19:49:30 +00:00
|
|
|
printQuery: opts.PrintQuery,
|
2015-06-13 15:43:44 +00:00
|
|
|
history: opts.History,
|
2015-07-26 14:02:04 +00:00
|
|
|
margin: opts.Margin,
|
2019-03-28 17:11:03 +00:00
|
|
|
unicode: opts.Unicode,
|
2017-02-04 12:51:22 +00:00
|
|
|
bordered: opts.Bordered,
|
2017-07-18 11:50:38 +00:00
|
|
|
cleanExit: opts.ClearOnExit,
|
2016-11-15 14:57:32 +00:00
|
|
|
strong: strongAttr,
|
2015-06-16 14:14:57 +00:00
|
|
|
cycle: opts.Cycle,
|
2015-09-15 10:04:53 +00:00
|
|
|
header: header,
|
|
|
|
header0: header,
|
2015-08-28 12:23:10 +00:00
|
|
|
ansi: opts.Ansi,
|
2017-01-07 16:30:31 +00:00
|
|
|
tabstop: opts.Tabstop,
|
2015-06-17 15:42:38 +00:00
|
|
|
reading: true,
|
2017-06-30 16:13:15 +00:00
|
|
|
success: true,
|
2016-05-17 17:06:52 +00:00
|
|
|
jumping: jumpDisabled,
|
|
|
|
jumpLabels: opts.JumpLabels,
|
2016-09-17 19:52:47 +00:00
|
|
|
printer: opts.Printer,
|
2015-01-09 16:06:08 +00:00
|
|
|
merger: EmptyMerger,
|
2016-01-12 18:07:42 +00:00
|
|
|
selected: make(map[int32]selectedItem),
|
2015-01-12 03:56:17 +00:00
|
|
|
reqBox: util.NewEventBox(),
|
2016-06-11 10:59:12 +00:00
|
|
|
preview: opts.Preview,
|
2019-03-29 09:28:45 +00:00
|
|
|
previewer: previewer{"", 0, 0, previewBox != nil && !opts.Preview.hidden, false},
|
2016-06-11 10:59:12 +00:00
|
|
|
previewBox: previewBox,
|
2015-01-01 19:49:30 +00:00
|
|
|
eventBox: eventBox,
|
|
|
|
mutex: sync.Mutex{},
|
2015-01-07 03:46:45 +00:00
|
|
|
suppress: true,
|
2016-09-07 00:58:18 +00:00
|
|
|
slab: util.MakeSlab(slab16Size, slab32Size),
|
2016-10-21 10:35:59 +00:00
|
|
|
theme: opts.Theme,
|
2015-02-17 15:08:17 +00:00
|
|
|
startChan: make(chan bool, 1),
|
2018-09-27 06:27:08 +00:00
|
|
|
killChan: make(chan int),
|
2017-01-07 16:30:31 +00:00
|
|
|
tui: renderer,
|
|
|
|
initFunc: func() { renderer.Init() }}
|
2017-05-26 10:02:49 +00:00
|
|
|
t.prompt, t.promptLen = t.processTabs([]rune(opts.Prompt), 0)
|
|
|
|
return &t
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Input returns current query string
|
2015-01-01 19:49:30 +00:00
|
|
|
func (t *Terminal) Input() []rune {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
return copySlice(t.input)
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// UpdateCount updates the count information
|
2017-06-30 16:13:15 +00:00
|
|
|
func (t *Terminal) UpdateCount(cnt int, final bool, success bool) {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Lock()
|
|
|
|
t.count = cnt
|
|
|
|
t.reading = !final
|
2017-06-30 16:13:15 +00:00
|
|
|
t.success = success
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Unlock()
|
2015-01-11 18:01:24 +00:00
|
|
|
t.reqBox.Set(reqInfo, nil)
|
2015-01-07 03:46:45 +00:00
|
|
|
if final {
|
2015-01-11 18:01:24 +00:00
|
|
|
t.reqBox.Set(reqRefresh, nil)
|
2015-01-07 03:46:45 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 10:04:53 +00:00
|
|
|
func reverseStringArray(input []string) []string {
|
|
|
|
size := len(input)
|
|
|
|
reversed := make([]string, size)
|
|
|
|
for idx, str := range input {
|
|
|
|
reversed[size-idx-1] = str
|
|
|
|
}
|
|
|
|
return reversed
|
|
|
|
}
|
|
|
|
|
2015-07-21 18:21:20 +00:00
|
|
|
// UpdateHeader updates the header
|
2015-09-15 10:04:53 +00:00
|
|
|
func (t *Terminal) UpdateHeader(header []string) {
|
2015-07-21 18:21:20 +00:00
|
|
|
t.mutex.Lock()
|
2015-09-15 10:04:53 +00:00
|
|
|
t.header = append(append([]string{}, t.header0...), header...)
|
2015-07-21 18:21:20 +00:00
|
|
|
t.mutex.Unlock()
|
|
|
|
t.reqBox.Set(reqHeader, nil)
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// UpdateProgress updates the search progress
|
2015-01-01 19:49:30 +00:00
|
|
|
func (t *Terminal) UpdateProgress(progress float32) {
|
|
|
|
t.mutex.Lock()
|
2015-01-11 12:56:55 +00:00
|
|
|
newProgress := int(progress * 100)
|
|
|
|
changed := t.progress != newProgress
|
|
|
|
t.progress = newProgress
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Unlock()
|
2015-01-11 12:56:55 +00:00
|
|
|
|
|
|
|
if changed {
|
2015-01-11 18:01:24 +00:00
|
|
|
t.reqBox.Set(reqInfo, nil)
|
2015-01-11 12:56:55 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// UpdateList updates Merger to display the list
|
2015-01-09 16:06:08 +00:00
|
|
|
func (t *Terminal) UpdateList(merger *Merger) {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Lock()
|
|
|
|
t.progress = 100
|
2015-01-09 16:06:08 +00:00
|
|
|
t.merger = merger
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Unlock()
|
2015-01-11 18:01:24 +00:00
|
|
|
t.reqBox.Set(reqInfo, nil)
|
|
|
|
t.reqBox.Set(reqList, nil)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:21:51 +00:00
|
|
|
func (t *Terminal) output() bool {
|
2015-01-01 19:49:30 +00:00
|
|
|
if t.printQuery {
|
2016-09-17 19:52:47 +00:00
|
|
|
t.printer(string(t.input))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-03-28 17:59:32 +00:00
|
|
|
if len(t.expect) > 0 {
|
2016-09-17 19:52:47 +00:00
|
|
|
t.printer(t.pressed)
|
2015-03-28 17:59:32 +00:00
|
|
|
}
|
2015-09-15 04:21:51 +00:00
|
|
|
found := len(t.selected) > 0
|
|
|
|
if !found {
|
2017-01-27 07:38:42 +00:00
|
|
|
current := t.currentItem()
|
|
|
|
if current != nil {
|
|
|
|
t.printer(current.AsString(t.ansi))
|
2015-09-15 04:21:51 +00:00
|
|
|
found = true
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-11-08 16:42:01 +00:00
|
|
|
for _, sel := range t.sortSelected() {
|
2016-10-03 05:16:10 +00:00
|
|
|
t.printer(sel.item.AsString(t.ansi))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-15 04:21:51 +00:00
|
|
|
return found
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 16:42:01 +00:00
|
|
|
func (t *Terminal) sortSelected() []selectedItem {
|
|
|
|
sels := make([]selectedItem, 0, len(t.selected))
|
|
|
|
for _, sel := range t.selected {
|
|
|
|
sels = append(sels, sel)
|
|
|
|
}
|
|
|
|
sort.Sort(byTimeOrder(sels))
|
|
|
|
return sels
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (t *Terminal) displayWidth(runes []rune) int {
|
2015-01-01 19:49:30 +00:00
|
|
|
l := 0
|
|
|
|
for _, r := range runes {
|
2017-01-07 16:30:31 +00:00
|
|
|
l += util.RuneWidth(r, l, t.tabstop)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2016-06-11 10:59:12 +00:00
|
|
|
const (
|
|
|
|
minWidth = 16
|
|
|
|
minHeight = 4
|
2016-09-20 16:23:41 +00:00
|
|
|
|
|
|
|
maxDisplayWidthCalc = 1024
|
2016-06-11 10:59:12 +00:00
|
|
|
)
|
2015-07-26 14:02:04 +00:00
|
|
|
|
2016-06-11 10:59:12 +00:00
|
|
|
func calculateSize(base int, size sizeSpec, margin int, minSize int) int {
|
|
|
|
max := base - margin
|
|
|
|
if size.percent {
|
|
|
|
return util.Constrain(int(float64(base)*0.01*size.size), minSize, max)
|
|
|
|
}
|
|
|
|
return util.Constrain(int(size.size), minSize, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) resizeWindows() {
|
2017-01-07 16:30:31 +00:00
|
|
|
screenWidth := t.tui.MaxX()
|
|
|
|
screenHeight := t.tui.MaxY()
|
2016-06-11 10:59:12 +00:00
|
|
|
marginInt := [4]int{}
|
2017-01-07 16:30:31 +00:00
|
|
|
t.prevLines = make([]itemLine, screenHeight)
|
2016-06-11 10:59:12 +00:00
|
|
|
for idx, sizeSpec := range t.margin {
|
|
|
|
if sizeSpec.percent {
|
|
|
|
var max float64
|
2015-07-26 14:02:04 +00:00
|
|
|
if idx%2 == 0 {
|
2016-06-11 10:59:12 +00:00
|
|
|
max = float64(screenHeight)
|
2015-07-26 14:02:04 +00:00
|
|
|
} else {
|
2016-06-11 10:59:12 +00:00
|
|
|
max = float64(screenWidth)
|
2015-07-26 14:02:04 +00:00
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
marginInt[idx] = int(max * sizeSpec.size * 0.01)
|
2015-07-26 14:02:04 +00:00
|
|
|
} else {
|
2016-06-11 10:59:12 +00:00
|
|
|
marginInt[idx] = int(sizeSpec.size)
|
2015-07-26 14:02:04 +00:00
|
|
|
}
|
2017-02-04 12:51:22 +00:00
|
|
|
if t.bordered && idx%2 == 0 {
|
|
|
|
marginInt[idx] += 1
|
|
|
|
}
|
2015-07-26 14:02:04 +00:00
|
|
|
}
|
|
|
|
adjust := func(idx1 int, idx2 int, max int, min int) {
|
|
|
|
if max >= min {
|
2016-06-11 10:59:12 +00:00
|
|
|
margin := marginInt[idx1] + marginInt[idx2]
|
2015-07-26 14:02:04 +00:00
|
|
|
if max-margin < min {
|
|
|
|
desired := max - min
|
2016-06-11 10:59:12 +00:00
|
|
|
marginInt[idx1] = desired * marginInt[idx1] / margin
|
|
|
|
marginInt[idx2] = desired * marginInt[idx2] / margin
|
2015-07-26 14:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 15:23:16 +00:00
|
|
|
|
|
|
|
previewVisible := t.isPreviewEnabled() && t.preview.size.size > 0
|
2016-06-11 10:59:12 +00:00
|
|
|
minAreaWidth := minWidth
|
|
|
|
minAreaHeight := minHeight
|
2017-01-23 15:23:16 +00:00
|
|
|
if previewVisible {
|
2016-06-11 10:59:12 +00:00
|
|
|
switch t.preview.position {
|
|
|
|
case posUp, posDown:
|
|
|
|
minAreaHeight *= 2
|
|
|
|
case posLeft, posRight:
|
|
|
|
minAreaWidth *= 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
adjust(1, 3, screenWidth, minAreaWidth)
|
|
|
|
adjust(0, 2, screenHeight, minAreaHeight)
|
2017-02-04 12:51:22 +00:00
|
|
|
if t.border != nil {
|
|
|
|
t.border.Close()
|
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
if t.window != nil {
|
|
|
|
t.window.Close()
|
|
|
|
}
|
2017-02-04 12:51:22 +00:00
|
|
|
if t.pborder != nil {
|
|
|
|
t.pborder.Close()
|
2016-06-11 10:59:12 +00:00
|
|
|
t.pwindow.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
width := screenWidth - marginInt[1] - marginInt[3]
|
|
|
|
height := screenHeight - marginInt[0] - marginInt[2]
|
2017-02-04 12:51:22 +00:00
|
|
|
if t.bordered {
|
|
|
|
t.border = t.tui.NewWindow(
|
|
|
|
marginInt[0]-1,
|
|
|
|
marginInt[3],
|
|
|
|
width,
|
2019-03-28 17:11:03 +00:00
|
|
|
height+2, tui.MakeBorderStyle(tui.BorderHorizontal, t.unicode))
|
2017-02-04 12:51:22 +00:00
|
|
|
}
|
2019-03-28 17:11:03 +00:00
|
|
|
noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode)
|
2017-01-23 15:23:16 +00:00
|
|
|
if previewVisible {
|
2016-06-11 10:59:12 +00:00
|
|
|
createPreviewWindow := func(y int, x int, w int, h int) {
|
2019-03-28 17:11:03 +00:00
|
|
|
t.pborder = t.tui.NewWindow(y, x, w, h, tui.MakeBorderStyle(tui.BorderAround, t.unicode))
|
2016-12-04 17:13:47 +00:00
|
|
|
pwidth := w - 4
|
|
|
|
// ncurses auto-wraps the line when the cursor reaches the right-end of
|
|
|
|
// the window. To prevent unintended line-wraps, we use the width one
|
|
|
|
// column larger than the desired value.
|
2017-01-07 16:30:31 +00:00
|
|
|
if !t.preview.wrap && t.tui.DoesAutoWrap() {
|
2016-12-04 17:13:47 +00:00
|
|
|
pwidth += 1
|
|
|
|
}
|
2019-03-28 17:11:03 +00:00
|
|
|
t.pwindow = t.tui.NewWindow(y+1, x+2, pwidth, h-2, noBorder)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
switch t.preview.position {
|
|
|
|
case posUp:
|
|
|
|
pheight := calculateSize(height, t.preview.size, minHeight, 3)
|
2017-01-07 16:30:31 +00:00
|
|
|
t.window = t.tui.NewWindow(
|
2019-03-28 17:11:03 +00:00
|
|
|
marginInt[0]+pheight, marginInt[3], width, height-pheight, noBorder)
|
2016-06-11 10:59:12 +00:00
|
|
|
createPreviewWindow(marginInt[0], marginInt[3], width, pheight)
|
|
|
|
case posDown:
|
|
|
|
pheight := calculateSize(height, t.preview.size, minHeight, 3)
|
2017-01-07 16:30:31 +00:00
|
|
|
t.window = t.tui.NewWindow(
|
2019-03-28 17:11:03 +00:00
|
|
|
marginInt[0], marginInt[3], width, height-pheight, noBorder)
|
2016-06-11 10:59:12 +00:00
|
|
|
createPreviewWindow(marginInt[0]+height-pheight, marginInt[3], width, pheight)
|
|
|
|
case posLeft:
|
|
|
|
pwidth := calculateSize(width, t.preview.size, minWidth, 5)
|
2017-01-07 16:30:31 +00:00
|
|
|
t.window = t.tui.NewWindow(
|
2019-03-28 17:11:03 +00:00
|
|
|
marginInt[0], marginInt[3]+pwidth, width-pwidth, height, noBorder)
|
2016-06-11 10:59:12 +00:00
|
|
|
createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)
|
|
|
|
case posRight:
|
|
|
|
pwidth := calculateSize(width, t.preview.size, minWidth, 5)
|
2017-01-07 16:30:31 +00:00
|
|
|
t.window = t.tui.NewWindow(
|
2019-03-28 17:11:03 +00:00
|
|
|
marginInt[0], marginInt[3], width-pwidth, height, noBorder)
|
2016-06-11 10:59:12 +00:00
|
|
|
createPreviewWindow(marginInt[0], marginInt[3]+width-pwidth, pwidth, height)
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-07 16:30:31 +00:00
|
|
|
t.window = t.tui.NewWindow(
|
2016-06-11 10:59:12 +00:00
|
|
|
marginInt[0],
|
|
|
|
marginInt[3],
|
|
|
|
width,
|
2019-03-28 17:11:03 +00:00
|
|
|
height, noBorder)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
2017-09-09 04:50:07 +00:00
|
|
|
for i := 0; i < t.window.Height(); i++ {
|
|
|
|
t.window.MoveAndClear(i, 0)
|
2017-01-16 03:06:54 +00:00
|
|
|
}
|
2015-07-26 14:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func (t *Terminal) move(y int, x int, clear bool) {
|
2018-06-09 16:41:50 +00:00
|
|
|
h := t.window.Height()
|
|
|
|
|
|
|
|
switch t.layout {
|
|
|
|
case layoutDefault:
|
|
|
|
y = h - y - 1
|
|
|
|
case layoutReverseList:
|
|
|
|
n := 2 + len(t.header)
|
|
|
|
if t.inlineInfo {
|
|
|
|
n--
|
|
|
|
}
|
|
|
|
if y < n {
|
|
|
|
y = h - y - 1
|
|
|
|
} else {
|
|
|
|
y -= n
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if clear {
|
2016-06-11 10:59:12 +00:00
|
|
|
t.window.MoveAndClear(y, x)
|
2015-01-01 19:49:30 +00:00
|
|
|
} else {
|
2016-06-11 10:59:12 +00:00
|
|
|
t.window.Move(y, x)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 10:07:12 +00:00
|
|
|
func (t *Terminal) truncateQuery() {
|
|
|
|
t.input, _ = t.trimRight(t.input, maxPatternLength)
|
|
|
|
t.cx = util.Constrain(t.cx, 0, len(t.input))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) updatePromptOffset() ([]rune, []rune) {
|
|
|
|
maxWidth := util.Max(1, t.window.Width()-t.promptLen-1)
|
|
|
|
|
|
|
|
_, overflow := t.trimLeft(t.input[:t.cx], maxWidth)
|
|
|
|
minOffset := int(overflow)
|
|
|
|
maxOffset := util.Min(util.Min(len(t.input), minOffset+maxWidth), t.cx)
|
|
|
|
|
|
|
|
t.xoffset = util.Constrain(t.xoffset, minOffset, maxOffset)
|
|
|
|
before, _ := t.trimLeft(t.input[t.xoffset:t.cx], maxWidth)
|
|
|
|
beforeLen := t.displayWidth(before)
|
|
|
|
after, _ := t.trimRight(t.input[t.cx:], maxWidth-beforeLen)
|
|
|
|
afterLen := t.displayWidth(after)
|
|
|
|
t.queryLen = [2]int{beforeLen, afterLen}
|
|
|
|
return before, after
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func (t *Terminal) placeCursor() {
|
2018-06-25 10:07:12 +00:00
|
|
|
t.move(0, t.promptLen+t.queryLen[0], false)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printPrompt() {
|
|
|
|
t.move(0, 0, true)
|
2016-11-15 14:57:32 +00:00
|
|
|
t.window.CPrint(tui.ColPrompt, t.strong, t.prompt)
|
2018-06-25 10:07:12 +00:00
|
|
|
|
|
|
|
before, after := t.updatePromptOffset()
|
|
|
|
t.window.CPrint(tui.ColNormal, t.strong, string(before))
|
|
|
|
t.window.CPrint(tui.ColNormal, t.strong, string(after))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printInfo() {
|
2017-02-18 14:17:29 +00:00
|
|
|
pos := 0
|
2015-04-21 14:50:53 +00:00
|
|
|
if t.inlineInfo {
|
2018-06-25 10:07:12 +00:00
|
|
|
pos = t.promptLen + t.queryLen[0] + t.queryLen[1] + 1
|
2017-02-18 14:17:29 +00:00
|
|
|
if pos+len(" < ") > t.window.Width() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.move(0, pos, true)
|
2015-04-21 14:50:53 +00:00
|
|
|
if t.reading {
|
2016-11-15 14:57:32 +00:00
|
|
|
t.window.CPrint(tui.ColSpinner, t.strong, " < ")
|
2015-04-21 14:50:53 +00:00
|
|
|
} else {
|
2016-11-15 14:57:32 +00:00
|
|
|
t.window.CPrint(tui.ColPrompt, t.strong, " < ")
|
2015-04-21 14:50:53 +00:00
|
|
|
}
|
2017-02-18 14:17:29 +00:00
|
|
|
pos += len(" < ")
|
2015-04-21 14:50:53 +00:00
|
|
|
} else {
|
|
|
|
t.move(1, 0, true)
|
|
|
|
if t.reading {
|
|
|
|
duration := int64(spinnerDuration)
|
|
|
|
idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration
|
2016-11-15 14:57:32 +00:00
|
|
|
t.window.CPrint(tui.ColSpinner, t.strong, _spinner[idx])
|
2015-04-21 14:50:53 +00:00
|
|
|
}
|
|
|
|
t.move(1, 2, false)
|
2017-02-18 14:17:29 +00:00
|
|
|
pos = 2
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 16:06:08 +00:00
|
|
|
output := fmt.Sprintf("%d/%d", t.merger.Length(), t.count)
|
2015-05-20 12:25:15 +00:00
|
|
|
if t.toggleSort {
|
2015-04-16 05:44:41 +00:00
|
|
|
if t.sort {
|
2017-03-02 17:26:30 +00:00
|
|
|
output += " +S"
|
2015-04-16 05:44:41 +00:00
|
|
|
} else {
|
2017-03-02 17:26:30 +00:00
|
|
|
output += " -S"
|
2015-04-16 05:44:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
if t.multi && len(t.selected) > 0 {
|
|
|
|
output += fmt.Sprintf(" (%d)", len(t.selected))
|
|
|
|
}
|
|
|
|
if t.progress > 0 && t.progress < 100 {
|
|
|
|
output += fmt.Sprintf(" (%d%%)", t.progress)
|
|
|
|
}
|
2017-06-30 16:13:15 +00:00
|
|
|
if !t.success && t.count == 0 {
|
2017-11-30 16:39:56 +00:00
|
|
|
if len(os.Getenv("FZF_DEFAULT_COMMAND")) > 0 {
|
|
|
|
output = "[$FZF_DEFAULT_COMMAND failed]"
|
|
|
|
} else {
|
|
|
|
output = "[default command failed - $FZF_DEFAULT_COMMAND required]"
|
|
|
|
}
|
2017-06-30 16:13:15 +00:00
|
|
|
}
|
2017-02-18 14:17:29 +00:00
|
|
|
if pos+len(output) <= t.window.Width() {
|
|
|
|
t.window.CPrint(tui.ColInfo, 0, output)
|
|
|
|
}
|
2015-07-26 14:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:19:37 +00:00
|
|
|
func (t *Terminal) printHeader() {
|
|
|
|
if len(t.header) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
max := t.window.Height()
|
2015-07-22 05:19:45 +00:00
|
|
|
var state *ansiState
|
2015-07-21 15:19:37 +00:00
|
|
|
for idx, lineStr := range t.header {
|
2015-07-22 04:45:38 +00:00
|
|
|
line := idx + 2
|
|
|
|
if t.inlineInfo {
|
2015-08-02 04:06:15 +00:00
|
|
|
line--
|
2015-07-22 04:45:38 +00:00
|
|
|
}
|
|
|
|
if line >= max {
|
2015-07-22 12:22:59 +00:00
|
|
|
continue
|
2015-07-22 04:45:38 +00:00
|
|
|
}
|
2016-06-14 12:52:47 +00:00
|
|
|
trimmed, colors, newState := extractColor(lineStr, state, nil)
|
2015-07-22 05:19:45 +00:00
|
|
|
state = newState
|
2015-07-21 15:19:37 +00:00
|
|
|
item := &Item{
|
2017-07-21 08:29:14 +00:00
|
|
|
text: util.ToChars([]byte(trimmed)),
|
2016-08-18 17:39:32 +00:00
|
|
|
colors: colors}
|
2015-07-21 15:19:37 +00:00
|
|
|
|
|
|
|
t.move(line, 2, true)
|
2017-07-17 18:10:49 +00:00
|
|
|
t.printHighlighted(Result{item: item},
|
2017-09-09 04:50:07 +00:00
|
|
|
tui.AttrRegular, tui.ColHeader, tui.ColHeader, false, false)
|
2015-07-21 15:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func (t *Terminal) printList() {
|
|
|
|
t.constrain()
|
|
|
|
|
2015-04-21 14:50:53 +00:00
|
|
|
maxy := t.maxItems()
|
2015-01-09 16:06:08 +00:00
|
|
|
count := t.merger.Length() - t.offset
|
2017-01-07 16:30:31 +00:00
|
|
|
for j := 0; j < maxy; j++ {
|
|
|
|
i := j
|
2018-06-09 16:41:50 +00:00
|
|
|
if t.layout == layoutDefault {
|
2017-01-07 16:30:31 +00:00
|
|
|
i = maxy - 1 - j
|
|
|
|
}
|
2015-07-21 15:19:37 +00:00
|
|
|
line := i + 2 + len(t.header)
|
2015-04-21 14:50:53 +00:00
|
|
|
if t.inlineInfo {
|
2015-08-02 04:06:15 +00:00
|
|
|
line--
|
2015-04-21 14:50:53 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
if i < count {
|
2017-01-07 16:30:31 +00:00
|
|
|
t.printItem(t.merger.Get(i+t.offset), line, i, i == t.cy-t.offset)
|
|
|
|
} else if t.prevLines[i] != emptyLine {
|
|
|
|
t.prevLines[i] = emptyLine
|
|
|
|
t.move(line, 0, true)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func (t *Terminal) printItem(result Result, line int, i int, current bool) {
|
2016-08-18 17:39:32 +00:00
|
|
|
item := result.item
|
2016-01-13 12:36:44 +00:00
|
|
|
_, selected := t.selected[item.Index()]
|
2016-05-17 17:06:52 +00:00
|
|
|
label := " "
|
|
|
|
if t.jumping != jumpDisabled {
|
|
|
|
if i < len(t.jumpLabels) {
|
|
|
|
// Striped
|
|
|
|
current = i%2 == 0
|
|
|
|
label = t.jumpLabels[i : i+1]
|
|
|
|
}
|
|
|
|
} else if current {
|
|
|
|
label = ">"
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
|
|
|
|
// Avoid unnecessary redraw
|
2017-02-23 17:30:11 +00:00
|
|
|
newLine := itemLine{current: current, selected: selected, label: label,
|
2017-07-17 18:10:49 +00:00
|
|
|
result: result, queryLen: len(t.input), width: 0}
|
2017-01-15 17:26:36 +00:00
|
|
|
prevLine := t.prevLines[i]
|
|
|
|
if prevLine.current == newLine.current &&
|
|
|
|
prevLine.selected == newLine.selected &&
|
|
|
|
prevLine.label == newLine.label &&
|
2017-02-23 17:30:11 +00:00
|
|
|
prevLine.queryLen == newLine.queryLen &&
|
2017-01-15 17:26:36 +00:00
|
|
|
prevLine.result == newLine.result {
|
2017-01-07 16:30:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-09 04:50:07 +00:00
|
|
|
t.move(line, 0, false)
|
2015-01-01 19:49:30 +00:00
|
|
|
if current {
|
2019-03-17 06:37:23 +00:00
|
|
|
t.window.CPrint(tui.ColCurrentCursor, t.strong, label)
|
2015-01-01 19:49:30 +00:00
|
|
|
if selected {
|
2019-03-17 06:37:23 +00:00
|
|
|
t.window.CPrint(tui.ColCurrentSelected, t.strong, ">")
|
2015-01-01 19:49:30 +00:00
|
|
|
} else {
|
2019-03-17 06:37:23 +00:00
|
|
|
t.window.CPrint(tui.ColCurrentSelected, t.strong, " ")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-01-15 17:26:36 +00:00
|
|
|
newLine.width = t.printHighlighted(result, t.strong, tui.ColCurrent, tui.ColCurrentMatch, true, true)
|
2015-01-01 19:49:30 +00:00
|
|
|
} else {
|
2019-03-17 06:37:23 +00:00
|
|
|
t.window.CPrint(tui.ColCursor, t.strong, label)
|
2015-01-01 19:49:30 +00:00
|
|
|
if selected {
|
2016-11-15 14:57:32 +00:00
|
|
|
t.window.CPrint(tui.ColSelected, t.strong, ">")
|
2015-01-01 19:49:30 +00:00
|
|
|
} else {
|
2016-06-11 10:59:12 +00:00
|
|
|
t.window.Print(" ")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-01-15 17:26:36 +00:00
|
|
|
newLine.width = t.printHighlighted(result, 0, tui.ColNormal, tui.ColMatch, false, true)
|
|
|
|
}
|
2017-09-09 04:50:07 +00:00
|
|
|
fillSpaces := prevLine.width - newLine.width
|
|
|
|
if fillSpaces > 0 {
|
|
|
|
t.window.Print(strings.Repeat(" ", fillSpaces))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2017-01-15 17:26:36 +00:00
|
|
|
t.prevLines[i] = newLine
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (t *Terminal) trimRight(runes []rune, width int) ([]rune, int) {
|
2015-01-18 07:59:04 +00:00
|
|
|
// We start from the beginning to handle tab characters
|
|
|
|
l := 0
|
|
|
|
for idx, r := range runes {
|
2017-01-07 16:30:31 +00:00
|
|
|
l += util.RuneWidth(r, l, t.tabstop)
|
2016-12-04 17:13:47 +00:00
|
|
|
if l > width {
|
2015-01-18 07:59:04 +00:00
|
|
|
return runes[:idx], len(runes) - idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runes, 0
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (t *Terminal) displayWidthWithLimit(runes []rune, prefixWidth int, limit int) int {
|
2015-01-18 07:59:04 +00:00
|
|
|
l := 0
|
|
|
|
for _, r := range runes {
|
2017-01-07 16:30:31 +00:00
|
|
|
l += util.RuneWidth(r, l+prefixWidth, t.tabstop)
|
2015-01-18 07:59:04 +00:00
|
|
|
if l > limit {
|
|
|
|
// Early exit
|
|
|
|
return l
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-01-18 07:59:04 +00:00
|
|
|
return l
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (t *Terminal) trimLeft(runes []rune, width int) ([]rune, int32) {
|
2016-09-20 16:23:41 +00:00
|
|
|
if len(runes) > maxDisplayWidthCalc && len(runes) > width {
|
|
|
|
trimmed := len(runes) - width
|
|
|
|
return runes[trimmed:], int32(trimmed)
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
currentWidth := t.displayWidth(runes)
|
2015-01-11 18:01:24 +00:00
|
|
|
var trimmed int32
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
for currentWidth > width && len(runes) > 0 {
|
|
|
|
runes = runes[1:]
|
2015-01-11 18:01:24 +00:00
|
|
|
trimmed++
|
2017-01-07 16:30:31 +00:00
|
|
|
currentWidth = t.displayWidthWithLimit(runes, 2, width)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
return runes, trimmed
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (t *Terminal) overflow(runes []rune, max int) bool {
|
2017-01-15 17:26:36 +00:00
|
|
|
return t.displayWidthWithLimit(runes, 0, max) > max
|
2016-08-14 08:44:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func (t *Terminal) printHighlighted(result Result, attr tui.Attr, col1 tui.ColorPair, col2 tui.ColorPair, current bool, match bool) int {
|
2016-08-18 17:39:32 +00:00
|
|
|
item := result.item
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
// Overflow
|
2016-08-13 15:39:44 +00:00
|
|
|
text := make([]rune, item.text.Length())
|
|
|
|
copy(text, item.text.ToRunes())
|
2016-08-19 16:46:54 +00:00
|
|
|
matchOffsets := []Offset{}
|
2016-09-07 00:58:18 +00:00
|
|
|
var pos *[]int
|
2017-01-11 13:47:26 +00:00
|
|
|
if match && t.merger.pattern != nil {
|
2016-09-07 00:58:18 +00:00
|
|
|
_, matchOffsets, pos = t.merger.pattern.MatchItem(item, true, t.slab)
|
|
|
|
}
|
|
|
|
charOffsets := matchOffsets
|
|
|
|
if pos != nil {
|
|
|
|
charOffsets = make([]Offset, len(*pos))
|
|
|
|
for idx, p := range *pos {
|
|
|
|
offset := Offset{int32(p), int32(p + 1)}
|
|
|
|
charOffsets[idx] = offset
|
|
|
|
}
|
|
|
|
sort.Sort(ByOrder(charOffsets))
|
2016-08-19 16:46:54 +00:00
|
|
|
}
|
|
|
|
var maxe int
|
2016-09-07 00:58:18 +00:00
|
|
|
for _, offset := range charOffsets {
|
2016-08-19 16:46:54 +00:00
|
|
|
maxe = util.Max(maxe, int(offset[1]))
|
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
|
2016-10-21 10:35:59 +00:00
|
|
|
offsets := result.colorOffsets(charOffsets, t.theme, col2, attr, current)
|
2017-01-07 16:30:31 +00:00
|
|
|
maxWidth := t.window.Width() - 3
|
2016-03-01 18:06:21 +00:00
|
|
|
maxe = util.Constrain(maxe+util.Min(maxWidth/2-2, t.hscrollOff), 0, len(text))
|
2017-01-15 17:26:36 +00:00
|
|
|
displayWidth := t.displayWidthWithLimit(text, 0, maxWidth)
|
|
|
|
if displayWidth > maxWidth {
|
2015-04-16 03:56:01 +00:00
|
|
|
if t.hscroll {
|
2015-01-01 19:49:30 +00:00
|
|
|
// Stri..
|
2017-01-07 16:30:31 +00:00
|
|
|
if !t.overflow(text[:maxe], maxWidth-2) {
|
|
|
|
text, _ = t.trimRight(text, maxWidth-2)
|
2015-04-16 03:56:01 +00:00
|
|
|
text = append(text, []rune("..")...)
|
|
|
|
} else {
|
|
|
|
// Stri..
|
2017-01-07 16:30:31 +00:00
|
|
|
if t.overflow(text[maxe:], 2) {
|
2015-04-16 03:56:01 +00:00
|
|
|
text = append(text[:maxe], []rune("..")...)
|
|
|
|
}
|
|
|
|
// ..ri..
|
|
|
|
var diff int32
|
2017-01-07 16:30:31 +00:00
|
|
|
text, diff = t.trimLeft(text, maxWidth-2)
|
2015-04-16 03:56:01 +00:00
|
|
|
|
|
|
|
// Transform offsets
|
|
|
|
for idx, offset := range offsets {
|
|
|
|
b, e := offset.offset[0], offset.offset[1]
|
|
|
|
b += 2 - diff
|
|
|
|
e += 2 - diff
|
|
|
|
b = util.Max32(b, 2)
|
|
|
|
offsets[idx].offset[0] = b
|
|
|
|
offsets[idx].offset[1] = util.Max32(b, e)
|
|
|
|
}
|
|
|
|
text = append([]rune(".."), text...)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-04-16 03:56:01 +00:00
|
|
|
} else {
|
2017-01-07 16:30:31 +00:00
|
|
|
text, _ = t.trimRight(text, maxWidth-2)
|
2015-04-16 03:56:01 +00:00
|
|
|
text = append(text, []rune("..")...)
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-03-18 16:59:14 +00:00
|
|
|
for idx, offset := range offsets {
|
2015-04-16 03:56:01 +00:00
|
|
|
offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-2))
|
|
|
|
offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:39:49 +00:00
|
|
|
displayWidth = t.displayWidthWithLimit(text, 0, displayWidth)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
var index int32
|
2015-01-18 07:59:04 +00:00
|
|
|
var substr string
|
|
|
|
var prefixWidth int
|
2015-03-18 16:59:14 +00:00
|
|
|
maxOffset := int32(len(text))
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, offset := range offsets {
|
2015-03-18 16:59:14 +00:00
|
|
|
b := util.Constrain32(offset.offset[0], index, maxOffset)
|
|
|
|
e := util.Constrain32(offset.offset[1], index, maxOffset)
|
2015-01-18 07:59:04 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
substr, prefixWidth = t.processTabs(text[index:b], prefixWidth)
|
2016-09-28 15:54:27 +00:00
|
|
|
t.window.CPrint(col1, attr, substr)
|
2015-01-18 07:59:04 +00:00
|
|
|
|
2015-03-19 03:14:26 +00:00
|
|
|
if b < e {
|
2017-01-07 16:30:31 +00:00
|
|
|
substr, prefixWidth = t.processTabs(text[b:e], prefixWidth)
|
2016-09-28 15:54:27 +00:00
|
|
|
t.window.CPrint(offset.color, offset.attr, substr)
|
2015-03-19 03:14:26 +00:00
|
|
|
}
|
2015-01-18 07:59:04 +00:00
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
index = e
|
2015-03-18 16:59:14 +00:00
|
|
|
if index >= maxOffset {
|
|
|
|
break
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-03-18 16:59:14 +00:00
|
|
|
if index < maxOffset {
|
2017-01-07 16:30:31 +00:00
|
|
|
substr, _ = t.processTabs(text[index:], prefixWidth)
|
2016-09-28 15:54:27 +00:00
|
|
|
t.window.CPrint(col1, attr, substr)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
2017-01-15 17:26:36 +00:00
|
|
|
return displayWidth
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-24 17:02:00 +00:00
|
|
|
func numLinesMax(str string, max int) int {
|
|
|
|
lines := 0
|
|
|
|
for lines < max {
|
|
|
|
idx := strings.Index(str, "\n")
|
|
|
|
if idx < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
str = str[idx+1:]
|
|
|
|
lines++
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2016-06-11 10:59:12 +00:00
|
|
|
func (t *Terminal) printPreview() {
|
2017-01-23 15:23:16 +00:00
|
|
|
if !t.hasPreviewWindow() {
|
2016-09-28 17:53:05 +00:00
|
|
|
return
|
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
t.pwindow.Erase()
|
2017-01-11 13:13:40 +00:00
|
|
|
|
|
|
|
maxWidth := t.pwindow.Width()
|
|
|
|
if t.tui.DoesAutoWrap() {
|
|
|
|
maxWidth -= 1
|
|
|
|
}
|
|
|
|
reader := bufio.NewReader(strings.NewReader(t.previewer.text))
|
|
|
|
lineNo := -t.previewer.offset
|
2017-07-19 13:46:16 +00:00
|
|
|
height := t.pwindow.Height()
|
2019-03-29 09:28:45 +00:00
|
|
|
t.previewer.more = t.previewer.offset > 0
|
2017-05-27 17:26:42 +00:00
|
|
|
var ansi *ansiState
|
2019-03-29 09:28:45 +00:00
|
|
|
for ; ; lineNo++ {
|
2017-01-11 13:13:40 +00:00
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
eof := err == io.EOF
|
|
|
|
if !eof {
|
|
|
|
line = line[:len(line)-1]
|
2016-09-24 17:02:00 +00:00
|
|
|
}
|
2019-03-29 09:28:45 +00:00
|
|
|
if lineNo >= height || t.pwindow.Y() == height-1 && t.pwindow.X() > 0 {
|
2017-01-11 13:13:40 +00:00
|
|
|
break
|
2019-03-29 09:28:45 +00:00
|
|
|
} else if lineNo >= 0 {
|
2017-01-11 13:13:40 +00:00
|
|
|
var fillRet tui.FillReturn
|
2018-12-22 02:52:18 +00:00
|
|
|
prefixWidth := 0
|
2017-05-27 17:26:42 +00:00
|
|
|
_, _, ansi = extractColor(line, ansi, func(str string, ansi *ansiState) bool {
|
2017-01-11 13:13:40 +00:00
|
|
|
trimmed := []rune(str)
|
|
|
|
if !t.preview.wrap {
|
|
|
|
trimmed, _ = t.trimRight(trimmed, maxWidth-t.pwindow.X())
|
|
|
|
}
|
2018-12-22 02:52:18 +00:00
|
|
|
str, width := t.processTabs(trimmed, prefixWidth)
|
|
|
|
prefixWidth += width
|
2017-03-02 03:49:51 +00:00
|
|
|
if t.theme != nil && ansi != nil && ansi.colored() {
|
2017-01-11 13:13:40 +00:00
|
|
|
fillRet = t.pwindow.CFill(ansi.fg, ansi.bg, ansi.attr, str)
|
|
|
|
} else {
|
2017-09-08 09:15:31 +00:00
|
|
|
fillRet = t.pwindow.CFill(tui.ColNormal.Fg(), tui.ColNormal.Bg(), tui.AttrRegular, str)
|
2017-01-11 13:13:40 +00:00
|
|
|
}
|
|
|
|
return fillRet == tui.FillContinue
|
|
|
|
})
|
2019-05-21 07:35:45 +00:00
|
|
|
t.previewer.more = t.previewer.more || t.pwindow.Y() == height-1 && t.pwindow.X() == t.pwindow.Width()
|
2019-03-29 08:29:24 +00:00
|
|
|
if fillRet == tui.FillNextLine {
|
2017-01-11 13:13:40 +00:00
|
|
|
continue
|
2019-03-29 08:29:24 +00:00
|
|
|
} else if fillRet == tui.FillSuspend {
|
2017-01-11 13:13:40 +00:00
|
|
|
break
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2017-01-11 13:13:40 +00:00
|
|
|
t.pwindow.Fill("\n")
|
2016-12-04 17:13:47 +00:00
|
|
|
}
|
2017-01-11 13:13:40 +00:00
|
|
|
if eof {
|
|
|
|
break
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
2017-01-11 13:13:40 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
t.pwindow.FinishFill()
|
2017-07-19 13:46:16 +00:00
|
|
|
if t.previewer.lines > height {
|
2019-03-29 09:28:45 +00:00
|
|
|
t.previewer.more = true
|
2016-10-03 16:40:45 +00:00
|
|
|
offset := fmt.Sprintf("%d/%d", t.previewer.offset+1, t.previewer.lines)
|
2017-01-07 16:30:31 +00:00
|
|
|
pos := t.pwindow.Width() - len(offset)
|
|
|
|
if t.tui.DoesAutoWrap() {
|
|
|
|
pos -= 1
|
|
|
|
}
|
|
|
|
t.pwindow.Move(0, pos)
|
2016-10-24 00:44:56 +00:00
|
|
|
t.pwindow.CPrint(tui.ColInfo, tui.Reverse, offset)
|
2016-10-03 16:40:45 +00:00
|
|
|
}
|
2015-01-18 07:59:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (t *Terminal) processTabs(runes []rune, prefixWidth int) (string, int) {
|
2015-01-18 07:59:04 +00:00
|
|
|
var strbuf bytes.Buffer
|
|
|
|
l := prefixWidth
|
|
|
|
for _, r := range runes {
|
2017-01-07 16:30:31 +00:00
|
|
|
w := util.RuneWidth(r, l, t.tabstop)
|
2015-01-18 07:59:04 +00:00
|
|
|
l += w
|
|
|
|
if r == '\t' {
|
|
|
|
strbuf.WriteString(strings.Repeat(" ", w))
|
|
|
|
} else {
|
|
|
|
strbuf.WriteRune(r)
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-01-18 07:59:04 +00:00
|
|
|
return strbuf.String(), l
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printAll() {
|
2016-06-11 10:59:12 +00:00
|
|
|
t.resizeWindows()
|
2015-01-01 19:49:30 +00:00
|
|
|
t.printList()
|
|
|
|
t.printPrompt()
|
2015-04-25 14:20:40 +00:00
|
|
|
t.printInfo()
|
2015-07-21 15:47:14 +00:00
|
|
|
t.printHeader()
|
2016-09-28 17:53:05 +00:00
|
|
|
t.printPreview()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) refresh() {
|
2019-03-29 17:06:54 +00:00
|
|
|
t.placeCursor()
|
2015-01-07 03:46:45 +00:00
|
|
|
if !t.suppress {
|
2017-02-04 12:51:22 +00:00
|
|
|
windows := make([]tui.Window, 0, 4)
|
|
|
|
if t.bordered {
|
|
|
|
windows = append(windows, t.border)
|
|
|
|
}
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewWindow() {
|
2017-02-04 12:51:22 +00:00
|
|
|
windows = append(windows, t.pborder, t.pwindow)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
2017-02-04 12:51:22 +00:00
|
|
|
windows = append(windows, t.window)
|
|
|
|
t.tui.RefreshWindows(windows)
|
2015-01-07 03:46:45 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) delChar() bool {
|
|
|
|
if len(t.input) > 0 && t.cx < len(t.input) {
|
|
|
|
t.input = append(t.input[:t.cx], t.input[t.cx+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func findLastMatch(pattern string, str string) int {
|
|
|
|
rx, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
locs := rx.FindAllStringIndex(str, -1)
|
|
|
|
if locs == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return locs[len(locs)-1][0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func findFirstMatch(pattern string, str string) int {
|
|
|
|
rx, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
loc := rx.FindStringIndex(str)
|
|
|
|
if loc == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return loc[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func copySlice(slice []rune) []rune {
|
|
|
|
ret := make([]rune, len(slice))
|
|
|
|
copy(ret, slice)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) rubout(pattern string) {
|
|
|
|
pcx := t.cx
|
|
|
|
after := t.input[t.cx:]
|
|
|
|
t.cx = findLastMatch(pattern, string(t.input[:t.cx])) + 1
|
|
|
|
t.yanked = copySlice(t.input[t.cx:pcx])
|
|
|
|
t.input = append(t.input[:t.cx], after...)
|
|
|
|
}
|
|
|
|
|
2016-10-24 00:44:56 +00:00
|
|
|
func keyMatch(key int, event tui.Event) bool {
|
2016-04-13 19:18:59 +00:00
|
|
|
return event.Type == key ||
|
2016-10-24 00:44:56 +00:00
|
|
|
event.Type == tui.Rune && int(event.Char) == key-tui.AltZ ||
|
|
|
|
event.Type == tui.Mouse && key == tui.DoubleClick && event.MouseEvent.Double
|
2015-03-31 13:05:02 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 15:36:19 +00:00
|
|
|
func quoteEntryCmd(entry string) string {
|
|
|
|
escaped := strings.Replace(entry, `\`, `\\`, -1)
|
|
|
|
escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`
|
|
|
|
r, _ := regexp.Compile(`[&|<>()@^%!"]`)
|
|
|
|
return r.ReplaceAllStringFunc(escaped, func(match string) string {
|
|
|
|
return "^" + match
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-08 16:42:01 +00:00
|
|
|
func quoteEntry(entry string) string {
|
2016-11-06 17:15:34 +00:00
|
|
|
if util.IsWindows() {
|
2017-10-02 15:36:19 +00:00
|
|
|
return quoteEntryCmd(entry)
|
2016-11-06 17:15:34 +00:00
|
|
|
}
|
2016-06-07 15:54:21 +00:00
|
|
|
return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"
|
2015-11-08 16:42:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 02:47:46 +00:00
|
|
|
func parsePlaceholder(match string) (bool, string, placeholderFlags) {
|
|
|
|
flags := placeholderFlags{}
|
|
|
|
|
|
|
|
if match[0] == '\\' {
|
|
|
|
// Escaped placeholder pattern
|
|
|
|
return true, match[1:], flags
|
|
|
|
}
|
|
|
|
|
|
|
|
skipChars := 1
|
|
|
|
for _, char := range match[1:] {
|
|
|
|
switch char {
|
|
|
|
case '+':
|
|
|
|
flags.plus = true
|
|
|
|
skipChars++
|
|
|
|
case 's':
|
|
|
|
flags.preserveSpace = true
|
|
|
|
skipChars++
|
2019-02-18 16:12:57 +00:00
|
|
|
case 'n':
|
|
|
|
flags.number = true
|
|
|
|
skipChars++
|
2018-06-09 16:40:22 +00:00
|
|
|
case 'q':
|
|
|
|
flags.query = true
|
2018-03-30 02:47:46 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matchWithoutFlags := "{" + match[skipChars:]
|
|
|
|
|
|
|
|
return false, matchWithoutFlags, flags
|
|
|
|
}
|
|
|
|
|
2018-06-09 16:40:22 +00:00
|
|
|
func hasPreviewFlags(template string) (plus bool, query bool) {
|
2017-01-27 07:38:42 +00:00
|
|
|
for _, match := range placeholder.FindAllString(template, -1) {
|
2018-03-30 02:47:46 +00:00
|
|
|
_, _, flags := parsePlaceholder(match)
|
|
|
|
if flags.plus {
|
2018-06-09 16:40:22 +00:00
|
|
|
plus = true
|
|
|
|
}
|
|
|
|
if flags.query {
|
|
|
|
query = true
|
2017-01-27 07:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-09 16:40:22 +00:00
|
|
|
return
|
2017-01-27 07:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, forcePlus bool, query string, allItems []*Item) string {
|
|
|
|
current := allItems[:1]
|
|
|
|
selected := allItems[1:]
|
|
|
|
if current[0] == nil {
|
|
|
|
current = []*Item{}
|
|
|
|
}
|
|
|
|
if selected[0] == nil {
|
|
|
|
selected = []*Item{}
|
|
|
|
}
|
2016-10-03 05:16:10 +00:00
|
|
|
return placeholder.ReplaceAllStringFunc(template, func(match string) string {
|
2018-03-30 02:47:46 +00:00
|
|
|
escaped, match, flags := parsePlaceholder(match)
|
|
|
|
|
|
|
|
if escaped {
|
|
|
|
return match
|
2016-10-03 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Current query
|
|
|
|
if match == "{q}" {
|
|
|
|
return quoteEntry(query)
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:38:42 +00:00
|
|
|
items := current
|
2018-03-30 02:47:46 +00:00
|
|
|
if flags.plus || forcePlus {
|
2017-01-27 07:38:42 +00:00
|
|
|
items = selected
|
|
|
|
}
|
|
|
|
|
2016-10-03 05:16:10 +00:00
|
|
|
replacements := make([]string, len(items))
|
|
|
|
|
|
|
|
if match == "{}" {
|
|
|
|
for idx, item := range items {
|
2019-02-18 16:12:57 +00:00
|
|
|
if flags.number {
|
|
|
|
n := int(item.text.Index)
|
|
|
|
if n < 0 {
|
|
|
|
replacements[idx] = ""
|
|
|
|
} else {
|
|
|
|
replacements[idx] = strconv.Itoa(n)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
replacements[idx] = quoteEntry(item.AsString(stripAnsi))
|
|
|
|
}
|
2016-10-03 05:16:10 +00:00
|
|
|
}
|
|
|
|
return strings.Join(replacements, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens := strings.Split(match[1:len(match)-1], ",")
|
|
|
|
ranges := make([]Range, len(tokens))
|
|
|
|
for idx, s := range tokens {
|
|
|
|
r, ok := ParseRange(&s)
|
|
|
|
if !ok {
|
|
|
|
// Invalid expression, just return the original string in the template
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
ranges[idx] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, item := range items {
|
2017-07-21 08:29:14 +00:00
|
|
|
tokens := Tokenize(item.AsString(stripAnsi), delimiter)
|
2016-10-03 05:16:10 +00:00
|
|
|
trans := Transform(tokens, ranges)
|
|
|
|
str := string(joinTokens(trans))
|
|
|
|
if delimiter.str != nil {
|
|
|
|
str = strings.TrimSuffix(str, *delimiter.str)
|
|
|
|
} else if delimiter.regex != nil {
|
|
|
|
delims := delimiter.regex.FindAllStringIndex(str, -1)
|
|
|
|
if len(delims) > 0 && delims[len(delims)-1][1] == len(str) {
|
|
|
|
str = str[:delims[len(delims)-1][0]]
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 02:47:46 +00:00
|
|
|
if !flags.preserveSpace {
|
|
|
|
str = strings.TrimSpace(str)
|
|
|
|
}
|
2016-10-03 05:16:10 +00:00
|
|
|
replacements[idx] = quoteEntry(str)
|
|
|
|
}
|
|
|
|
return strings.Join(replacements, " ")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:58:08 +00:00
|
|
|
func (t *Terminal) redraw() {
|
|
|
|
t.tui.Clear()
|
|
|
|
t.tui.Refresh()
|
|
|
|
t.printAll()
|
|
|
|
}
|
|
|
|
|
2017-01-27 08:46:56 +00:00
|
|
|
func (t *Terminal) executeCommand(template string, forcePlus bool, background bool) {
|
2017-01-27 07:38:42 +00:00
|
|
|
valid, list := t.buildPlusList(template, forcePlus)
|
|
|
|
if !valid {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
command := replacePlaceholder(template, t.ansi, t.delimiter, forcePlus, string(t.input), list)
|
2018-09-27 06:27:08 +00:00
|
|
|
cmd := util.ExecCommand(command, false)
|
2017-01-27 08:46:56 +00:00
|
|
|
if !background {
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2017-04-28 13:58:08 +00:00
|
|
|
t.tui.Pause(true)
|
2017-01-27 08:46:56 +00:00
|
|
|
cmd.Run()
|
2017-04-28 13:58:08 +00:00
|
|
|
t.tui.Resume(true)
|
|
|
|
t.redraw()
|
2017-01-27 08:46:56 +00:00
|
|
|
t.refresh()
|
|
|
|
} else {
|
2018-09-27 02:10:49 +00:00
|
|
|
t.tui.Pause(false)
|
2017-01-27 08:46:56 +00:00
|
|
|
cmd.Run()
|
2018-09-27 02:10:49 +00:00
|
|
|
t.tui.Resume(false)
|
2017-01-27 08:46:56 +00:00
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 15:23:16 +00:00
|
|
|
func (t *Terminal) hasPreviewer() bool {
|
2016-06-11 10:59:12 +00:00
|
|
|
return t.previewBox != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) isPreviewEnabled() bool {
|
2017-01-23 15:23:16 +00:00
|
|
|
return t.hasPreviewer() && t.previewer.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) hasPreviewWindow() bool {
|
|
|
|
return t.pwindow != nil && t.isPreviewEnabled()
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 05:16:10 +00:00
|
|
|
func (t *Terminal) currentItem() *Item {
|
2017-01-27 07:38:42 +00:00
|
|
|
cnt := t.merger.Length()
|
|
|
|
if cnt > 0 && cnt > t.cy {
|
|
|
|
return t.merger.Get(t.cy).item
|
|
|
|
}
|
|
|
|
return nil
|
2016-10-03 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 07:38:42 +00:00
|
|
|
func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {
|
|
|
|
current := t.currentItem()
|
2018-06-09 16:40:22 +00:00
|
|
|
plus, query := hasPreviewFlags(template)
|
|
|
|
if !(query && len(t.input) > 0 || (forcePlus || plus) && len(t.selected) > 0) {
|
2017-01-27 07:38:42 +00:00
|
|
|
return current != nil, []*Item{current, current}
|
|
|
|
}
|
2018-06-09 16:40:22 +00:00
|
|
|
|
|
|
|
// We would still want to update preview window even if there is no match if
|
|
|
|
// 1. command template contains {q} and the query string is not empty
|
|
|
|
// 2. or it contains {+} and we have more than one item already selected.
|
|
|
|
// To do so, we pass an empty Item instead of nil to trigger an update.
|
|
|
|
if current == nil {
|
2019-02-18 16:12:57 +00:00
|
|
|
current = &minItem
|
2018-06-09 16:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sels []*Item
|
|
|
|
if len(t.selected) == 0 {
|
|
|
|
sels = []*Item{current, current}
|
|
|
|
} else {
|
|
|
|
sels = make([]*Item, len(t.selected)+1)
|
|
|
|
sels[0] = current
|
|
|
|
for i, sel := range t.sortSelected() {
|
|
|
|
sels[i+1] = sel.item
|
|
|
|
}
|
2017-01-27 07:38:42 +00:00
|
|
|
}
|
|
|
|
return true, sels
|
2015-06-14 03:25:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 16:39:25 +00:00
|
|
|
func (t *Terminal) selectItem(item *Item) {
|
|
|
|
t.selected[item.Index()] = selectedItem{time.Now(), item}
|
|
|
|
t.version++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) deselectItem(item *Item) {
|
|
|
|
delete(t.selected, item.Index())
|
|
|
|
t.version++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) toggleItem(item *Item) {
|
|
|
|
if _, found := t.selected[item.Index()]; !found {
|
|
|
|
t.selectItem(item)
|
|
|
|
} else {
|
|
|
|
t.deselectItem(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 06:27:08 +00:00
|
|
|
func (t *Terminal) killPreview(code int) {
|
|
|
|
select {
|
|
|
|
case t.killChan <- code:
|
|
|
|
default:
|
|
|
|
if code != exitCancel {
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) cancelPreview() {
|
|
|
|
t.killPreview(exitCancel)
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Loop is called to start Terminal I/O
|
2015-01-01 19:49:30 +00:00
|
|
|
func (t *Terminal) Loop() {
|
2016-09-07 00:58:18 +00:00
|
|
|
// prof := profile.Start(profile.ProfilePath("/tmp/"))
|
2015-02-17 15:08:17 +00:00
|
|
|
<-t.startChan
|
2015-01-01 19:49:30 +00:00
|
|
|
{ // Late initialization
|
2015-10-05 14:19:26 +00:00
|
|
|
intChan := make(chan os.Signal, 1)
|
2016-02-02 08:51:21 +00:00
|
|
|
signal.Notify(intChan, os.Interrupt, os.Kill, syscall.SIGTERM)
|
2015-10-05 14:19:26 +00:00
|
|
|
go func() {
|
|
|
|
<-intChan
|
|
|
|
t.reqBox.Set(reqQuit, nil)
|
|
|
|
}()
|
|
|
|
|
2017-04-28 13:58:08 +00:00
|
|
|
contChan := make(chan os.Signal, 1)
|
|
|
|
notifyOnCont(contChan)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-contChan
|
|
|
|
t.reqBox.Set(reqReinit, nil)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-01-23 11:30:50 +00:00
|
|
|
resizeChan := make(chan os.Signal, 1)
|
2016-10-24 00:44:56 +00:00
|
|
|
notifyOnResize(resizeChan) // Non-portable
|
2015-01-23 11:30:50 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-resizeChan
|
|
|
|
t.reqBox.Set(reqRedraw, nil)
|
|
|
|
}
|
|
|
|
}()
|
2015-06-17 15:42:38 +00:00
|
|
|
|
2015-11-03 13:59:57 +00:00
|
|
|
t.mutex.Lock()
|
|
|
|
t.initFunc()
|
2016-06-11 10:59:12 +00:00
|
|
|
t.resizeWindows()
|
2015-11-03 13:59:57 +00:00
|
|
|
t.printPrompt()
|
|
|
|
t.printInfo()
|
|
|
|
t.printHeader()
|
2018-06-25 10:07:12 +00:00
|
|
|
t.refresh()
|
2015-11-03 13:59:57 +00:00
|
|
|
t.mutex.Unlock()
|
|
|
|
go func() {
|
2016-01-16 09:07:50 +00:00
|
|
|
timer := time.NewTimer(t.initDelay)
|
2015-11-03 13:59:57 +00:00
|
|
|
<-timer.C
|
|
|
|
t.reqBox.Set(reqRefresh, nil)
|
|
|
|
}()
|
|
|
|
|
2015-06-17 15:42:38 +00:00
|
|
|
// Keep the spinner spinning
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
t.mutex.Lock()
|
|
|
|
reading := t.reading
|
|
|
|
t.mutex.Unlock()
|
|
|
|
if !reading {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(spinnerDuration)
|
|
|
|
t.reqBox.Set(reqInfo, nil)
|
|
|
|
}
|
|
|
|
}()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewer() {
|
2016-06-11 10:59:12 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2017-01-27 07:38:42 +00:00
|
|
|
var request []*Item
|
2016-06-11 10:59:12 +00:00
|
|
|
t.previewBox.Wait(func(events *util.Events) {
|
|
|
|
for req, value := range *events {
|
|
|
|
switch req {
|
|
|
|
case reqPreviewEnqueue:
|
2017-01-27 07:38:42 +00:00
|
|
|
request = value.([]*Item)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
events.Clear()
|
|
|
|
})
|
2017-01-27 07:38:42 +00:00
|
|
|
// We don't display preview window if no match
|
|
|
|
if request[0] != nil {
|
2016-10-03 05:16:10 +00:00
|
|
|
command := replacePlaceholder(t.preview.command,
|
2017-01-27 07:38:42 +00:00
|
|
|
t.ansi, t.delimiter, false, string(t.input), request)
|
2018-09-27 06:27:08 +00:00
|
|
|
cmd := util.ExecCommand(command, true)
|
2017-11-30 18:27:58 +00:00
|
|
|
if t.pwindow != nil {
|
|
|
|
env := os.Environ()
|
2019-02-22 05:33:29 +00:00
|
|
|
lines := fmt.Sprintf("LINES=%d", t.pwindow.Height())
|
|
|
|
columns := fmt.Sprintf("COLUMNS=%d", t.pwindow.Width())
|
|
|
|
env = append(env, lines)
|
|
|
|
env = append(env, "FZF_PREVIEW_"+lines)
|
|
|
|
env = append(env, columns)
|
|
|
|
env = append(env, "FZF_PREVIEW_"+columns)
|
2017-11-30 18:27:58 +00:00
|
|
|
cmd.Env = env
|
|
|
|
}
|
2018-09-27 06:27:08 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &out
|
|
|
|
cmd.Start()
|
|
|
|
finishChan := make(chan bool, 1)
|
|
|
|
updateChan := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case code := <-t.killChan:
|
|
|
|
if code != exitCancel {
|
|
|
|
util.KillCommand(cmd)
|
|
|
|
os.Exit(code)
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case <-time.After(previewCancelWait):
|
|
|
|
util.KillCommand(cmd)
|
|
|
|
updateChan <- true
|
|
|
|
case <-finishChan:
|
|
|
|
updateChan <- false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-finishChan:
|
|
|
|
updateChan <- false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
cmd.Wait()
|
|
|
|
finishChan <- true
|
|
|
|
if out.Len() > 0 || !<-updateChan {
|
|
|
|
t.reqBox.Set(reqPreviewDisplay, string(out.Bytes()))
|
|
|
|
}
|
2016-06-15 04:03:42 +00:00
|
|
|
} else {
|
|
|
|
t.reqBox.Set(reqPreviewDisplay, "")
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-07-19 04:17:06 +00:00
|
|
|
exit := func(getCode func() int) {
|
2017-07-18 11:50:38 +00:00
|
|
|
if !t.cleanExit && t.fullscreen && t.inlineInfo {
|
|
|
|
t.placeCursor()
|
|
|
|
}
|
|
|
|
t.tui.Close()
|
2017-07-19 04:17:06 +00:00
|
|
|
code := getCode()
|
2015-09-15 04:21:51 +00:00
|
|
|
if code <= exitNoMatch && t.history != nil {
|
2015-06-13 15:43:44 +00:00
|
|
|
t.history.append(string(t.input))
|
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
// prof.Stop()
|
2018-09-27 06:27:08 +00:00
|
|
|
t.killPreview(code)
|
2015-06-13 15:43:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
go func() {
|
2018-12-13 01:58:57 +00:00
|
|
|
var focusedIndex int32 = minItem.Index()
|
2018-12-05 09:45:55 +00:00
|
|
|
var version int64 = -1
|
2015-01-01 19:49:30 +00:00
|
|
|
for {
|
2015-01-12 03:56:17 +00:00
|
|
|
t.reqBox.Wait(func(events *util.Events) {
|
2015-01-01 19:49:30 +00:00
|
|
|
defer events.Clear()
|
|
|
|
t.mutex.Lock()
|
2016-06-11 10:59:12 +00:00
|
|
|
for req, value := range *events {
|
2015-01-01 19:49:30 +00:00
|
|
|
switch req {
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqPrompt:
|
2015-01-01 19:49:30 +00:00
|
|
|
t.printPrompt()
|
2015-04-21 14:50:53 +00:00
|
|
|
if t.inlineInfo {
|
|
|
|
t.printInfo()
|
|
|
|
}
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqInfo:
|
2015-01-01 19:49:30 +00:00
|
|
|
t.printInfo()
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqList:
|
2015-01-01 19:49:30 +00:00
|
|
|
t.printList()
|
2018-12-13 01:58:57 +00:00
|
|
|
var currentIndex int32 = minItem.Index()
|
|
|
|
currentItem := t.currentItem()
|
|
|
|
if currentItem != nil {
|
|
|
|
currentIndex = currentItem.Index()
|
|
|
|
}
|
|
|
|
if focusedIndex != currentIndex || version != t.version {
|
2017-07-27 16:39:25 +00:00
|
|
|
version = t.version
|
2018-12-13 01:58:57 +00:00
|
|
|
focusedIndex = currentIndex
|
2016-06-15 04:03:42 +00:00
|
|
|
if t.isPreviewEnabled() {
|
2017-01-27 07:38:42 +00:00
|
|
|
_, list := t.buildPlusList(t.preview.command, false)
|
2018-09-27 06:27:08 +00:00
|
|
|
t.cancelPreview()
|
2017-01-27 07:38:42 +00:00
|
|
|
t.previewBox.Set(reqPreviewEnqueue, list)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 17:06:52 +00:00
|
|
|
case reqJump:
|
|
|
|
if t.merger.Length() == 0 {
|
|
|
|
t.jumping = jumpDisabled
|
|
|
|
}
|
|
|
|
t.printList()
|
2015-07-21 18:21:20 +00:00
|
|
|
case reqHeader:
|
|
|
|
t.printHeader()
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqRefresh:
|
2015-01-07 03:46:45 +00:00
|
|
|
t.suppress = false
|
2017-04-28 13:58:08 +00:00
|
|
|
case reqReinit:
|
|
|
|
t.tui.Resume(t.fullscreen)
|
|
|
|
t.redraw()
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqRedraw:
|
2017-04-28 13:58:08 +00:00
|
|
|
t.redraw()
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqClose:
|
2017-07-19 04:17:06 +00:00
|
|
|
exit(func() int {
|
|
|
|
if t.output() {
|
|
|
|
return exitOk
|
|
|
|
}
|
|
|
|
return exitNoMatch
|
|
|
|
})
|
2016-06-11 10:59:12 +00:00
|
|
|
case reqPreviewDisplay:
|
2016-09-24 17:02:00 +00:00
|
|
|
t.previewer.text = value.(string)
|
|
|
|
t.previewer.lines = strings.Count(t.previewer.text, "\n")
|
|
|
|
t.previewer.offset = 0
|
|
|
|
t.printPreview()
|
|
|
|
case reqPreviewRefresh:
|
2016-06-11 10:59:12 +00:00
|
|
|
t.printPreview()
|
2016-05-12 15:51:15 +00:00
|
|
|
case reqPrintQuery:
|
2017-07-19 04:17:06 +00:00
|
|
|
exit(func() int {
|
|
|
|
t.printer(string(t.input))
|
|
|
|
return exitOk
|
|
|
|
})
|
2015-01-11 18:01:24 +00:00
|
|
|
case reqQuit:
|
2017-07-19 04:17:06 +00:00
|
|
|
exit(func() int { return exitInterrupt })
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-25 10:07:12 +00:00
|
|
|
t.refresh()
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Unlock()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
looping := true
|
|
|
|
for looping {
|
2017-01-07 16:30:31 +00:00
|
|
|
event := t.tui.GetChar()
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
t.mutex.Lock()
|
|
|
|
previousInput := t.input
|
2019-03-29 06:02:31 +00:00
|
|
|
previousCx := t.cx
|
|
|
|
events := []util.EventType{}
|
2015-01-12 03:56:17 +00:00
|
|
|
req := func(evts ...util.EventType) {
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, event := range evts {
|
|
|
|
events = append(events, event)
|
2015-01-11 18:01:24 +00:00
|
|
|
if event == reqClose || event == reqQuit {
|
2015-01-01 19:49:30 +00:00
|
|
|
looping = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-09 14:44:54 +00:00
|
|
|
toggle := func() {
|
|
|
|
if t.cy < t.merger.Length() {
|
2017-07-27 16:39:25 +00:00
|
|
|
t.toggleItem(t.merger.Get(t.cy).item)
|
2015-01-11 18:01:24 +00:00
|
|
|
req(reqInfo)
|
2015-01-10 03:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 19:12:44 +00:00
|
|
|
scrollPreview := func(amount int) {
|
2019-03-29 09:28:45 +00:00
|
|
|
if !t.previewer.more {
|
|
|
|
return
|
|
|
|
}
|
2019-03-29 06:12:46 +00:00
|
|
|
newOffset := util.Constrain(
|
2016-10-03 16:39:48 +00:00
|
|
|
t.previewer.offset+amount, 0, t.previewer.lines-1)
|
2019-03-29 06:12:46 +00:00
|
|
|
if t.previewer.offset != newOffset {
|
|
|
|
t.previewer.offset = newOffset
|
|
|
|
req(reqPreviewRefresh)
|
|
|
|
}
|
2016-09-24 19:12:44 +00:00
|
|
|
}
|
2015-06-18 15:31:48 +00:00
|
|
|
for key, ret := range t.expect {
|
2015-03-31 13:05:02 +00:00
|
|
|
if keyMatch(key, event) {
|
2015-06-18 15:31:48 +00:00
|
|
|
t.pressed = ret
|
2016-09-17 19:45:21 +00:00
|
|
|
t.reqBox.Set(reqClose, nil)
|
|
|
|
t.mutex.Unlock()
|
|
|
|
return
|
2015-03-28 17:59:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-20 12:25:15 +00:00
|
|
|
|
2017-01-21 17:32:49 +00:00
|
|
|
var doAction func(action, int) bool
|
|
|
|
doActions := func(actions []action, mapkey int) bool {
|
|
|
|
for _, action := range actions {
|
|
|
|
if !doAction(action, mapkey) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
doAction = func(a action, mapkey int) bool {
|
|
|
|
switch a.t {
|
2015-10-12 17:24:38 +00:00
|
|
|
case actIgnore:
|
2017-01-27 08:46:56 +00:00
|
|
|
case actExecute, actExecuteSilent:
|
|
|
|
t.executeCommand(a.a, false, a.t == actExecuteSilent)
|
2015-11-08 16:42:01 +00:00
|
|
|
case actExecuteMulti:
|
2017-01-27 08:46:56 +00:00
|
|
|
t.executeCommand(a.a, true, false)
|
2015-10-12 17:24:38 +00:00
|
|
|
case actInvalid:
|
|
|
|
t.mutex.Unlock()
|
|
|
|
return false
|
2016-06-11 10:59:12 +00:00
|
|
|
case actTogglePreview:
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewer() {
|
2016-09-24 17:02:00 +00:00
|
|
|
t.previewer.enabled = !t.previewer.enabled
|
2017-01-22 08:43:27 +00:00
|
|
|
t.tui.Clear()
|
2016-06-11 10:59:12 +00:00
|
|
|
t.resizeWindows()
|
2017-01-27 07:38:42 +00:00
|
|
|
if t.previewer.enabled {
|
|
|
|
valid, list := t.buildPlusList(t.preview.command, false)
|
|
|
|
if valid {
|
2018-09-27 06:27:08 +00:00
|
|
|
t.cancelPreview()
|
2017-01-27 07:38:42 +00:00
|
|
|
t.previewBox.Set(reqPreviewEnqueue, list)
|
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
2019-03-29 17:06:54 +00:00
|
|
|
req(reqPrompt, reqList, reqInfo, reqHeader)
|
2016-06-11 10:59:12 +00:00
|
|
|
}
|
2017-02-18 14:49:00 +00:00
|
|
|
case actTogglePreviewWrap:
|
|
|
|
if t.hasPreviewWindow() {
|
|
|
|
t.preview.wrap = !t.preview.wrap
|
|
|
|
req(reqPreviewRefresh)
|
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actToggleSort:
|
|
|
|
t.sort = !t.sort
|
|
|
|
t.eventBox.Set(EvtSearchNew, t.sort)
|
|
|
|
t.mutex.Unlock()
|
|
|
|
return false
|
2016-09-24 17:02:00 +00:00
|
|
|
case actPreviewUp:
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewWindow() {
|
2016-09-24 19:12:44 +00:00
|
|
|
scrollPreview(-1)
|
2016-09-24 17:02:00 +00:00
|
|
|
}
|
|
|
|
case actPreviewDown:
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewWindow() {
|
2016-09-24 19:12:44 +00:00
|
|
|
scrollPreview(1)
|
|
|
|
}
|
|
|
|
case actPreviewPageUp:
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewWindow() {
|
2017-01-07 16:30:31 +00:00
|
|
|
scrollPreview(-t.pwindow.Height())
|
2016-09-24 19:12:44 +00:00
|
|
|
}
|
|
|
|
case actPreviewPageDown:
|
2017-01-23 15:23:16 +00:00
|
|
|
if t.hasPreviewWindow() {
|
2017-01-07 16:30:31 +00:00
|
|
|
scrollPreview(t.pwindow.Height())
|
2016-09-24 17:02:00 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actBeginningOfLine:
|
2015-07-23 12:05:33 +00:00
|
|
|
t.cx = 0
|
2015-10-12 17:24:38 +00:00
|
|
|
case actBackwardChar:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.cx--
|
2015-06-09 14:44:54 +00:00
|
|
|
}
|
2016-05-12 15:51:15 +00:00
|
|
|
case actPrintQuery:
|
|
|
|
req(reqPrintQuery)
|
2017-12-01 04:08:55 +00:00
|
|
|
case actReplaceQuery:
|
2017-12-03 14:48:59 +00:00
|
|
|
if t.cy >= 0 && t.cy < t.merger.Length() {
|
2017-12-01 04:08:55 +00:00
|
|
|
t.input = t.merger.Get(t.cy).item.text.ToRunes()
|
|
|
|
t.cx = len(t.input)
|
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actAbort:
|
|
|
|
req(reqQuit)
|
|
|
|
case actDeleteChar:
|
|
|
|
t.delChar()
|
|
|
|
case actDeleteCharEOF:
|
|
|
|
if !t.delChar() && t.cx == 0 {
|
|
|
|
req(reqQuit)
|
2015-06-09 14:44:54 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actEndOfLine:
|
|
|
|
t.cx = len(t.input)
|
|
|
|
case actCancel:
|
|
|
|
if len(t.input) == 0 {
|
|
|
|
req(reqQuit)
|
|
|
|
} else {
|
|
|
|
t.yanked = t.input
|
|
|
|
t.input = []rune{}
|
|
|
|
t.cx = 0
|
2015-06-09 14:44:54 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actForwardChar:
|
|
|
|
if t.cx < len(t.input) {
|
|
|
|
t.cx++
|
|
|
|
}
|
|
|
|
case actBackwardDeleteChar:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.input = append(t.input[:t.cx-1], t.input[t.cx:]...)
|
|
|
|
t.cx--
|
|
|
|
}
|
|
|
|
case actSelectAll:
|
|
|
|
if t.multi {
|
|
|
|
for i := 0; i < t.merger.Length(); i++ {
|
2017-07-27 16:39:25 +00:00
|
|
|
t.selectItem(t.merger.Get(i).item)
|
2015-10-12 17:24:38 +00:00
|
|
|
}
|
|
|
|
req(reqList, reqInfo)
|
|
|
|
}
|
|
|
|
case actDeselectAll:
|
|
|
|
if t.multi {
|
2017-07-28 04:13:03 +00:00
|
|
|
t.selected = make(map[int32]selectedItem)
|
|
|
|
t.version++
|
2015-10-12 17:24:38 +00:00
|
|
|
req(reqList, reqInfo)
|
|
|
|
}
|
|
|
|
case actToggle:
|
|
|
|
if t.multi && t.merger.Length() > 0 {
|
|
|
|
toggle()
|
|
|
|
req(reqList)
|
|
|
|
}
|
|
|
|
case actToggleAll:
|
|
|
|
if t.multi {
|
|
|
|
for i := 0; i < t.merger.Length(); i++ {
|
2017-07-27 16:39:25 +00:00
|
|
|
t.toggleItem(t.merger.Get(i).item)
|
2015-10-12 17:24:38 +00:00
|
|
|
}
|
|
|
|
req(reqList, reqInfo)
|
|
|
|
}
|
2016-01-13 17:35:43 +00:00
|
|
|
case actToggleIn:
|
2018-06-09 16:41:50 +00:00
|
|
|
if t.layout != layoutDefault {
|
2017-01-21 17:32:49 +00:00
|
|
|
return doAction(action{t: actToggleUp}, mapkey)
|
2016-01-13 17:35:43 +00:00
|
|
|
}
|
2017-01-21 17:32:49 +00:00
|
|
|
return doAction(action{t: actToggleDown}, mapkey)
|
2016-01-13 17:35:43 +00:00
|
|
|
case actToggleOut:
|
2018-06-09 16:41:50 +00:00
|
|
|
if t.layout != layoutDefault {
|
2017-01-21 17:32:49 +00:00
|
|
|
return doAction(action{t: actToggleDown}, mapkey)
|
2016-01-13 17:35:43 +00:00
|
|
|
}
|
2017-01-21 17:32:49 +00:00
|
|
|
return doAction(action{t: actToggleUp}, mapkey)
|
2015-10-12 17:24:38 +00:00
|
|
|
case actToggleDown:
|
|
|
|
if t.multi && t.merger.Length() > 0 {
|
|
|
|
toggle()
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(-1, true)
|
2015-10-12 17:24:38 +00:00
|
|
|
req(reqList)
|
|
|
|
}
|
|
|
|
case actToggleUp:
|
|
|
|
if t.multi && t.merger.Length() > 0 {
|
|
|
|
toggle()
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(1, true)
|
2015-10-12 17:24:38 +00:00
|
|
|
req(reqList)
|
|
|
|
}
|
|
|
|
case actDown:
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(-1, true)
|
2015-01-11 18:01:24 +00:00
|
|
|
req(reqList)
|
2015-10-12 17:24:38 +00:00
|
|
|
case actUp:
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(1, true)
|
2015-01-11 18:01:24 +00:00
|
|
|
req(reqList)
|
2015-10-12 17:24:38 +00:00
|
|
|
case actAccept:
|
|
|
|
req(reqClose)
|
2017-12-01 17:27:02 +00:00
|
|
|
case actAcceptNonEmpty:
|
|
|
|
if len(t.selected) > 0 || t.merger.Length() > 0 || !t.reading && t.count == 0 {
|
|
|
|
req(reqClose)
|
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actClearScreen:
|
|
|
|
req(reqRedraw)
|
2017-05-22 08:07:05 +00:00
|
|
|
case actTop:
|
|
|
|
t.vset(0)
|
|
|
|
req(reqList)
|
2015-10-12 17:24:38 +00:00
|
|
|
case actUnixLineDiscard:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.yanked = copySlice(t.input[:t.cx])
|
|
|
|
t.input = t.input[t.cx:]
|
|
|
|
t.cx = 0
|
2015-07-26 15:06:44 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actUnixWordRubout:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.rubout("\\s\\S")
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actBackwardKillWord:
|
|
|
|
if t.cx > 0 {
|
2017-01-15 10:42:28 +00:00
|
|
|
t.rubout(t.wordRubout)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actYank:
|
|
|
|
suffix := copySlice(t.input[t.cx:])
|
|
|
|
t.input = append(append(t.input[:t.cx], t.yanked...), suffix...)
|
|
|
|
t.cx += len(t.yanked)
|
|
|
|
case actPageUp:
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(t.maxItems()-1, false)
|
2015-10-12 17:24:38 +00:00
|
|
|
req(reqList)
|
|
|
|
case actPageDown:
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(-(t.maxItems() - 1), false)
|
2015-10-12 17:24:38 +00:00
|
|
|
req(reqList)
|
2017-01-16 02:58:13 +00:00
|
|
|
case actHalfPageUp:
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(t.maxItems()/2, false)
|
2017-01-16 02:58:13 +00:00
|
|
|
req(reqList)
|
|
|
|
case actHalfPageDown:
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(-(t.maxItems() / 2), false)
|
2017-01-16 02:58:13 +00:00
|
|
|
req(reqList)
|
2016-05-17 17:06:52 +00:00
|
|
|
case actJump:
|
|
|
|
t.jumping = jumpEnabled
|
|
|
|
req(reqJump)
|
|
|
|
case actJumpAccept:
|
|
|
|
t.jumping = jumpAcceptEnabled
|
|
|
|
req(reqJump)
|
2015-10-12 17:24:38 +00:00
|
|
|
case actBackwardWord:
|
2017-01-15 10:42:28 +00:00
|
|
|
t.cx = findLastMatch(t.wordRubout, string(t.input[:t.cx])) + 1
|
2015-10-12 17:24:38 +00:00
|
|
|
case actForwardWord:
|
2017-01-15 10:42:28 +00:00
|
|
|
t.cx += findFirstMatch(t.wordNext, string(t.input[t.cx:])) + 1
|
2015-10-12 17:24:38 +00:00
|
|
|
case actKillWord:
|
|
|
|
ncx := t.cx +
|
2017-01-15 10:42:28 +00:00
|
|
|
findFirstMatch(t.wordNext, string(t.input[t.cx:])) + 1
|
2015-10-12 17:24:38 +00:00
|
|
|
if ncx > t.cx {
|
|
|
|
t.yanked = copySlice(t.input[t.cx:ncx])
|
|
|
|
t.input = append(t.input[:t.cx], t.input[ncx:]...)
|
|
|
|
}
|
|
|
|
case actKillLine:
|
|
|
|
if t.cx < len(t.input) {
|
|
|
|
t.yanked = copySlice(t.input[t.cx:])
|
|
|
|
t.input = t.input[:t.cx]
|
|
|
|
}
|
|
|
|
case actRune:
|
|
|
|
prefix := copySlice(t.input[:t.cx])
|
|
|
|
t.input = append(append(prefix, event.Char), t.input[t.cx:]...)
|
|
|
|
t.cx++
|
|
|
|
case actPreviousHistory:
|
|
|
|
if t.history != nil {
|
|
|
|
t.history.override(string(t.input))
|
2017-08-09 14:25:32 +00:00
|
|
|
t.input = trimQuery(t.history.previous())
|
2015-10-12 17:24:38 +00:00
|
|
|
t.cx = len(t.input)
|
|
|
|
}
|
|
|
|
case actNextHistory:
|
|
|
|
if t.history != nil {
|
|
|
|
t.history.override(string(t.input))
|
2017-08-09 14:25:32 +00:00
|
|
|
t.input = trimQuery(t.history.next())
|
2015-10-12 17:24:38 +00:00
|
|
|
t.cx = len(t.input)
|
|
|
|
}
|
2017-04-28 13:58:08 +00:00
|
|
|
case actSigStop:
|
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
if err == nil {
|
|
|
|
t.tui.Clear()
|
|
|
|
t.tui.Pause(t.fullscreen)
|
|
|
|
notifyStop(p)
|
|
|
|
t.mutex.Unlock()
|
|
|
|
return false
|
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
case actMouse:
|
|
|
|
me := event.MouseEvent
|
|
|
|
mx, my := me.X, me.Y
|
|
|
|
if me.S != 0 {
|
|
|
|
// Scroll
|
2016-06-11 10:59:12 +00:00
|
|
|
if t.window.Enclose(my, mx) && t.merger.Length() > 0 {
|
2015-10-12 17:24:38 +00:00
|
|
|
if t.multi && me.Mod {
|
2015-07-26 14:02:04 +00:00
|
|
|
toggle()
|
|
|
|
}
|
2017-05-23 17:43:39 +00:00
|
|
|
t.vmove(me.S, true)
|
2015-07-26 14:02:04 +00:00
|
|
|
req(reqList)
|
2017-01-23 15:23:16 +00:00
|
|
|
} else if t.hasPreviewWindow() && t.pwindow.Enclose(my, mx) {
|
2016-09-24 19:12:44 +00:00
|
|
|
scrollPreview(-me.S)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2016-06-11 10:59:12 +00:00
|
|
|
} else if t.window.Enclose(my, mx) {
|
2017-01-07 16:30:31 +00:00
|
|
|
mx -= t.window.Left()
|
|
|
|
my -= t.window.Top()
|
2017-05-26 10:02:49 +00:00
|
|
|
mx = util.Constrain(mx-t.promptLen, 0, len(t.input))
|
2015-10-12 17:24:38 +00:00
|
|
|
min := 2 + len(t.header)
|
|
|
|
if t.inlineInfo {
|
|
|
|
min--
|
|
|
|
}
|
2018-06-09 16:41:50 +00:00
|
|
|
h := t.window.Height()
|
|
|
|
switch t.layout {
|
|
|
|
case layoutDefault:
|
|
|
|
my = h - my - 1
|
|
|
|
case layoutReverseList:
|
|
|
|
if my < h-min {
|
|
|
|
my += min
|
|
|
|
} else {
|
|
|
|
my = h - my - 1
|
|
|
|
}
|
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
if me.Double {
|
|
|
|
// Double-click
|
|
|
|
if my >= min {
|
|
|
|
if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {
|
2017-01-21 17:32:49 +00:00
|
|
|
return doActions(t.keymap[tui.DoubleClick], tui.DoubleClick)
|
2015-10-12 17:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if me.Down {
|
|
|
|
if my == 0 && mx >= 0 {
|
|
|
|
// Prompt
|
2018-06-25 10:07:12 +00:00
|
|
|
t.cx = mx + t.xoffset
|
2015-10-12 17:24:38 +00:00
|
|
|
} else if my >= min {
|
|
|
|
// List
|
|
|
|
if t.vset(t.offset+my-min) && t.multi && me.Mod {
|
|
|
|
toggle()
|
|
|
|
}
|
|
|
|
req(reqList)
|
2017-11-30 17:11:20 +00:00
|
|
|
if me.Left {
|
|
|
|
return doActions(t.keymap[tui.LeftClick], tui.LeftClick)
|
|
|
|
}
|
|
|
|
return doActions(t.keymap[tui.RightClick], tui.RightClick)
|
2015-10-12 17:24:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 17:24:38 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-05-17 17:06:52 +00:00
|
|
|
changed := false
|
2015-10-12 17:36:11 +00:00
|
|
|
mapkey := event.Type
|
2016-05-17 17:06:52 +00:00
|
|
|
if t.jumping == jumpDisabled {
|
2017-01-21 17:32:49 +00:00
|
|
|
actions := t.keymap[mapkey]
|
2016-10-24 00:44:56 +00:00
|
|
|
if mapkey == tui.Rune {
|
|
|
|
mapkey = int(event.Char) + int(tui.AltZ)
|
2016-05-17 17:06:52 +00:00
|
|
|
if act, prs := t.keymap[mapkey]; prs {
|
2017-01-21 17:32:49 +00:00
|
|
|
actions = act
|
2016-05-17 17:06:52 +00:00
|
|
|
}
|
2015-10-12 17:36:11 +00:00
|
|
|
}
|
2017-01-21 17:32:49 +00:00
|
|
|
if !doActions(actions, mapkey) {
|
2016-05-17 17:06:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-02-18 14:17:29 +00:00
|
|
|
t.truncateQuery()
|
2016-05-17 17:06:52 +00:00
|
|
|
changed = string(previousInput) != string(t.input)
|
2017-05-22 08:07:05 +00:00
|
|
|
if onChanges, prs := t.keymap[tui.Change]; changed && prs {
|
|
|
|
if !doActions(onChanges, tui.Change) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 17:06:52 +00:00
|
|
|
} else {
|
2016-10-24 00:44:56 +00:00
|
|
|
if mapkey == tui.Rune {
|
2016-05-18 13:45:34 +00:00
|
|
|
if idx := strings.IndexRune(t.jumpLabels, event.Char); idx >= 0 && idx < t.maxItems() && idx < t.merger.Length() {
|
2016-05-17 17:06:52 +00:00
|
|
|
t.cy = idx + t.offset
|
|
|
|
if t.jumping == jumpAcceptEnabled {
|
|
|
|
req(reqClose)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.jumping = jumpDisabled
|
|
|
|
req(reqList)
|
2015-10-12 17:36:11 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
if changed {
|
2018-06-09 16:40:22 +00:00
|
|
|
if t.isPreviewEnabled() {
|
|
|
|
_, q := hasPreviewFlags(t.preview.command)
|
|
|
|
if q {
|
|
|
|
t.version++
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2019-03-29 06:02:31 +00:00
|
|
|
|
|
|
|
if changed || t.cx != previousCx {
|
|
|
|
req(reqPrompt)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.mutex.Unlock() // Must be unlocked before touching reqBox
|
2019-03-29 17:06:54 +00:00
|
|
|
|
|
|
|
if changed {
|
|
|
|
t.eventBox.Set(EvtSearchNew, t.sort)
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, event := range events {
|
|
|
|
t.reqBox.Set(event, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) constrain() {
|
2015-01-09 16:06:08 +00:00
|
|
|
count := t.merger.Length()
|
2015-04-21 14:50:53 +00:00
|
|
|
height := t.maxItems()
|
2015-01-01 19:49:30 +00:00
|
|
|
diffpos := t.cy - t.offset
|
|
|
|
|
2015-01-12 03:56:17 +00:00
|
|
|
t.cy = util.Constrain(t.cy, 0, count-1)
|
2016-02-17 16:46:18 +00:00
|
|
|
t.offset = util.Constrain(t.offset, t.cy-height+1, t.cy)
|
2015-01-01 19:49:30 +00:00
|
|
|
// Adjustment
|
|
|
|
if count-t.offset < height {
|
2015-01-12 03:56:17 +00:00
|
|
|
t.offset = util.Max(0, count-height)
|
|
|
|
t.cy = util.Constrain(t.offset+diffpos, 0, count-1)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-07-22 04:45:38 +00:00
|
|
|
t.offset = util.Max(0, t.offset)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 17:43:39 +00:00
|
|
|
func (t *Terminal) vmove(o int, allowCycle bool) {
|
2018-06-09 16:41:50 +00:00
|
|
|
if t.layout != layoutDefault {
|
2015-06-16 14:14:57 +00:00
|
|
|
o *= -1
|
|
|
|
}
|
|
|
|
dest := t.cy + o
|
2017-05-23 17:43:39 +00:00
|
|
|
if t.cycle && allowCycle {
|
2015-06-16 14:14:57 +00:00
|
|
|
max := t.merger.Length() - 1
|
|
|
|
if dest > max {
|
|
|
|
if t.cy == max {
|
|
|
|
dest = 0
|
|
|
|
}
|
|
|
|
} else if dest < 0 {
|
|
|
|
if t.cy == 0 {
|
|
|
|
dest = max
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-06-16 14:14:57 +00:00
|
|
|
t.vset(dest)
|
2015-01-10 05:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) vset(o int) bool {
|
2015-01-12 03:56:17 +00:00
|
|
|
t.cy = util.Constrain(o, 0, t.merger.Length()-1)
|
2015-01-10 05:50:24 +00:00
|
|
|
return t.cy == o
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 14:50:53 +00:00
|
|
|
func (t *Terminal) maxItems() int {
|
2017-01-07 16:30:31 +00:00
|
|
|
max := t.window.Height() - 2 - len(t.header)
|
2015-04-21 14:50:53 +00:00
|
|
|
if t.inlineInfo {
|
2015-08-02 04:06:15 +00:00
|
|
|
max++
|
2015-04-21 14:50:53 +00:00
|
|
|
}
|
2015-07-22 04:45:38 +00:00
|
|
|
return util.Max(max, 0)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|