Show textinput completions in popovers
Rather than showing completions inline in the text input, show them in a popover which can be scrolled by repeatedly pressing the tab key. The selected completion can be executed by pressing enter.
This commit is contained in:
parent
bcd03c4c4a
commit
7160f98a90
|
@ -99,6 +99,17 @@ header-layout=From|To,Cc|Bcc,Date,Subject
|
||||||
# Default: false
|
# Default: false
|
||||||
always-show-mime=false
|
always-show-mime=false
|
||||||
|
|
||||||
|
# How long to wait after the last input before auto-completion is triggered.
|
||||||
|
#
|
||||||
|
# Default: 250ms
|
||||||
|
completion-delay=250ms
|
||||||
|
|
||||||
|
#
|
||||||
|
# Global switch for completion popovers
|
||||||
|
#
|
||||||
|
# Default: true
|
||||||
|
completion-popovers=true
|
||||||
|
|
||||||
[compose]
|
[compose]
|
||||||
#
|
#
|
||||||
# Specifies the command to run the editor with. It will be shown in an embedded
|
# Specifies the command to run the editor with. It will be shown in an embedded
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
|
@ -40,6 +41,8 @@ type UIConfig struct {
|
||||||
DirListFormat string `ini:"dirlist-format"`
|
DirListFormat string `ini:"dirlist-format"`
|
||||||
Sort []string `delim:" "`
|
Sort []string `delim:" "`
|
||||||
NextMessageOnDelete bool `ini:"next-message-on-delete"`
|
NextMessageOnDelete bool `ini:"next-message-on-delete"`
|
||||||
|
CompletionDelay time.Duration `ini:"completion-delay"`
|
||||||
|
CompletionPopovers bool `ini:"completion-popovers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -387,6 +390,8 @@ func LoadConfigFromFile(root *string, sharedir string) (*AercConfig, error) {
|
||||||
SpinnerDelimiter: ",",
|
SpinnerDelimiter: ",",
|
||||||
DirListFormat: "%n %>r",
|
DirListFormat: "%n %>r",
|
||||||
NextMessageOnDelete: true,
|
NextMessageOnDelete: true,
|
||||||
|
CompletionDelay: 250 * time.Millisecond,
|
||||||
|
CompletionPopovers: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
Viewer: ViewerConfig{
|
Viewer: ViewerConfig{
|
||||||
|
|
|
@ -156,6 +156,16 @@ These options are configured in the *[ui]* section of aerc.conf.
|
||||||
|
|
||||||
Default: true
|
Default: true
|
||||||
|
|
||||||
|
*completion-popovers*
|
||||||
|
Shows potential auto-completions for text inputs in popovers.
|
||||||
|
|
||||||
|
Default: true
|
||||||
|
|
||||||
|
*completion-delay*
|
||||||
|
How long to wait after the last input before auto-completion is triggered.
|
||||||
|
|
||||||
|
Default: 250ms
|
||||||
|
|
||||||
## VIEWER
|
## VIEWER
|
||||||
|
|
||||||
These options are configured in the *[viewer]* section of aerc.conf.
|
These options are configured in the *[viewer]* section of aerc.conf.
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
"github.com/mattn/go-runewidth"
|
"github.com/mattn/go-runewidth"
|
||||||
)
|
)
|
||||||
|
@ -22,6 +25,8 @@ type TextInput struct {
|
||||||
tabcomplete func(s string) []string
|
tabcomplete func(s string) []string
|
||||||
completions []string
|
completions []string
|
||||||
completeIndex int
|
completeIndex int
|
||||||
|
completeDelay time.Duration
|
||||||
|
completeDebouncer *time.Timer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new TextInput. TextInputs will render a "textbox" in the entire
|
// Creates a new TextInput. TextInputs will render a "textbox" in the entire
|
||||||
|
@ -46,8 +51,9 @@ func (ti *TextInput) Prompt(prompt string) *TextInput {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ti *TextInput) TabComplete(
|
func (ti *TextInput) TabComplete(
|
||||||
tabcomplete func(s string) []string) *TextInput {
|
tabcomplete func(s string) []string, d time.Duration) *TextInput {
|
||||||
ti.tabcomplete = tabcomplete
|
ti.tabcomplete = tabcomplete
|
||||||
|
ti.completeDelay = d
|
||||||
return ti
|
return ti
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,9 +101,37 @@ func (ti *TextInput) Draw(ctx *Context) {
|
||||||
cells := runewidth.StringWidth(string(text[:sindex]) + ti.prompt)
|
cells := runewidth.StringWidth(string(text[:sindex]) + ti.prompt)
|
||||||
if ti.focus {
|
if ti.focus {
|
||||||
ctx.SetCursor(cells, 0)
|
ctx.SetCursor(cells, 0)
|
||||||
|
ti.drawPopover(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ti *TextInput) drawPopover(ctx *Context) {
|
||||||
|
if len(ti.completions) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmp := &completions{
|
||||||
|
options: ti.completions,
|
||||||
|
idx: ti.completeIndex,
|
||||||
|
stringLeft: ti.StringLeft(),
|
||||||
|
onSelect: func(idx int) {
|
||||||
|
ti.completeIndex = idx
|
||||||
|
ti.Invalidate()
|
||||||
|
},
|
||||||
|
onExec: func() {
|
||||||
|
ti.executeCompletion()
|
||||||
|
ti.invalidateCompletions()
|
||||||
|
ti.Invalidate()
|
||||||
|
},
|
||||||
|
onStem: func(stem string) {
|
||||||
|
ti.Set(stem + ti.StringRight())
|
||||||
|
ti.Invalidate()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
width := maxLen(ti.completions) + 3
|
||||||
|
height := len(ti.completions)
|
||||||
|
ctx.Popover(0, 0, width, height, cmp)
|
||||||
|
}
|
||||||
|
|
||||||
func (ti *TextInput) MouseEvent(localX int, localY int, event tcell.Event) {
|
func (ti *TextInput) MouseEvent(localX int, localY int, event tcell.Event) {
|
||||||
switch event := event.(type) {
|
switch event := event.(type) {
|
||||||
case *tcell.EventMouse:
|
case *tcell.EventMouse:
|
||||||
|
@ -208,32 +242,7 @@ func (ti *TextInput) backspace() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ti *TextInput) nextCompletion() {
|
func (ti *TextInput) executeCompletion() {
|
||||||
if ti.completions == nil {
|
|
||||||
if ti.tabcomplete == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ti.completions = ti.tabcomplete(ti.StringLeft())
|
|
||||||
ti.completeIndex = 0
|
|
||||||
} else {
|
|
||||||
ti.completeIndex++
|
|
||||||
if ti.completeIndex >= len(ti.completions) {
|
|
||||||
ti.completeIndex = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(ti.completions) > 0 {
|
|
||||||
ti.Set(ti.completions[ti.completeIndex] + ti.StringRight())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ti *TextInput) previousCompletion() {
|
|
||||||
if ti.completions == nil || len(ti.completions) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ti.completeIndex--
|
|
||||||
if ti.completeIndex < 0 {
|
|
||||||
ti.completeIndex = len(ti.completions) - 1
|
|
||||||
}
|
|
||||||
if len(ti.completions) > 0 {
|
if len(ti.completions) > 0 {
|
||||||
ti.Set(ti.completions[ti.completeIndex] + ti.StringRight())
|
ti.Set(ti.completions[ti.completeIndex] + ti.StringRight())
|
||||||
}
|
}
|
||||||
|
@ -244,11 +253,33 @@ func (ti *TextInput) invalidateCompletions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ti *TextInput) onChange() {
|
func (ti *TextInput) onChange() {
|
||||||
|
ti.updateCompletions()
|
||||||
for _, change := range ti.change {
|
for _, change := range ti.change {
|
||||||
change(ti)
|
change(ti)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ti *TextInput) updateCompletions() {
|
||||||
|
if ti.tabcomplete == nil {
|
||||||
|
// no completer
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ti.completeDebouncer == nil {
|
||||||
|
ti.completeDebouncer = time.AfterFunc(ti.completeDelay, func() {
|
||||||
|
ti.showCompletions()
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
ti.completeDebouncer.Stop()
|
||||||
|
ti.completeDebouncer.Reset(ti.completeDelay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ti *TextInput) showCompletions() {
|
||||||
|
ti.completions = ti.tabcomplete(ti.StringLeft())
|
||||||
|
ti.completeIndex = 0
|
||||||
|
ti.Invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
func (ti *TextInput) OnChange(onChange func(ti *TextInput)) {
|
func (ti *TextInput) OnChange(onChange func(ti *TextInput)) {
|
||||||
ti.change = append(ti.change, onChange)
|
ti.change = append(ti.change, onChange)
|
||||||
}
|
}
|
||||||
|
@ -296,18 +327,13 @@ func (ti *TextInput) Event(event tcell.Event) bool {
|
||||||
case tcell.KeyCtrlU:
|
case tcell.KeyCtrlU:
|
||||||
ti.invalidateCompletions()
|
ti.invalidateCompletions()
|
||||||
ti.deleteLineBackward()
|
ti.deleteLineBackward()
|
||||||
|
case tcell.KeyESC:
|
||||||
|
if ti.completions != nil {
|
||||||
|
ti.invalidateCompletions()
|
||||||
|
ti.Invalidate()
|
||||||
|
}
|
||||||
case tcell.KeyTab:
|
case tcell.KeyTab:
|
||||||
if ti.tabcomplete != nil {
|
ti.showCompletions()
|
||||||
ti.nextCompletion()
|
|
||||||
} else {
|
|
||||||
ti.insert('\t')
|
|
||||||
}
|
|
||||||
ti.Invalidate()
|
|
||||||
case tcell.KeyBacktab:
|
|
||||||
if ti.tabcomplete != nil {
|
|
||||||
ti.previousCompletion()
|
|
||||||
}
|
|
||||||
ti.Invalidate()
|
|
||||||
case tcell.KeyRune:
|
case tcell.KeyRune:
|
||||||
ti.invalidateCompletions()
|
ti.invalidateCompletions()
|
||||||
ti.insert(event.Rune())
|
ti.insert(event.Rune())
|
||||||
|
@ -315,3 +341,150 @@ func (ti *TextInput) Event(event tcell.Event) bool {
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type completions struct {
|
||||||
|
options []string
|
||||||
|
stringLeft string
|
||||||
|
idx int
|
||||||
|
onSelect func(int)
|
||||||
|
onExec func()
|
||||||
|
onStem func(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxLen(ss []string) int {
|
||||||
|
max := 0
|
||||||
|
for _, s := range ss {
|
||||||
|
l := runewidth.StringWidth(s)
|
||||||
|
if l > max {
|
||||||
|
max = l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *completions) Draw(ctx *Context) {
|
||||||
|
bg := tcell.StyleDefault
|
||||||
|
sel := tcell.StyleDefault.Reverse(true)
|
||||||
|
gutter := tcell.StyleDefault
|
||||||
|
pill := tcell.StyleDefault.Reverse(true)
|
||||||
|
|
||||||
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', bg)
|
||||||
|
|
||||||
|
numVisible := ctx.Height()
|
||||||
|
startIdx := 0
|
||||||
|
if len(c.options) > numVisible && c.idx+1 > numVisible {
|
||||||
|
startIdx = c.idx - (numVisible - 1)
|
||||||
|
}
|
||||||
|
endIdx := startIdx + numVisible - 1
|
||||||
|
|
||||||
|
for idx, opt := range c.options {
|
||||||
|
if idx < startIdx {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if idx > endIdx {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c.idx == idx {
|
||||||
|
ctx.Fill(0, idx-startIdx, ctx.Width(), 1, ' ', sel)
|
||||||
|
ctx.Printf(0, idx-startIdx, sel, " %s ", opt)
|
||||||
|
} else {
|
||||||
|
ctx.Printf(0, idx-startIdx, bg, " %s ", opt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
percentVisible := float64(numVisible) / float64(len(c.options))
|
||||||
|
if percentVisible >= 1.0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// gutter
|
||||||
|
ctx.Fill(ctx.Width()-1, 0, 1, ctx.Height(), ' ', gutter)
|
||||||
|
|
||||||
|
pillSize := int(math.Ceil(float64(ctx.Height()) * percentVisible))
|
||||||
|
percentScrolled := float64(startIdx) / float64(len(c.options))
|
||||||
|
pillOffset := int(math.Floor(float64(ctx.Height()) * percentScrolled))
|
||||||
|
ctx.Fill(ctx.Width()-1, pillOffset, 1, pillSize, ' ', pill)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *completions) next() {
|
||||||
|
idx := c.idx
|
||||||
|
idx++
|
||||||
|
if idx > len(c.options)-1 {
|
||||||
|
idx = 0
|
||||||
|
}
|
||||||
|
c.onSelect(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *completions) prev() {
|
||||||
|
idx := c.idx
|
||||||
|
idx--
|
||||||
|
if idx < 0 {
|
||||||
|
idx = len(c.options) - 1
|
||||||
|
}
|
||||||
|
c.onSelect(idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *completions) Event(e tcell.Event) bool {
|
||||||
|
switch e := e.(type) {
|
||||||
|
case *tcell.EventKey:
|
||||||
|
switch e.Key() {
|
||||||
|
case tcell.KeyTab:
|
||||||
|
if len(c.options) == 1 {
|
||||||
|
c.onExec()
|
||||||
|
} else {
|
||||||
|
stem := findStem(c.options)
|
||||||
|
if stem != "" && stem != c.stringLeft {
|
||||||
|
c.onStem(stem)
|
||||||
|
} else {
|
||||||
|
c.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
case tcell.KeyCtrlN, tcell.KeyDown:
|
||||||
|
c.next()
|
||||||
|
return true
|
||||||
|
case tcell.KeyBacktab, tcell.KeyCtrlP, tcell.KeyUp:
|
||||||
|
c.prev()
|
||||||
|
return true
|
||||||
|
case tcell.KeyEnter:
|
||||||
|
c.onExec()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func findStem(words []string) string {
|
||||||
|
if len(words) <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if len(words) == 1 {
|
||||||
|
return words[0]
|
||||||
|
}
|
||||||
|
var stem string
|
||||||
|
stemLen := 1
|
||||||
|
firstWord := []rune(words[0])
|
||||||
|
for {
|
||||||
|
if len(firstWord) < stemLen {
|
||||||
|
return stem
|
||||||
|
}
|
||||||
|
var r rune = firstWord[stemLen-1]
|
||||||
|
for _, word := range words[1:] {
|
||||||
|
runes := []rune(word)
|
||||||
|
if len(runes) < stemLen {
|
||||||
|
return stem
|
||||||
|
}
|
||||||
|
if runes[stemLen-1] != r {
|
||||||
|
return stem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stem = stem + string(r)
|
||||||
|
stemLen++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *completions) Focus(_ bool) {}
|
||||||
|
|
||||||
|
func (c *completions) Invalidate() {}
|
||||||
|
|
||||||
|
func (c *completions) OnInvalidate(_ func(Drawable)) {}
|
||||||
|
|
|
@ -372,7 +372,7 @@ func (aerc *Aerc) focus(item ui.Interactive) {
|
||||||
|
|
||||||
func (aerc *Aerc) BeginExCommand(cmd string) {
|
func (aerc *Aerc) BeginExCommand(cmd string) {
|
||||||
previous := aerc.focused
|
previous := aerc.focused
|
||||||
exline := NewExLine(cmd, func(cmd string) {
|
exline := NewExLine(aerc.conf, cmd, func(cmd string) {
|
||||||
parts, err := shlex.Split(cmd)
|
parts, err := shlex.Split(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
aerc.PushStatus(" "+err.Error(), 10*time.Second).
|
aerc.PushStatus(" "+err.Error(), 10*time.Second).
|
||||||
|
@ -399,7 +399,7 @@ func (aerc *Aerc) BeginExCommand(cmd string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aerc *Aerc) RegisterPrompt(prompt string, cmd []string) {
|
func (aerc *Aerc) RegisterPrompt(prompt string, cmd []string) {
|
||||||
p := NewPrompt(prompt, func(text string) {
|
p := NewPrompt(aerc.conf, prompt, func(text string) {
|
||||||
if text != "" {
|
if text != "" {
|
||||||
cmd = append(cmd, text)
|
cmd = append(cmd, text)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package widgets
|
||||||
import (
|
import (
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
|
|
||||||
|
"git.sr.ht/~sircmpwn/aerc/config"
|
||||||
"git.sr.ht/~sircmpwn/aerc/lib"
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
||||||
"git.sr.ht/~sircmpwn/aerc/lib/ui"
|
"git.sr.ht/~sircmpwn/aerc/lib/ui"
|
||||||
)
|
)
|
||||||
|
@ -16,11 +17,14 @@ type ExLine struct {
|
||||||
input *ui.TextInput
|
input *ui.TextInput
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExLine(cmd string, commit func(cmd string), finish func(),
|
func NewExLine(conf *config.AercConfig, cmd string, commit func(cmd string), finish func(),
|
||||||
tabcomplete func(cmd string) []string,
|
tabcomplete func(cmd string) []string,
|
||||||
cmdHistory lib.History) *ExLine {
|
cmdHistory lib.History) *ExLine {
|
||||||
|
|
||||||
input := ui.NewTextInput("").Prompt(":").TabComplete(tabcomplete).Set(cmd)
|
input := ui.NewTextInput("").Prompt(":").Set(cmd)
|
||||||
|
if conf.Ui.CompletionPopovers {
|
||||||
|
input.TabComplete(tabcomplete, conf.Ui.CompletionDelay)
|
||||||
|
}
|
||||||
exline := &ExLine{
|
exline := &ExLine{
|
||||||
commit: commit,
|
commit: commit,
|
||||||
finish: finish,
|
finish: finish,
|
||||||
|
@ -34,10 +38,13 @@ func NewExLine(cmd string, commit func(cmd string), finish func(),
|
||||||
return exline
|
return exline
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPrompt(prompt string, commit func(text string),
|
func NewPrompt(conf *config.AercConfig, prompt string, commit func(text string),
|
||||||
tabcomplete func(cmd string) []string) *ExLine {
|
tabcomplete func(cmd string) []string) *ExLine {
|
||||||
|
|
||||||
input := ui.NewTextInput("").Prompt(prompt).TabComplete(tabcomplete)
|
input := ui.NewTextInput("").Prompt(prompt)
|
||||||
|
if conf.Ui.CompletionPopovers {
|
||||||
|
input.TabComplete(tabcomplete, conf.Ui.CompletionDelay)
|
||||||
|
}
|
||||||
exline := &ExLine{
|
exline := &ExLine{
|
||||||
commit: commit,
|
commit: commit,
|
||||||
tabcomplete: tabcomplete,
|
tabcomplete: tabcomplete,
|
||||||
|
|
Loading…
Reference in New Issue