2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
C "github.com/junegunn/fzf/src/curses"
|
|
|
|
"github.com/junegunn/go-runewidth"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Terminal struct {
|
|
|
|
prompt string
|
|
|
|
reverse bool
|
|
|
|
tac bool
|
|
|
|
cx int
|
|
|
|
cy int
|
|
|
|
offset int
|
|
|
|
yanked []rune
|
|
|
|
input []rune
|
|
|
|
multi bool
|
|
|
|
printQuery bool
|
|
|
|
count int
|
|
|
|
progress int
|
|
|
|
reading bool
|
2015-01-09 16:06:08 +00:00
|
|
|
merger *Merger
|
2015-01-01 19:49:30 +00:00
|
|
|
selected map[*string]*string
|
|
|
|
reqBox *EventBox
|
|
|
|
eventBox *EventBox
|
|
|
|
mutex sync.Mutex
|
|
|
|
initFunc func()
|
2015-01-07 03:46:45 +00:00
|
|
|
suppress bool
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _spinner []string = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}
|
|
|
|
|
|
|
|
const (
|
|
|
|
REQ_PROMPT EventType = iota
|
|
|
|
REQ_INFO
|
|
|
|
REQ_LIST
|
2015-01-07 03:46:45 +00:00
|
|
|
REQ_REFRESH
|
2015-01-01 19:49:30 +00:00
|
|
|
REQ_REDRAW
|
|
|
|
REQ_CLOSE
|
|
|
|
REQ_QUIT
|
|
|
|
)
|
|
|
|
|
2015-01-07 03:46:45 +00:00
|
|
|
const (
|
|
|
|
INITIAL_DELAY = 100 * time.Millisecond
|
|
|
|
SPINNER_DURATION = 200 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2015-01-01 19:49:30 +00:00
|
|
|
func NewTerminal(opts *Options, eventBox *EventBox) *Terminal {
|
|
|
|
input := []rune(opts.Query)
|
|
|
|
return &Terminal{
|
|
|
|
prompt: opts.Prompt,
|
|
|
|
tac: opts.Sort == 0,
|
|
|
|
reverse: opts.Reverse,
|
|
|
|
cx: displayWidth(input),
|
|
|
|
cy: 0,
|
|
|
|
offset: 0,
|
|
|
|
yanked: []rune{},
|
|
|
|
input: input,
|
|
|
|
multi: opts.Multi,
|
|
|
|
printQuery: opts.PrintQuery,
|
2015-01-09 16:06:08 +00:00
|
|
|
merger: EmptyMerger,
|
2015-01-01 19:49:30 +00:00
|
|
|
selected: make(map[*string]*string),
|
|
|
|
reqBox: NewEventBox(),
|
|
|
|
eventBox: eventBox,
|
|
|
|
mutex: sync.Mutex{},
|
2015-01-07 03:46:45 +00:00
|
|
|
suppress: true,
|
2015-01-01 19:49:30 +00:00
|
|
|
initFunc: func() {
|
|
|
|
C.Init(opts.Color, opts.Color256, opts.Black, opts.Mouse)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) Input() []rune {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
return copySlice(t.input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) UpdateCount(cnt int, final bool) {
|
|
|
|
t.mutex.Lock()
|
|
|
|
t.count = cnt
|
|
|
|
t.reading = !final
|
|
|
|
t.mutex.Unlock()
|
|
|
|
t.reqBox.Set(REQ_INFO, nil)
|
2015-01-07 03:46:45 +00:00
|
|
|
if final {
|
|
|
|
t.reqBox.Set(REQ_REFRESH, nil)
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) UpdateProgress(progress float32) {
|
|
|
|
t.mutex.Lock()
|
|
|
|
t.progress = int(progress * 100)
|
|
|
|
t.mutex.Unlock()
|
|
|
|
t.reqBox.Set(REQ_INFO, nil)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
t.reqBox.Set(REQ_INFO, nil)
|
|
|
|
t.reqBox.Set(REQ_LIST, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) listIndex(y int) int {
|
|
|
|
if t.tac {
|
2015-01-09 16:06:08 +00:00
|
|
|
return t.merger.Length() - y - 1
|
2015-01-01 19:49:30 +00:00
|
|
|
} else {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) output() {
|
|
|
|
if t.printQuery {
|
|
|
|
fmt.Println(string(t.input))
|
|
|
|
}
|
|
|
|
if len(t.selected) == 0 {
|
2015-01-09 16:06:08 +00:00
|
|
|
if t.merger.Length() > t.cy {
|
|
|
|
t.merger.Get(t.listIndex(t.cy)).Print()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ptr, orig := range t.selected {
|
|
|
|
if orig != nil {
|
|
|
|
fmt.Println(*orig)
|
|
|
|
} else {
|
|
|
|
fmt.Println(*ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func displayWidth(runes []rune) int {
|
|
|
|
l := 0
|
|
|
|
for _, r := range runes {
|
|
|
|
l += runewidth.RuneWidth(r)
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) move(y int, x int, clear bool) {
|
|
|
|
maxy := C.MaxY()
|
|
|
|
if !t.reverse {
|
|
|
|
y = maxy - y - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if clear {
|
|
|
|
C.MoveAndClear(y, x)
|
|
|
|
} else {
|
|
|
|
C.Move(y, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) placeCursor() {
|
|
|
|
t.move(0, len(t.prompt)+displayWidth(t.input[:t.cx]), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printPrompt() {
|
|
|
|
t.move(0, 0, true)
|
|
|
|
C.CPrint(C.COL_PROMPT, true, t.prompt)
|
|
|
|
C.CPrint(C.COL_NORMAL, true, string(t.input))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printInfo() {
|
|
|
|
t.move(1, 0, true)
|
|
|
|
if t.reading {
|
2015-01-07 03:46:45 +00:00
|
|
|
duration := int64(SPINNER_DURATION)
|
2015-01-01 19:49:30 +00:00
|
|
|
idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration
|
|
|
|
C.CPrint(C.COL_SPINNER, true, _spinner[idx])
|
|
|
|
}
|
|
|
|
|
|
|
|
t.move(1, 2, false)
|
2015-01-09 16:06:08 +00:00
|
|
|
output := fmt.Sprintf("%d/%d", t.merger.Length(), t.count)
|
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)
|
|
|
|
}
|
|
|
|
C.CPrint(C.COL_INFO, false, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printList() {
|
|
|
|
t.constrain()
|
|
|
|
|
|
|
|
maxy := maxItems()
|
2015-01-09 16:06:08 +00:00
|
|
|
count := t.merger.Length() - t.offset
|
2015-01-01 19:49:30 +00:00
|
|
|
for i := 0; i < maxy; i++ {
|
|
|
|
t.move(i+2, 0, true)
|
|
|
|
if i < count {
|
2015-01-09 16:06:08 +00:00
|
|
|
t.printItem(t.merger.Get(t.listIndex(i+t.offset)), i == t.cy-t.offset)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printItem(item *Item, current bool) {
|
|
|
|
_, selected := t.selected[item.text]
|
|
|
|
if current {
|
|
|
|
C.CPrint(C.COL_CURSOR, true, ">")
|
|
|
|
if selected {
|
|
|
|
C.CPrint(C.COL_CURRENT, true, ">")
|
|
|
|
} else {
|
|
|
|
C.CPrint(C.COL_CURRENT, true, " ")
|
|
|
|
}
|
|
|
|
t.printHighlighted(item, true, C.COL_CURRENT, C.COL_CURRENT_MATCH)
|
|
|
|
} else {
|
|
|
|
C.CPrint(C.COL_CURSOR, true, " ")
|
|
|
|
if selected {
|
|
|
|
C.CPrint(C.COL_SELECTED, true, ">")
|
|
|
|
} else {
|
|
|
|
C.Print(" ")
|
|
|
|
}
|
|
|
|
t.printHighlighted(item, false, 0, C.COL_MATCH)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimRight(runes []rune, width int) ([]rune, int) {
|
|
|
|
currentWidth := displayWidth(runes)
|
|
|
|
trimmed := 0
|
|
|
|
|
|
|
|
for currentWidth > width && len(runes) > 0 {
|
|
|
|
sz := len(runes)
|
|
|
|
currentWidth -= runewidth.RuneWidth(runes[sz-1])
|
|
|
|
runes = runes[:sz-1]
|
|
|
|
trimmed += 1
|
|
|
|
}
|
|
|
|
return runes, trimmed
|
|
|
|
}
|
|
|
|
|
2015-01-08 17:37:08 +00:00
|
|
|
func trimLeft(runes []rune, width int) ([]rune, int32) {
|
2015-01-01 19:49:30 +00:00
|
|
|
currentWidth := displayWidth(runes)
|
2015-01-08 17:37:08 +00:00
|
|
|
var trimmed int32 = 0
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
for currentWidth > width && len(runes) > 0 {
|
|
|
|
currentWidth -= runewidth.RuneWidth(runes[0])
|
|
|
|
runes = runes[1:]
|
|
|
|
trimmed += 1
|
|
|
|
}
|
|
|
|
return runes, trimmed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int) {
|
2015-01-08 17:37:08 +00:00
|
|
|
var maxe int32 = 0
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, offset := range item.offsets {
|
|
|
|
if offset[1] > maxe {
|
|
|
|
maxe = offset[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overflow
|
|
|
|
text := []rune(*item.text)
|
|
|
|
offsets := item.offsets
|
|
|
|
maxWidth := C.MaxX() - 3
|
|
|
|
fullWidth := displayWidth(text)
|
|
|
|
if fullWidth > maxWidth {
|
|
|
|
// Stri..
|
|
|
|
matchEndWidth := displayWidth(text[:maxe])
|
|
|
|
if matchEndWidth <= maxWidth-2 {
|
|
|
|
text, _ = trimRight(text, maxWidth-2)
|
|
|
|
text = append(text, []rune("..")...)
|
|
|
|
} else {
|
|
|
|
// Stri..
|
|
|
|
if matchEndWidth < fullWidth-2 {
|
|
|
|
text = append(text[:maxe], []rune("..")...)
|
|
|
|
}
|
|
|
|
// ..ri..
|
2015-01-08 17:37:08 +00:00
|
|
|
var diff int32
|
2015-01-01 19:49:30 +00:00
|
|
|
text, diff = trimLeft(text, maxWidth-2)
|
|
|
|
|
|
|
|
// Transform offsets
|
|
|
|
offsets = make([]Offset, len(item.offsets))
|
|
|
|
for idx, offset := range item.offsets {
|
|
|
|
b, e := offset[0], offset[1]
|
|
|
|
b += 2 - diff
|
|
|
|
e += 2 - diff
|
2015-01-08 17:37:08 +00:00
|
|
|
b = Max32(b, 2)
|
2015-01-01 19:49:30 +00:00
|
|
|
if b < e {
|
|
|
|
offsets[idx] = Offset{b, e}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text = append([]rune(".."), text...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(ByOrder(offsets))
|
2015-01-08 17:37:08 +00:00
|
|
|
var index int32 = 0
|
2015-01-01 19:49:30 +00:00
|
|
|
for _, offset := range offsets {
|
2015-01-08 17:37:08 +00:00
|
|
|
b := Max32(index, offset[0])
|
|
|
|
e := Max32(index, offset[1])
|
2015-01-01 19:49:30 +00:00
|
|
|
C.CPrint(col1, bold, string(text[index:b]))
|
|
|
|
C.CPrint(col2, bold, string(text[b:e]))
|
|
|
|
index = e
|
|
|
|
}
|
2015-01-08 17:37:08 +00:00
|
|
|
if index < int32(len(text)) {
|
2015-01-01 19:49:30 +00:00
|
|
|
C.CPrint(col1, bold, string(text[index:]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printAll() {
|
|
|
|
t.printList()
|
|
|
|
t.printInfo()
|
|
|
|
t.printPrompt()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) refresh() {
|
2015-01-07 03:46:45 +00:00
|
|
|
if !t.suppress {
|
|
|
|
C.Refresh()
|
|
|
|
}
|
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...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) Loop() {
|
|
|
|
{ // Late initialization
|
|
|
|
t.mutex.Lock()
|
|
|
|
t.initFunc()
|
|
|
|
t.printPrompt()
|
2015-01-03 20:09:40 +00:00
|
|
|
t.placeCursor()
|
2015-01-07 03:46:45 +00:00
|
|
|
C.Refresh()
|
|
|
|
t.printInfo()
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Unlock()
|
2015-01-07 03:46:45 +00:00
|
|
|
go func() {
|
|
|
|
timer := time.NewTimer(INITIAL_DELAY)
|
|
|
|
<-timer.C
|
|
|
|
t.reqBox.Set(REQ_REFRESH, nil)
|
|
|
|
}()
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
t.reqBox.Wait(func(events *Events) {
|
|
|
|
defer events.Clear()
|
|
|
|
t.mutex.Lock()
|
|
|
|
for req := range *events {
|
|
|
|
switch req {
|
|
|
|
case REQ_PROMPT:
|
|
|
|
t.printPrompt()
|
|
|
|
case REQ_INFO:
|
|
|
|
t.printInfo()
|
|
|
|
case REQ_LIST:
|
|
|
|
t.printList()
|
2015-01-07 03:46:45 +00:00
|
|
|
case REQ_REFRESH:
|
|
|
|
t.suppress = false
|
2015-01-01 19:49:30 +00:00
|
|
|
case REQ_REDRAW:
|
|
|
|
C.Clear()
|
|
|
|
t.printAll()
|
|
|
|
case REQ_CLOSE:
|
|
|
|
C.Close()
|
|
|
|
t.output()
|
|
|
|
os.Exit(0)
|
|
|
|
case REQ_QUIT:
|
|
|
|
C.Close()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 20:09:40 +00:00
|
|
|
t.placeCursor()
|
2015-01-01 19:49:30 +00:00
|
|
|
t.mutex.Unlock()
|
|
|
|
})
|
|
|
|
t.refresh()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
looping := true
|
|
|
|
for looping {
|
|
|
|
event := C.GetChar()
|
|
|
|
|
|
|
|
t.mutex.Lock()
|
|
|
|
previousInput := t.input
|
|
|
|
events := []EventType{REQ_PROMPT}
|
|
|
|
toggle := func() {
|
2015-01-09 16:06:08 +00:00
|
|
|
item := t.merger.Get(t.listIndex(t.cy))
|
2015-01-01 19:49:30 +00:00
|
|
|
if _, found := t.selected[item.text]; !found {
|
|
|
|
t.selected[item.text] = item.origText
|
|
|
|
} else {
|
|
|
|
delete(t.selected, item.text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req := func(evts ...EventType) {
|
|
|
|
for _, event := range evts {
|
|
|
|
events = append(events, event)
|
|
|
|
if event == REQ_CLOSE || event == REQ_QUIT {
|
|
|
|
looping = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch event.Type {
|
|
|
|
case C.INVALID:
|
2015-01-08 13:04:12 +00:00
|
|
|
t.mutex.Unlock()
|
2015-01-01 19:49:30 +00:00
|
|
|
continue
|
|
|
|
case C.CTRL_A:
|
|
|
|
t.cx = 0
|
|
|
|
case C.CTRL_B:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.cx -= 1
|
|
|
|
}
|
|
|
|
case C.CTRL_C, C.CTRL_G, C.CTRL_Q, C.ESC:
|
|
|
|
req(REQ_QUIT)
|
|
|
|
case C.CTRL_D:
|
|
|
|
if !t.delChar() && t.cx == 0 {
|
|
|
|
req(REQ_QUIT)
|
|
|
|
}
|
|
|
|
case C.CTRL_E:
|
|
|
|
t.cx = len(t.input)
|
|
|
|
case C.CTRL_F:
|
|
|
|
if t.cx < len(t.input) {
|
|
|
|
t.cx += 1
|
|
|
|
}
|
|
|
|
case C.CTRL_H:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.input = append(t.input[:t.cx-1], t.input[t.cx:]...)
|
|
|
|
t.cx -= 1
|
|
|
|
}
|
|
|
|
case C.TAB:
|
2015-01-09 16:06:08 +00:00
|
|
|
if t.multi && t.merger.Length() > 0 {
|
2015-01-01 19:49:30 +00:00
|
|
|
toggle()
|
|
|
|
t.vmove(-1)
|
|
|
|
req(REQ_LIST, REQ_INFO)
|
|
|
|
}
|
|
|
|
case C.BTAB:
|
2015-01-09 16:06:08 +00:00
|
|
|
if t.multi && t.merger.Length() > 0 {
|
2015-01-01 19:49:30 +00:00
|
|
|
toggle()
|
|
|
|
t.vmove(1)
|
|
|
|
req(REQ_LIST, REQ_INFO)
|
|
|
|
}
|
|
|
|
case C.CTRL_J, C.CTRL_N:
|
|
|
|
t.vmove(-1)
|
|
|
|
req(REQ_LIST)
|
|
|
|
case C.CTRL_K, C.CTRL_P:
|
|
|
|
t.vmove(1)
|
|
|
|
req(REQ_LIST)
|
|
|
|
case C.CTRL_M:
|
|
|
|
req(REQ_CLOSE)
|
|
|
|
case C.CTRL_L:
|
|
|
|
req(REQ_REDRAW)
|
|
|
|
case C.CTRL_U:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.yanked = copySlice(t.input[:t.cx])
|
|
|
|
t.input = t.input[t.cx:]
|
|
|
|
t.cx = 0
|
|
|
|
}
|
|
|
|
case C.CTRL_W:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.rubout("\\s\\S")
|
|
|
|
}
|
|
|
|
case C.ALT_BS:
|
|
|
|
if t.cx > 0 {
|
|
|
|
t.rubout("[^[:alnum:]][[:alnum:]]")
|
|
|
|
}
|
|
|
|
case C.CTRL_Y:
|
|
|
|
t.input = append(append(t.input[:t.cx], t.yanked...), t.input[t.cx:]...)
|
|
|
|
t.cx += len(t.yanked)
|
|
|
|
case C.DEL:
|
|
|
|
t.delChar()
|
|
|
|
case C.PGUP:
|
|
|
|
t.vmove(maxItems() - 1)
|
|
|
|
req(REQ_LIST)
|
|
|
|
case C.PGDN:
|
|
|
|
t.vmove(-(maxItems() - 1))
|
|
|
|
req(REQ_LIST)
|
|
|
|
case C.ALT_B:
|
|
|
|
t.cx = findLastMatch("[^[:alnum:]][[:alnum:]]", string(t.input[:t.cx])) + 1
|
|
|
|
case C.ALT_F:
|
|
|
|
t.cx += findFirstMatch("[[:alnum:]][^[:alnum:]]|(.$)", string(t.input[t.cx:])) + 1
|
|
|
|
case C.ALT_D:
|
|
|
|
ncx := t.cx +
|
|
|
|
findFirstMatch("[[:alnum:]][^[:alnum:]]|(.$)", string(t.input[t.cx:])) + 1
|
|
|
|
if ncx > t.cx {
|
|
|
|
t.yanked = copySlice(t.input[t.cx:ncx])
|
|
|
|
t.input = append(t.input[:t.cx], t.input[ncx:]...)
|
|
|
|
}
|
|
|
|
case C.RUNE:
|
|
|
|
prefix := copySlice(t.input[:t.cx])
|
|
|
|
t.input = append(append(prefix, event.Char), t.input[t.cx:]...)
|
|
|
|
t.cx += 1
|
|
|
|
case C.MOUSE:
|
|
|
|
me := event.MouseEvent
|
|
|
|
mx, my := Min(len(t.input), Max(0, me.X-len(t.prompt))), me.Y
|
|
|
|
if !t.reverse {
|
|
|
|
my = C.MaxY() - my - 1
|
|
|
|
}
|
|
|
|
if me.S != 0 {
|
|
|
|
// Scroll
|
|
|
|
if me.Mod {
|
|
|
|
toggle()
|
|
|
|
}
|
|
|
|
t.vmove(me.S)
|
|
|
|
req(REQ_LIST)
|
|
|
|
} else if me.Double {
|
|
|
|
// Double-click
|
|
|
|
if my >= 2 {
|
|
|
|
t.cy = my - 2
|
|
|
|
req(REQ_CLOSE)
|
|
|
|
}
|
|
|
|
} else if me.Down {
|
|
|
|
if my == 0 && mx >= 0 {
|
|
|
|
// Prompt
|
|
|
|
t.cx = mx
|
|
|
|
} else if my >= 2 {
|
|
|
|
// List
|
2015-01-04 16:40:19 +00:00
|
|
|
t.cy = t.offset + my - 2
|
2015-01-01 19:49:30 +00:00
|
|
|
if me.Mod {
|
|
|
|
toggle()
|
|
|
|
}
|
|
|
|
req(REQ_LIST)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changed := string(previousInput) != string(t.input)
|
|
|
|
t.mutex.Unlock() // Must be unlocked before touching reqBox
|
|
|
|
|
|
|
|
if changed {
|
|
|
|
t.eventBox.Set(EVT_SEARCH_NEW, nil)
|
|
|
|
}
|
|
|
|
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-01-01 19:49:30 +00:00
|
|
|
height := C.MaxY() - 2
|
|
|
|
diffpos := t.cy - t.offset
|
|
|
|
|
|
|
|
t.cy = Max(0, Min(t.cy, count-1))
|
|
|
|
|
|
|
|
if t.cy > t.offset+(height-1) {
|
|
|
|
// Ceil
|
|
|
|
t.offset = t.cy - (height - 1)
|
|
|
|
} else if t.offset > t.cy {
|
|
|
|
// Floor
|
|
|
|
t.offset = t.cy
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjustment
|
|
|
|
if count-t.offset < height {
|
|
|
|
t.offset = Max(0, count-height)
|
|
|
|
t.cy = Max(0, Min(t.offset+diffpos, count-1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) vmove(o int) {
|
|
|
|
if t.reverse {
|
|
|
|
t.cy -= o
|
|
|
|
} else {
|
|
|
|
t.cy += o
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func maxItems() int {
|
|
|
|
return C.MaxY() - 2
|
|
|
|
}
|