feat(clipboard): show the paste target and navigate the history picker
The clipboard history picker listed the entries without saying when they were copied or which window would receive the paste, and the only way to pick an entry was to read its hint label and type the letters, which the picker never showed. Each entry now carries the time it was copied and the window that will be pasted into is named above the list, with a footer documenting the keys. Pressing Up/Down moves the highlighted entry and Enter pastes it, through a new --pick-from-list mode of the hints kitten that keeps the hint letters working as before. Fixes #49
This commit is contained in:
8 files changed
+238
-44
No files matched your search
@@ -66,6 +66,24 @@ func rune_to_index_map(alphabet string) *map[rune]int {
|
||||
return &runeToIndexMap
|
||||
}
|
||||
|
||||
// next_selection moves pos by delta positions in a list of marks choices,
|
||||
// wrapping around, as the arrow keys do in the list picker. A negative pos,
|
||||
// meaning nothing is selected yet, selects the first choice when moving down
|
||||
// and the last one when moving up, and a stale pos falls back to the first
|
||||
// choice so that the arrow keys never stop working.
|
||||
func next_selection(pos, marks, delta int) int {
|
||||
if marks < 1 {
|
||||
return -1
|
||||
}
|
||||
if pos < 0 || pos >= marks {
|
||||
if delta < 0 {
|
||||
return marks - 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
return ((pos+delta)%marks + marks) % marks
|
||||
}
|
||||
|
||||
// decode_hint converts a hint to its corresponding index as it would appear in
|
||||
// the breadth-first traversal of the prefix tree generated by the given
|
||||
// alphabet.
|
||||
|
||||
+110
-24
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
@@ -160,6 +161,10 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
}
|
||||
}
|
||||
chosen := []*Mark{}
|
||||
// The list navigation is only offered when the input is an ordered list of
|
||||
// choices, see the --pick-from-list option.
|
||||
pick_from_list := o.PickFromList
|
||||
selected_mark := -1
|
||||
lp, err := loop.New(loop.NoAlternateScreen) // no alternate screen reduces flicker on exit
|
||||
if err != nil {
|
||||
return
|
||||
@@ -168,8 +173,9 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
faint := fctx.SprintFunc("dim")
|
||||
hint_style := fctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s bold", o.HintsForegroundColor, o.HintsBackgroundColor))
|
||||
text_style := fctx.SprintFunc(fmt.Sprintf("fg=%s bg=%s bold", o.HintsTextColor, o.HintsTextBackgroundColor))
|
||||
selected_style := fctx.SprintFunc("reverse")
|
||||
|
||||
highlight_mark := func(m *Mark, mark_text string) string {
|
||||
highlight_mark := func(m *Mark, mark_text string, selected bool) string {
|
||||
hint := encode_hint(m.Index, alphabet)
|
||||
if current_input != "" && !strings.HasPrefix(hint, current_input) {
|
||||
return faint(mark_text)
|
||||
@@ -207,6 +213,9 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
mark_text = string(runes[hint_runes:])
|
||||
}
|
||||
ans := hint_style(hint) + text_style(mark_text)
|
||||
if selected {
|
||||
ans = selected_style(ans)
|
||||
}
|
||||
return fmt.Sprintf("\x1b]8;;mark:%d\a%s\x1b]8;;\a", m.Index, ans)
|
||||
}
|
||||
|
||||
@@ -217,7 +226,7 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
if ignore_mark_indices.Has(mark.Index) {
|
||||
continue
|
||||
}
|
||||
mtext := highlight_mark(mark, ans[mark.Start:mark.End])
|
||||
mtext := highlight_mark(mark, ans[mark.Start:mark.End], pick_from_list && mark.Index == selected_mark)
|
||||
ans = ans[:mark.Start] + mtext + ans[mark.End:]
|
||||
}
|
||||
ans = strings.ReplaceAll(ans, "\x00", "")
|
||||
@@ -238,6 +247,75 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
current_text = ""
|
||||
}
|
||||
|
||||
// The marks in the order they were given, which is the order of the list
|
||||
// the user sees rather than the order of the hints
|
||||
ordered_indices := make([]int, 0, len(index_map))
|
||||
for idx := range index_map {
|
||||
ordered_indices = append(ordered_indices, idx)
|
||||
}
|
||||
sort.Ints(ordered_indices)
|
||||
visible_marks := func() []*Mark {
|
||||
ans := make([]*Mark, 0, len(ordered_indices))
|
||||
for _, idx := range ordered_indices {
|
||||
m := index_map[idx]
|
||||
if m == nil || ignore_mark_indices.Has(m.Index) {
|
||||
continue
|
||||
}
|
||||
if current_input != "" && !strings.HasPrefix(encode_hint(idx, alphabet), current_input) {
|
||||
continue
|
||||
}
|
||||
ans = append(ans, m)
|
||||
}
|
||||
return ans
|
||||
}
|
||||
select_first := func() {
|
||||
selected_mark = -1
|
||||
if marks := visible_marks(); len(marks) > 0 {
|
||||
selected_mark = marks[0].Index
|
||||
}
|
||||
}
|
||||
choose_index := func(idx int) {
|
||||
m := index_map[idx]
|
||||
if m == nil {
|
||||
current_input = ""
|
||||
current_text = ""
|
||||
draw_screen()
|
||||
return
|
||||
}
|
||||
chosen = append(chosen, m)
|
||||
ignore_mark_indices.Add(m.Index)
|
||||
if o.Multiple {
|
||||
reset()
|
||||
select_first()
|
||||
draw_screen()
|
||||
} else {
|
||||
lp.Quit(0)
|
||||
}
|
||||
}
|
||||
select_by := func(delta int) {
|
||||
marks := visible_marks()
|
||||
if len(marks) == 0 {
|
||||
return
|
||||
}
|
||||
pos := -1
|
||||
for i, m := range marks {
|
||||
if m.Index == selected_mark {
|
||||
pos = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if pos < 0 {
|
||||
pos = 0
|
||||
}
|
||||
pos = next_selection(pos, len(marks), delta)
|
||||
selected_mark = marks[pos].Index
|
||||
current_text = ""
|
||||
draw_screen()
|
||||
}
|
||||
if pick_from_list {
|
||||
select_first()
|
||||
}
|
||||
|
||||
lp.OnInitialize = func() (string, error) {
|
||||
lp.SetCursorVisible(false)
|
||||
lp.SetWindowTitle(window_title)
|
||||
@@ -295,13 +373,15 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
}
|
||||
if len(matches) == 1 {
|
||||
chosen = append(chosen, matches[0])
|
||||
if o.Multiple {
|
||||
ignore_mark_indices.Add(matches[0].Index)
|
||||
reset()
|
||||
} else {
|
||||
ignore_mark_indices.Add(matches[0].Index)
|
||||
if !o.Multiple {
|
||||
lp.Quit(0)
|
||||
return nil
|
||||
}
|
||||
reset()
|
||||
}
|
||||
if pick_from_list {
|
||||
select_first()
|
||||
}
|
||||
current_text = ""
|
||||
draw_screen()
|
||||
@@ -310,7 +390,14 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
}
|
||||
|
||||
lp.OnKeyEvent = func(ev *loop.KeyEvent) error {
|
||||
if ev.MatchesPressOrRepeat("backspace") {
|
||||
if pick_from_list && (ev.MatchesPressOrRepeat("up") || ev.MatchesPressOrRepeat("down")) {
|
||||
ev.Handled = true
|
||||
if ev.MatchesPressOrRepeat("up") {
|
||||
select_by(-1)
|
||||
} else {
|
||||
select_by(1)
|
||||
}
|
||||
} else if ev.MatchesPressOrRepeat("backspace") {
|
||||
ev.Handled = true
|
||||
r := []rune(current_input)
|
||||
if len(r) > 0 {
|
||||
@@ -318,26 +405,25 @@ func main(_ *cli.Command, o *Options, args []string) (rc int, err error) {
|
||||
current_input = string(r)
|
||||
current_text = ""
|
||||
}
|
||||
if pick_from_list {
|
||||
select_first()
|
||||
}
|
||||
draw_screen()
|
||||
} else if ev.MatchesPressOrRepeat("enter") || ev.MatchesPressOrRepeat("space") {
|
||||
} else if ev.MatchesPressOrRepeat("enter") {
|
||||
ev.Handled = true
|
||||
if current_input != "" {
|
||||
char_to_index := rune_to_index_map(alphabet)
|
||||
idx := decode_hint(current_input, char_to_index)
|
||||
if m := index_map[idx]; m != nil {
|
||||
chosen = append(chosen, m)
|
||||
ignore_mark_indices.Add(idx)
|
||||
if o.Multiple {
|
||||
reset()
|
||||
draw_screen()
|
||||
} else {
|
||||
lp.Quit(0)
|
||||
}
|
||||
} else {
|
||||
current_input = ""
|
||||
current_text = ""
|
||||
draw_screen()
|
||||
if pick_from_list {
|
||||
if selected_mark > -1 {
|
||||
choose_index(selected_mark)
|
||||
} else if marks := visible_marks(); len(marks) > 0 {
|
||||
choose_index(marks[0].Index)
|
||||
}
|
||||
} else if current_input != "" {
|
||||
choose_index(decode_hint(current_input, rune_to_index_map(alphabet)))
|
||||
}
|
||||
} else if ev.MatchesPressOrRepeat("space") {
|
||||
ev.Handled = true
|
||||
if !pick_from_list && current_input != "" {
|
||||
choose_index(decode_hint(current_input, rune_to_index_map(alphabet)))
|
||||
}
|
||||
} else if ev.MatchesPressOrRepeat("esc") {
|
||||
if o.Multiple {
|
||||
|
||||
@@ -271,6 +271,14 @@ elsewhere. See {hints_url} for details.
|
||||
--window-title
|
||||
The title for the hints window, default title is based on the type of text being
|
||||
hinted.
|
||||
|
||||
|
||||
--pick-from-list
|
||||
type=bool-set
|
||||
The input is an ordered list of choices rather than text to search, as used by the
|
||||
clipboard history picker. The first choice is selected and the arrow keys move the
|
||||
selection, so a choice can be picked with :kbd:`Enter` instead of typing the
|
||||
letters of its hint.
|
||||
""".format(
|
||||
default_regex=DEFAULT_REGEX,
|
||||
line='{{line}}',
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||
|
||||
package hints
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNextSelection(t *testing.T) {
|
||||
cases := []struct {
|
||||
pos, marks, delta, expected int
|
||||
}{
|
||||
// Nothing selected yet: down selects the first choice, up the last one
|
||||
{-1, 3, 0, 0},
|
||||
{-1, 3, 1, 0},
|
||||
{-1, 3, -1, 2},
|
||||
// Moving past either end wraps around
|
||||
{0, 3, 1, 1},
|
||||
{2, 3, 1, 0},
|
||||
{0, 3, -1, 2},
|
||||
{1, 1, 2, 0},
|
||||
{0, 3, 4, 1},
|
||||
{0, 3, -4, 2},
|
||||
// A stale position falls back to the first choice
|
||||
{5, 3, 1, 0},
|
||||
// An empty list has nothing to select
|
||||
{-1, 0, 1, -1},
|
||||
{0, 0, -1, -1},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := next_selection(c.pos, c.marks, c.delta); got != c.expected {
|
||||
t.Fatalf("next_selection(%d, %d, %d) = %d, expected %d", c.pos, c.marks, c.delta, got, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user