feat(command-palette): add a read-only reference mode

The shortcuts help panel needs the palette's search and categories without
running an action when a binding is chosen, and it lives in a dock where input
comes from the terminal rather than the overlay protocol. Read the bindings from
a file and skip execution in that mode.
This commit is contained in:
BlackAngelTV committed 2026-11-12 12:43:46 +01:00
1 parent 8482515412
commit 45a16ea61c
3 files changed
+78 -12

No files matched your search

+49 -12
View File
@@ -275,6 +275,8 @@ type Handler struct {
cv *utils.CachedValues[*CachedSettings]
shortcut_tracker config.ShortcutTracker
keyboard_shortcuts []*config.KeyAction
read_only bool
data_file string
}
// initialize sets up the TUI: screen size, cursor, cached settings, and initial data load.
@@ -287,17 +289,30 @@ func (h *Handler) initialize() (string, error) {
h.lp.SetCursorVisible(true)
h.lp.SetCursorShape(loop.BAR_CURSOR, true)
h.lp.AllowLineWrapping(false)
h.lp.SetWindowTitle("Command Palette")
title := "Command Palette"
if h.read_only {
title = "Keyboard shortcuts"
}
h.lp.SetWindowTitle(title)
// Initialize with ShowUnmapped: true as the default; Load() returns this
// default when no cache file exists yet.
h.cv = utils.NewCachedValues("command-palette", &CachedSettings{ShowUnmapped: true})
settings := h.cv.Load()
h.show_unmapped = settings.ShowUnmapped
if h.read_only {
// A reference panel is about bindings, not about actions without one.
h.show_unmapped = false
} else {
// Initialize with ShowUnmapped: true as the default; Load() returns this
// default when no cache file exists yet.
h.cv = utils.NewCachedValues("command-palette", &CachedSettings{ShowUnmapped: true})
settings := h.cv.Load()
h.show_unmapped = settings.ShowUnmapped
}
h.keyboard_shortcuts = config.ResolveShortcuts(NewConfig().KeyboardShortcuts)
if err := h.loadData(); err != nil {
if h.data_file != "" {
if err := h.loadDataFile(); err != nil {
return "", err
}
} else if err := h.loadData(); err != nil {
return "", err
}
@@ -313,8 +328,23 @@ func (h *Handler) loadData() error {
if err != nil {
return fmt.Errorf("failed to read stdin: %w", err)
}
return h.parseData(data)
}
// loadDataFile reads the JSON produced by the help kitten and removes it so
// that repeated launches do not leave temporary files behind.
func (h *Handler) loadDataFile() error {
data, err := os.ReadFile(h.data_file)
if err != nil {
return fmt.Errorf("failed to read keybinding data: %w", err)
}
_ = os.Remove(h.data_file)
return h.parseData(data)
}
func (h *Handler) parseData(data []byte) error {
if len(data) == 0 {
return fmt.Errorf("no input data received on stdin; this kitten must be launched from kitty")
return fmt.Errorf("no keybinding data received")
}
if err := json.Unmarshal(data, &h.input_data); err != nil {
return fmt.Errorf("failed to parse input data: %w", err)
@@ -584,8 +614,11 @@ func (h *Handler) draw_screen() {
if h.show_unmapped {
unmappedToggleLabel = "Hide"
}
footer := h.lp.SprintStyled("fg=bright-yellow", "[Enter]") + " Run " +
h.lp.SprintStyled("fg=bright-yellow", "[Esc]") + " Quit " +
footer := ""
if !h.read_only {
footer = h.lp.SprintStyled("fg=bright-yellow", "[Enter]") + " Run "
}
footer += h.lp.SprintStyled("fg=bright-yellow", "[Esc]") + " Quit " +
h.lp.SprintStyled("fg=bright-yellow", "\u2191\u2193") + " Navigate " +
h.lp.SprintStyled("fg=bright-yellow", "[F12]") + " " + unmappedToggleLabel + " unmapped"
matchCount := ""
@@ -1005,6 +1038,10 @@ func (h *Handler) moveSelection(delta int) {
// triggerSelected sets the selected binding's definition as the result and exits.
func (h *Handler) triggerSelected() {
if h.read_only {
h.lp.Beep()
return
}
b := h.selectedBinding()
if b == nil || b.IsMouse {
h.lp.Beep()
@@ -1016,7 +1053,7 @@ func (h *Handler) triggerSelected() {
// main runs the command palette TUI as a kitty overlay.
func main(cmd *cli.Command, opts *Options, args []string) (rc int, err error) {
if tty.IsTerminal(os.Stdin.Fd()) {
if opts.DataFile == "" && tty.IsTerminal(os.Stdin.Fd()) {
return 1, fmt.Errorf("This kitten must only be run via the command_palette action mapped to a shortcut in kitty.conf")
}
output := tui.KittenOutputSerializer()
@@ -1025,7 +1062,7 @@ func main(cmd *cli.Command, opts *Options, args []string) (rc int, err error) {
return 1, err
}
handler := &Handler{lp: lp}
handler := &Handler{lp: lp, read_only: opts.ReadOnly, data_file: opts.DataFile}
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)
lp.OnInitialize = func() (string, error) {
+9
View File
@@ -338,6 +338,15 @@ def handle_result(args: list[str], data: dict[str, Any], target_window_id: int,
help_text = 'Browse and trigger keyboard shortcuts and actions'
usage = ''
OPTIONS = r"""
--data-file
Read keybinding data from this JSON file instead of STDIN. Used by the
keyboard shortcuts help panel, so that it can run in a dock window.
--read-only
type=bool-set
Display the bindings without running the selected action on Enter. The panel
is then a reference, not a launcher.
""".format
+20
View File
@@ -74,6 +74,26 @@ func (b *testHandlerBuilder) build() *Handler {
return h
}
func TestParseData(t *testing.T) {
h := &Handler{}
data := []byte(`{"modes":{"":{"Cat":[{"key":"a","action":"x","action_display":"x","definition":"x"}]}},"mouse":[],"mode_order":[""],"category_order":{"":["Cat"]}}`)
if err := h.parseData(data); err != nil {
t.Fatal(err)
}
if len(h.all_items) != 1 {
t.Fatalf("expected 1 item, got %d", len(h.all_items))
}
if b := h.all_items[0].binding; b.Key != "a" || b.Category != "Cat" {
t.Fatalf("unexpected binding: %+v", b)
}
}
func TestParseDataRejectsEmptyInput(t *testing.T) {
if err := (&Handler{}).parseData(nil); err == nil {
t.Fatal("expected an error for empty data")
}
}
func newTestHandler() *Handler {
return newBuilder().
addBinding("", "Copy/paste", testBinding("ctrl+shift+c", "copy_to_clipboard", "Copy the selected text from the active window to the clipboard")).