Files
BlackAngelTV 42c097c69a 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
2026-12-03 11:01:11 +01:00

109 lines
2.9 KiB
Go

// License: GPLv3 Copyright: 2026, Kovid Goyal, <kovid at kovidgoyal.net>
package hints
// hints_to_skip calculates how many hints to skip based on the given n and
// alphabet size so that the next n hints generated by expanding the prefix tree
// of an alphabet of size alphabetSize will be prefix-free.
//
// The result is 1-based, meaning that the first hint to be skipped is the first
// non-empty hint.
//
// For example, if alphabet was "abc" and n was 5, the first non-empty hints
// from that alphabet would be: "a", "b", "c", "aa", "ab", "ac", ...
// the result of hints_to_skip would be 1, and this would mean that if you skip
// "a" from the list above, the next 5 hints are prefix-free:
// "b", "c", "aa", "ab", "ac".
//
// See issue #10195 for an informal proof on why this function works.
func hints_to_skip(n int, alphabetSize int) int {
if n < 2 {
n = 2
}
return (n - 2) / (alphabetSize - 1)
}
// encode_hint generates the hint corresponding to the given index in the
// breadth-first traversal of the prefix tree generated by the given alphabet.
//
// For example, given the alphabet "abc", the hints would be generated in
// the following order:
// 0. "a"
// 1. "b"
// 2. "c"
// 3. "aa"
// 4. "ab"
// 5. "ac"
// 6. "ba"
// 7. "bb"
// 8. "bc"
// 9. "ca"
// 10. "cb"
// 11. "cc"
// 12. "aaa"
// ...
//
// Indices are 0-based on non-empty hints; index=0 the hint of length 1 with
// the first character of the alphabet.
func encode_hint(num int, alphabet string) (res string) {
l := len(alphabet)
for num >= 0 {
mod := num % l
res = string(alphabet[mod]) + res
num /= l
num -= 1
}
return
}
func rune_to_index_map(alphabet string) *map[rune]int {
runeToIndexMap := make(map[rune]int)
for index, char := range alphabet {
runeToIndexMap[char] = index
}
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.
//
// For example, given the alphabet "abc", the hints would be generated in
// the following order:
// 0. "a"
// 1. "b"
// 2. "c"
// 3. "aa"
// 4. "ab"
// ...
//
// hint is expected to be non-empty.
func decode_hint(hint string, char_to_index *map[rune]int) (res int) {
base := len(*char_to_index)
for _, c := range hint {
res = res*base + ((*char_to_index)[c] + 1)
}
return res - 1
}