2015-01-01 14:49:30 -05:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
2015-01-18 02:59:04 -05:00
|
|
|
"bytes"
|
2015-01-01 14:49:30 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-01-23 06:30:50 -05:00
|
|
|
"os/signal"
|
2015-01-01 14:49:30 -05:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2015-01-18 02:59:04 -05:00
|
|
|
"strings"
|
2015-01-01 14:49:30 -05:00
|
|
|
"sync"
|
2015-01-23 06:30:50 -05:00
|
|
|
"syscall"
|
2015-01-01 14:49:30 -05:00
|
|
|
"time"
|
2015-01-11 22:56:17 -05:00
|
|
|
|
|
|
|
C "github.com/junegunn/fzf/src/curses"
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
|
|
|
|
|
|
|
"github.com/junegunn/go-runewidth"
|
2015-01-01 14:49:30 -05:00
|
|
|
)
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Terminal represents terminal input/output
|
2015-01-01 14:49:30 -05:00
|
|
|
type Terminal struct {
|
|
|
|
prompt string
|
|
|
|
reverse bool
|
2015-04-15 23:56:01 -04:00
|
|
|
hscroll bool
|
2015-01-01 14:49:30 -05:00
|
|
|
cx int
|
|
|
|
cy int
|
|
|
|
offset int
|
|
|
|
yanked []rune
|
|
|
|
input []rune
|
|
|
|
multi bool
|
2015-04-16 01:44:41 -04:00
|
|
|
sort bool
|
2015-03-31 09:05:02 -04:00
|
|
|
toggleSort int
|
2015-03-28 13:59:32 -04:00
|
|
|
expect []int
|
|
|
|
pressed int
|
2015-01-01 14:49:30 -05:00
|
|
|
printQuery bool
|
|
|
|
count int
|
|
|
|
progress int
|
|
|
|
reading bool
|
2015-01-09 11:06:08 -05:00
|
|
|
merger *Merger
|
2015-04-17 09:23:52 -04:00
|
|
|
selected map[uint32]selectedItem
|
2015-01-11 22:56:17 -05:00
|
|
|
reqBox *util.EventBox
|
|
|
|
eventBox *util.EventBox
|
2015-01-01 14:49:30 -05:00
|
|
|
mutex sync.Mutex
|
|
|
|
initFunc func()
|
2015-01-06 22:46:45 -05:00
|
|
|
suppress bool
|
2015-02-17 10:08:17 -05:00
|
|
|
startChan chan bool
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-16 21:07:04 -05:00
|
|
|
type selectedItem struct {
|
|
|
|
at time.Time
|
|
|
|
text *string
|
|
|
|
}
|
|
|
|
|
2015-03-22 03:05:54 -04:00
|
|
|
type byTimeOrder []selectedItem
|
2015-01-16 21:07:04 -05:00
|
|
|
|
2015-03-22 03:05:54 -04:00
|
|
|
func (a byTimeOrder) Len() int {
|
2015-01-16 21:07:04 -05:00
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
|
2015-03-22 03:05:54 -04:00
|
|
|
func (a byTimeOrder) Swap(i, j int) {
|
2015-01-16 21:07:04 -05:00
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
2015-03-22 03:05:54 -04:00
|
|
|
func (a byTimeOrder) Less(i, j int) bool {
|
2015-01-16 21:07:04 -05:00
|
|
|
return a[i].at.Before(a[j].at)
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
var _spinner = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}
|
2015-01-23 22:28:00 -05:00
|
|
|
var _runeWidths = make(map[rune]int)
|
2015-01-01 14:49:30 -05:00
|
|
|
|
|
|
|
const (
|
2015-01-11 22:56:17 -05:00
|
|
|
reqPrompt util.EventType = iota
|
2015-01-11 13:01:24 -05:00
|
|
|
reqInfo
|
|
|
|
reqList
|
|
|
|
reqRefresh
|
|
|
|
reqRedraw
|
|
|
|
reqClose
|
|
|
|
reqQuit
|
2015-01-01 14:49:30 -05:00
|
|
|
)
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// NewTerminal returns new Terminal object
|
2015-01-11 22:56:17 -05:00
|
|
|
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
|
2015-01-01 14:49:30 -05:00
|
|
|
input := []rune(opts.Query)
|
|
|
|
return &Terminal{
|
|
|
|
prompt: opts.Prompt,
|
|
|
|
reverse: opts.Reverse,
|
2015-04-15 23:56:01 -04:00
|
|
|
hscroll: opts.Hscroll,
|
2015-01-23 23:26:33 -05:00
|
|
|
cx: len(input),
|
2015-01-01 14:49:30 -05:00
|
|
|
cy: 0,
|
|
|
|
offset: 0,
|
|
|
|
yanked: []rune{},
|
|
|
|
input: input,
|
|
|
|
multi: opts.Multi,
|
2015-04-16 01:44:41 -04:00
|
|
|
sort: opts.Sort > 0,
|
2015-03-31 09:05:02 -04:00
|
|
|
toggleSort: opts.ToggleSort,
|
2015-03-28 13:59:32 -04:00
|
|
|
expect: opts.Expect,
|
|
|
|
pressed: 0,
|
2015-01-01 14:49:30 -05:00
|
|
|
printQuery: opts.PrintQuery,
|
2015-01-09 11:06:08 -05:00
|
|
|
merger: EmptyMerger,
|
2015-04-17 09:23:52 -04:00
|
|
|
selected: make(map[uint32]selectedItem),
|
2015-01-11 22:56:17 -05:00
|
|
|
reqBox: util.NewEventBox(),
|
2015-01-01 14:49:30 -05:00
|
|
|
eventBox: eventBox,
|
|
|
|
mutex: sync.Mutex{},
|
2015-01-06 22:46:45 -05:00
|
|
|
suppress: true,
|
2015-02-17 10:08:17 -05:00
|
|
|
startChan: make(chan bool, 1),
|
2015-01-01 14:49:30 -05:00
|
|
|
initFunc: func() {
|
2015-04-17 13:52:30 -04:00
|
|
|
C.Init(opts.Theme, opts.Black, opts.Mouse)
|
2015-01-01 14:49:30 -05:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Input returns current query string
|
2015-01-01 14:49:30 -05:00
|
|
|
func (t *Terminal) Input() []rune {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
return copySlice(t.input)
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// UpdateCount updates the count information
|
2015-01-01 14:49:30 -05:00
|
|
|
func (t *Terminal) UpdateCount(cnt int, final bool) {
|
|
|
|
t.mutex.Lock()
|
|
|
|
t.count = cnt
|
|
|
|
t.reading = !final
|
|
|
|
t.mutex.Unlock()
|
2015-01-11 13:01:24 -05:00
|
|
|
t.reqBox.Set(reqInfo, nil)
|
2015-01-06 22:46:45 -05:00
|
|
|
if final {
|
2015-01-11 13:01:24 -05:00
|
|
|
t.reqBox.Set(reqRefresh, nil)
|
2015-01-06 22:46:45 -05:00
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// UpdateProgress updates the search progress
|
2015-01-01 14:49:30 -05:00
|
|
|
func (t *Terminal) UpdateProgress(progress float32) {
|
|
|
|
t.mutex.Lock()
|
2015-01-11 07:56:55 -05:00
|
|
|
newProgress := int(progress * 100)
|
|
|
|
changed := t.progress != newProgress
|
|
|
|
t.progress = newProgress
|
2015-01-01 14:49:30 -05:00
|
|
|
t.mutex.Unlock()
|
2015-01-11 07:56:55 -05:00
|
|
|
|
|
|
|
if changed {
|
2015-01-11 13:01:24 -05:00
|
|
|
t.reqBox.Set(reqInfo, nil)
|
2015-01-11 07:56:55 -05:00
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// UpdateList updates Merger to display the list
|
2015-01-09 11:06:08 -05:00
|
|
|
func (t *Terminal) UpdateList(merger *Merger) {
|
2015-01-01 14:49:30 -05:00
|
|
|
t.mutex.Lock()
|
|
|
|
t.progress = 100
|
2015-01-09 11:06:08 -05:00
|
|
|
t.merger = merger
|
2015-01-01 14:49:30 -05:00
|
|
|
t.mutex.Unlock()
|
2015-01-11 13:01:24 -05:00
|
|
|
t.reqBox.Set(reqInfo, nil)
|
|
|
|
t.reqBox.Set(reqList, nil)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) output() {
|
|
|
|
if t.printQuery {
|
|
|
|
fmt.Println(string(t.input))
|
|
|
|
}
|
2015-03-28 13:59:32 -04:00
|
|
|
if len(t.expect) > 0 {
|
|
|
|
if t.pressed == 0 {
|
|
|
|
fmt.Println()
|
|
|
|
} else if util.Between(t.pressed, C.AltA, C.AltZ) {
|
|
|
|
fmt.Printf("alt-%c\n", t.pressed+'a'-C.AltA)
|
|
|
|
} else if util.Between(t.pressed, C.F1, C.F4) {
|
|
|
|
fmt.Printf("f%c\n", t.pressed+'1'-C.F1)
|
|
|
|
} else if util.Between(t.pressed, C.CtrlA, C.CtrlZ) {
|
|
|
|
fmt.Printf("ctrl-%c\n", t.pressed+'a'-C.CtrlA)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%c\n", t.pressed-C.AltZ)
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
if len(t.selected) == 0 {
|
2015-01-14 16:06:22 -05:00
|
|
|
cnt := t.merger.Length()
|
|
|
|
if cnt > 0 && cnt > t.cy {
|
2015-02-25 11:42:15 -05:00
|
|
|
fmt.Println(t.merger.Get(t.cy).AsString())
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
} else {
|
2015-01-16 21:07:04 -05:00
|
|
|
sels := make([]selectedItem, 0, len(t.selected))
|
|
|
|
for _, sel := range t.selected {
|
|
|
|
sels = append(sels, sel)
|
|
|
|
}
|
2015-03-22 03:05:54 -04:00
|
|
|
sort.Sort(byTimeOrder(sels))
|
2015-01-16 21:07:04 -05:00
|
|
|
for _, sel := range sels {
|
|
|
|
fmt.Println(*sel.text)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 02:59:04 -05:00
|
|
|
func runeWidth(r rune, prefixWidth int) int {
|
|
|
|
if r == '\t' {
|
|
|
|
return 8 - prefixWidth%8
|
2015-01-23 22:28:00 -05:00
|
|
|
} else if w, found := _runeWidths[r]; found {
|
|
|
|
return w
|
2015-01-18 02:59:04 -05:00
|
|
|
} else {
|
2015-01-23 22:28:00 -05:00
|
|
|
w := runewidth.RuneWidth(r)
|
|
|
|
_runeWidths[r] = w
|
|
|
|
return w
|
2015-01-18 02:59:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 14:49:30 -05:00
|
|
|
func displayWidth(runes []rune) int {
|
|
|
|
l := 0
|
|
|
|
for _, r := range runes {
|
2015-01-18 02:59:04 -05:00
|
|
|
l += runeWidth(r, l)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
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)
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColPrompt, true, t.prompt)
|
|
|
|
C.CPrint(C.ColNormal, true, string(t.input))
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printInfo() {
|
|
|
|
t.move(1, 0, true)
|
|
|
|
if t.reading {
|
2015-01-11 13:01:24 -05:00
|
|
|
duration := int64(spinnerDuration)
|
2015-01-01 14:49:30 -05:00
|
|
|
idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColSpinner, true, _spinner[idx])
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
t.move(1, 2, false)
|
2015-01-09 11:06:08 -05:00
|
|
|
output := fmt.Sprintf("%d/%d", t.merger.Length(), t.count)
|
2015-04-16 01:44:41 -04:00
|
|
|
if t.toggleSort > 0 {
|
|
|
|
if t.sort {
|
|
|
|
output += "/S"
|
|
|
|
} else {
|
|
|
|
output += " "
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 14:49:30 -05: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)
|
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColInfo, false, output)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printList() {
|
|
|
|
t.constrain()
|
|
|
|
|
|
|
|
maxy := maxItems()
|
2015-01-09 11:06:08 -05:00
|
|
|
count := t.merger.Length() - t.offset
|
2015-01-01 14:49:30 -05:00
|
|
|
for i := 0; i < maxy; i++ {
|
|
|
|
t.move(i+2, 0, true)
|
|
|
|
if i < count {
|
2015-02-25 11:42:15 -05:00
|
|
|
t.printItem(t.merger.Get(i+t.offset), i == t.cy-t.offset)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printItem(item *Item, current bool) {
|
2015-04-17 09:23:52 -04:00
|
|
|
_, selected := t.selected[item.index]
|
2015-01-01 14:49:30 -05:00
|
|
|
if current {
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColCursor, true, ">")
|
2015-01-01 14:49:30 -05:00
|
|
|
if selected {
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColCurrent, true, ">")
|
2015-01-01 14:49:30 -05:00
|
|
|
} else {
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColCurrent, true, " ")
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-03-18 12:59:14 -04:00
|
|
|
t.printHighlighted(item, true, C.ColCurrent, C.ColCurrentMatch, true)
|
2015-01-01 14:49:30 -05:00
|
|
|
} else {
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColCursor, true, " ")
|
2015-01-01 14:49:30 -05:00
|
|
|
if selected {
|
2015-01-11 13:01:24 -05:00
|
|
|
C.CPrint(C.ColSelected, true, ">")
|
2015-01-01 14:49:30 -05:00
|
|
|
} else {
|
|
|
|
C.Print(" ")
|
|
|
|
}
|
2015-03-18 12:59:14 -04:00
|
|
|
t.printHighlighted(item, false, 0, C.ColMatch, false)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimRight(runes []rune, width int) ([]rune, int) {
|
2015-01-18 02:59:04 -05:00
|
|
|
// We start from the beginning to handle tab characters
|
|
|
|
l := 0
|
|
|
|
for idx, r := range runes {
|
|
|
|
l += runeWidth(r, l)
|
|
|
|
if idx > 0 && l > width {
|
|
|
|
return runes[:idx], len(runes) - idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runes, 0
|
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
|
2015-01-18 02:59:04 -05:00
|
|
|
func displayWidthWithLimit(runes []rune, prefixWidth int, limit int) int {
|
|
|
|
l := 0
|
|
|
|
for _, r := range runes {
|
|
|
|
l += runeWidth(r, l+prefixWidth)
|
|
|
|
if l > limit {
|
|
|
|
// Early exit
|
|
|
|
return l
|
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-18 02:59:04 -05:00
|
|
|
return l
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 12:37:08 -05:00
|
|
|
func trimLeft(runes []rune, width int) ([]rune, int32) {
|
2015-01-01 14:49:30 -05:00
|
|
|
currentWidth := displayWidth(runes)
|
2015-01-11 13:01:24 -05:00
|
|
|
var trimmed int32
|
2015-01-01 14:49:30 -05:00
|
|
|
|
|
|
|
for currentWidth > width && len(runes) > 0 {
|
|
|
|
runes = runes[1:]
|
2015-01-11 13:01:24 -05:00
|
|
|
trimmed++
|
2015-01-18 02:59:04 -05:00
|
|
|
currentWidth = displayWidthWithLimit(runes, 2, width)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
return runes, trimmed
|
|
|
|
}
|
|
|
|
|
2015-04-15 23:56:01 -04:00
|
|
|
func (t *Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, current bool) {
|
2015-01-11 13:01:24 -05:00
|
|
|
var maxe int32
|
2015-01-01 14:49:30 -05:00
|
|
|
for _, offset := range item.offsets {
|
|
|
|
if offset[1] > maxe {
|
|
|
|
maxe = offset[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overflow
|
|
|
|
text := []rune(*item.text)
|
2015-03-22 03:05:54 -04:00
|
|
|
offsets := item.colorOffsets(col2, bold, current)
|
2015-01-01 14:49:30 -05:00
|
|
|
maxWidth := C.MaxX() - 3
|
|
|
|
fullWidth := displayWidth(text)
|
|
|
|
if fullWidth > maxWidth {
|
2015-04-15 23:56:01 -04:00
|
|
|
if t.hscroll {
|
2015-01-01 14:49:30 -05:00
|
|
|
// Stri..
|
2015-04-15 23:56:01 -04:00
|
|
|
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..
|
|
|
|
var diff int32
|
|
|
|
text, diff = trimLeft(text, maxWidth-2)
|
|
|
|
|
|
|
|
// 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 14:49:30 -05:00
|
|
|
}
|
2015-04-15 23:56:01 -04:00
|
|
|
} else {
|
|
|
|
text, _ = trimRight(text, maxWidth-2)
|
|
|
|
text = append(text, []rune("..")...)
|
2015-01-01 14:49:30 -05:00
|
|
|
|
2015-03-18 12:59:14 -04:00
|
|
|
for idx, offset := range offsets {
|
2015-04-15 23:56:01 -04: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 14:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
var index int32
|
2015-01-18 02:59:04 -05:00
|
|
|
var substr string
|
|
|
|
var prefixWidth int
|
2015-03-18 12:59:14 -04:00
|
|
|
maxOffset := int32(len(text))
|
2015-01-01 14:49:30 -05:00
|
|
|
for _, offset := range offsets {
|
2015-03-18 12:59:14 -04:00
|
|
|
b := util.Constrain32(offset.offset[0], index, maxOffset)
|
|
|
|
e := util.Constrain32(offset.offset[1], index, maxOffset)
|
2015-01-18 02:59:04 -05:00
|
|
|
|
|
|
|
substr, prefixWidth = processTabs(text[index:b], prefixWidth)
|
|
|
|
C.CPrint(col1, bold, substr)
|
|
|
|
|
2015-03-18 23:14:26 -04:00
|
|
|
if b < e {
|
|
|
|
substr, prefixWidth = processTabs(text[b:e], prefixWidth)
|
2015-03-22 04:43:28 -04:00
|
|
|
C.CPrint(offset.color, offset.bold, substr)
|
2015-03-18 23:14:26 -04:00
|
|
|
}
|
2015-01-18 02:59:04 -05:00
|
|
|
|
2015-01-01 14:49:30 -05:00
|
|
|
index = e
|
2015-03-18 12:59:14 -04:00
|
|
|
if index >= maxOffset {
|
|
|
|
break
|
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-03-18 12:59:14 -04:00
|
|
|
if index < maxOffset {
|
2015-01-18 02:59:04 -05:00
|
|
|
substr, _ = processTabs(text[index:], prefixWidth)
|
|
|
|
C.CPrint(col1, bold, substr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processTabs(runes []rune, prefixWidth int) (string, int) {
|
|
|
|
var strbuf bytes.Buffer
|
|
|
|
l := prefixWidth
|
|
|
|
for _, r := range runes {
|
|
|
|
w := runeWidth(r, l)
|
|
|
|
l += w
|
|
|
|
if r == '\t' {
|
|
|
|
strbuf.WriteString(strings.Repeat(" ", w))
|
|
|
|
} else {
|
|
|
|
strbuf.WriteRune(r)
|
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-18 02:59:04 -05:00
|
|
|
return strbuf.String(), l
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) printAll() {
|
|
|
|
t.printList()
|
|
|
|
t.printInfo()
|
|
|
|
t.printPrompt()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) refresh() {
|
2015-01-06 22:46:45 -05:00
|
|
|
if !t.suppress {
|
|
|
|
C.Refresh()
|
|
|
|
}
|
2015-01-01 14:49:30 -05: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...)
|
|
|
|
}
|
|
|
|
|
2015-03-31 09:05:02 -04:00
|
|
|
func keyMatch(key int, event C.Event) bool {
|
|
|
|
return event.Type == key || event.Type == C.Rune && int(event.Char) == key-C.AltZ
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Loop is called to start Terminal I/O
|
2015-01-01 14:49:30 -05:00
|
|
|
func (t *Terminal) Loop() {
|
2015-02-17 10:08:17 -05:00
|
|
|
<-t.startChan
|
2015-01-01 14:49:30 -05:00
|
|
|
{ // Late initialization
|
|
|
|
t.mutex.Lock()
|
|
|
|
t.initFunc()
|
|
|
|
t.printPrompt()
|
2015-01-03 15:09:40 -05:00
|
|
|
t.placeCursor()
|
2015-01-06 22:46:45 -05:00
|
|
|
C.Refresh()
|
|
|
|
t.printInfo()
|
2015-01-01 14:49:30 -05:00
|
|
|
t.mutex.Unlock()
|
2015-01-06 22:46:45 -05:00
|
|
|
go func() {
|
2015-01-11 13:01:24 -05:00
|
|
|
timer := time.NewTimer(initialDelay)
|
2015-01-06 22:46:45 -05:00
|
|
|
<-timer.C
|
2015-01-11 13:01:24 -05:00
|
|
|
t.reqBox.Set(reqRefresh, nil)
|
2015-01-06 22:46:45 -05:00
|
|
|
}()
|
2015-01-23 06:30:50 -05:00
|
|
|
|
|
|
|
resizeChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(resizeChan, syscall.SIGWINCH)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-resizeChan
|
|
|
|
t.reqBox.Set(reqRedraw, nil)
|
|
|
|
}
|
|
|
|
}()
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2015-01-11 22:56:17 -05:00
|
|
|
t.reqBox.Wait(func(events *util.Events) {
|
2015-01-01 14:49:30 -05:00
|
|
|
defer events.Clear()
|
|
|
|
t.mutex.Lock()
|
|
|
|
for req := range *events {
|
|
|
|
switch req {
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqPrompt:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.printPrompt()
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqInfo:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.printInfo()
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqList:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.printList()
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqRefresh:
|
2015-01-06 22:46:45 -05:00
|
|
|
t.suppress = false
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqRedraw:
|
2015-01-01 14:49:30 -05:00
|
|
|
C.Clear()
|
2015-01-23 06:30:50 -05:00
|
|
|
C.Endwin()
|
|
|
|
C.Refresh()
|
2015-01-01 14:49:30 -05:00
|
|
|
t.printAll()
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqClose:
|
2015-01-01 14:49:30 -05:00
|
|
|
C.Close()
|
|
|
|
t.output()
|
|
|
|
os.Exit(0)
|
2015-01-11 13:01:24 -05:00
|
|
|
case reqQuit:
|
2015-01-01 14:49:30 -05:00
|
|
|
C.Close()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 15:09:40 -05:00
|
|
|
t.placeCursor()
|
2015-01-01 14:49:30 -05:00
|
|
|
t.mutex.Unlock()
|
|
|
|
})
|
|
|
|
t.refresh()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
looping := true
|
|
|
|
for looping {
|
|
|
|
event := C.GetChar()
|
|
|
|
|
|
|
|
t.mutex.Lock()
|
|
|
|
previousInput := t.input
|
2015-01-11 22:56:17 -05:00
|
|
|
events := []util.EventType{reqPrompt}
|
|
|
|
req := func(evts ...util.EventType) {
|
2015-01-01 14:49:30 -05:00
|
|
|
for _, event := range evts {
|
|
|
|
events = append(events, event)
|
2015-01-11 13:01:24 -05:00
|
|
|
if event == reqClose || event == reqQuit {
|
2015-01-01 14:49:30 -05:00
|
|
|
looping = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-09 22:21:17 -05:00
|
|
|
toggle := func() {
|
2015-02-25 11:42:15 -05:00
|
|
|
if t.cy < t.merger.Length() {
|
|
|
|
item := t.merger.Get(t.cy)
|
2015-04-17 09:23:52 -04:00
|
|
|
if _, found := t.selected[item.index]; !found {
|
2015-01-16 21:07:04 -05:00
|
|
|
var strptr *string
|
|
|
|
if item.origText != nil {
|
|
|
|
strptr = item.origText
|
|
|
|
} else {
|
|
|
|
strptr = item.text
|
|
|
|
}
|
2015-04-17 09:23:52 -04:00
|
|
|
t.selected[item.index] = selectedItem{time.Now(), strptr}
|
2015-01-09 22:21:17 -05:00
|
|
|
} else {
|
2015-04-17 09:23:52 -04:00
|
|
|
delete(t.selected, item.index)
|
2015-01-09 22:21:17 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqInfo)
|
2015-01-09 22:21:17 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-28 13:59:32 -04:00
|
|
|
for _, key := range t.expect {
|
2015-03-31 09:05:02 -04:00
|
|
|
if keyMatch(key, event) {
|
2015-03-28 13:59:32 -04:00
|
|
|
t.pressed = key
|
|
|
|
req(reqClose)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 09:05:02 -04:00
|
|
|
if t.toggleSort > 0 {
|
|
|
|
if keyMatch(t.toggleSort, event) {
|
2015-04-16 01:44:41 -04:00
|
|
|
t.sort = !t.sort
|
|
|
|
t.eventBox.Set(EvtSearchNew, t.sort)
|
2015-03-31 09:05:02 -04:00
|
|
|
t.mutex.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
switch event.Type {
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.Invalid:
|
2015-01-08 08:04:12 -05:00
|
|
|
t.mutex.Unlock()
|
2015-01-01 14:49:30 -05:00
|
|
|
continue
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlA:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.cx = 0
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlB:
|
2015-01-01 14:49:30 -05:00
|
|
|
if t.cx > 0 {
|
2015-01-11 13:01:24 -05:00
|
|
|
t.cx--
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlC, C.CtrlG, C.CtrlQ, C.ESC:
|
|
|
|
req(reqQuit)
|
|
|
|
case C.CtrlD:
|
2015-01-01 14:49:30 -05:00
|
|
|
if !t.delChar() && t.cx == 0 {
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqQuit)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlE:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.cx = len(t.input)
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlF:
|
2015-01-01 14:49:30 -05:00
|
|
|
if t.cx < len(t.input) {
|
2015-01-11 13:01:24 -05:00
|
|
|
t.cx++
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlH:
|
2015-01-01 14:49:30 -05:00
|
|
|
if t.cx > 0 {
|
|
|
|
t.input = append(t.input[:t.cx-1], t.input[t.cx:]...)
|
2015-01-11 13:01:24 -05:00
|
|
|
t.cx--
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.Tab:
|
2015-01-09 11:06:08 -05:00
|
|
|
if t.multi && t.merger.Length() > 0 {
|
2015-01-01 14:49:30 -05:00
|
|
|
toggle()
|
|
|
|
t.vmove(-1)
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.BTab:
|
2015-01-09 11:06:08 -05:00
|
|
|
if t.multi && t.merger.Length() > 0 {
|
2015-01-01 14:49:30 -05:00
|
|
|
toggle()
|
|
|
|
t.vmove(1)
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlJ, C.CtrlN:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.vmove(-1)
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
|
|
|
case C.CtrlK, C.CtrlP:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.vmove(1)
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
|
|
|
case C.CtrlM:
|
|
|
|
req(reqClose)
|
|
|
|
case C.CtrlL:
|
|
|
|
req(reqRedraw)
|
|
|
|
case C.CtrlU:
|
2015-01-01 14:49:30 -05:00
|
|
|
if t.cx > 0 {
|
|
|
|
t.yanked = copySlice(t.input[:t.cx])
|
|
|
|
t.input = t.input[t.cx:]
|
|
|
|
t.cx = 0
|
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlW:
|
2015-01-01 14:49:30 -05:00
|
|
|
if t.cx > 0 {
|
|
|
|
t.rubout("\\s\\S")
|
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.AltBS:
|
2015-01-01 14:49:30 -05:00
|
|
|
if t.cx > 0 {
|
|
|
|
t.rubout("[^[:alnum:]][[:alnum:]]")
|
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.CtrlY:
|
2015-01-16 14:55:29 -05:00
|
|
|
suffix := copySlice(t.input[t.cx:])
|
|
|
|
t.input = append(append(t.input[:t.cx], t.yanked...), suffix...)
|
2015-01-01 14:49:30 -05:00
|
|
|
t.cx += len(t.yanked)
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.Del:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.delChar()
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.PgUp:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.vmove(maxItems() - 1)
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
|
|
|
case C.PgDn:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.vmove(-(maxItems() - 1))
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
|
|
|
case C.AltB:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.cx = findLastMatch("[^[:alnum:]][[:alnum:]]", string(t.input[:t.cx])) + 1
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.AltF:
|
2015-01-01 14:49:30 -05:00
|
|
|
t.cx += findFirstMatch("[[:alnum:]][^[:alnum:]]|(.$)", string(t.input[t.cx:])) + 1
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.AltD:
|
2015-01-01 14:49:30 -05:00
|
|
|
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:]...)
|
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
case C.Rune:
|
2015-01-01 14:49:30 -05:00
|
|
|
prefix := copySlice(t.input[:t.cx])
|
|
|
|
t.input = append(append(prefix, event.Char), t.input[t.cx:]...)
|
2015-01-11 13:01:24 -05:00
|
|
|
t.cx++
|
|
|
|
case C.Mouse:
|
2015-01-01 14:49:30 -05:00
|
|
|
me := event.MouseEvent
|
2015-01-11 22:56:17 -05:00
|
|
|
mx, my := util.Constrain(me.X-len(t.prompt), 0, len(t.input)), me.Y
|
2015-01-01 14:49:30 -05:00
|
|
|
if !t.reverse {
|
|
|
|
my = C.MaxY() - my - 1
|
|
|
|
}
|
|
|
|
if me.S != 0 {
|
|
|
|
// Scroll
|
2015-01-09 22:21:17 -05:00
|
|
|
if t.merger.Length() > 0 {
|
|
|
|
if t.multi && me.Mod {
|
|
|
|
toggle()
|
|
|
|
}
|
|
|
|
t.vmove(me.S)
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
} else if me.Double {
|
|
|
|
// Double-click
|
|
|
|
if my >= 2 {
|
2015-02-25 11:42:15 -05:00
|
|
|
if t.vset(my-2) && t.cy < t.merger.Length() {
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqClose)
|
2015-01-09 22:26:11 -05:00
|
|
|
}
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
} else if me.Down {
|
|
|
|
if my == 0 && mx >= 0 {
|
|
|
|
// Prompt
|
|
|
|
t.cx = mx
|
|
|
|
} else if my >= 2 {
|
|
|
|
// List
|
2015-01-10 00:50:24 -05:00
|
|
|
if t.vset(t.offset+my-2) && t.multi && me.Mod {
|
2015-01-01 14:49:30 -05:00
|
|
|
toggle()
|
|
|
|
}
|
2015-01-11 13:01:24 -05:00
|
|
|
req(reqList)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changed := string(previousInput) != string(t.input)
|
|
|
|
t.mutex.Unlock() // Must be unlocked before touching reqBox
|
|
|
|
|
|
|
|
if changed {
|
2015-04-16 09:13:31 -04:00
|
|
|
t.eventBox.Set(EvtSearchNew, t.sort)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
for _, event := range events {
|
|
|
|
t.reqBox.Set(event, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) constrain() {
|
2015-01-09 11:06:08 -05:00
|
|
|
count := t.merger.Length()
|
2015-01-01 14:49:30 -05:00
|
|
|
height := C.MaxY() - 2
|
|
|
|
diffpos := t.cy - t.offset
|
|
|
|
|
2015-01-11 22:56:17 -05:00
|
|
|
t.cy = util.Constrain(t.cy, 0, count-1)
|
2015-01-01 14:49:30 -05:00
|
|
|
|
|
|
|
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 {
|
2015-01-11 22:56:17 -05:00
|
|
|
t.offset = util.Max(0, count-height)
|
|
|
|
t.cy = util.Constrain(t.offset+diffpos, 0, count-1)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) vmove(o int) {
|
|
|
|
if t.reverse {
|
2015-01-10 00:50:24 -05:00
|
|
|
t.vset(t.cy - o)
|
2015-01-01 14:49:30 -05:00
|
|
|
} else {
|
2015-01-10 00:50:24 -05:00
|
|
|
t.vset(t.cy + o)
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
2015-01-10 00:50:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Terminal) vset(o int) bool {
|
2015-01-11 22:56:17 -05:00
|
|
|
t.cy = util.Constrain(o, 0, t.merger.Length()-1)
|
2015-01-10 00:50:24 -05:00
|
|
|
return t.cy == o
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func maxItems() int {
|
|
|
|
return C.MaxY() - 2
|
|
|
|
}
|