diff --git a/docs/guides/tui.md b/docs/guides/tui.md index c9413853..90cfb6db 100644 --- a/docs/guides/tui.md +++ b/docs/guides/tui.md @@ -2896,6 +2896,17 @@ the reason it cannot be restricted](../assets/tui-chat-compare.png) One conversation: the finished turns above, the running turn's live output below them, and a composer at the bottom. +**On macOS, the chat's `alt+` keys need the terminal to send Option as Meta.** +The composer owns every printable key, so the chat's own commands — the diff, +rewind, the shell, the model, the chat switch, stash and history — are `alt+` +chords, and a stock Terminal.app or iTerm2 turns Option into a character +instead: Option+d types `∂`. Set Terminal's **Use Option as Meta key** +(Settings › Profiles › Keyboard) or iTerm2's Option key to **Esc+** (Profiles › +Keys). The first time the composer receives such a character for a chord the +chat binds, it says so once on its note line. Until then `ctrl+p` opens the +palette, which reaches every command, and [`tui.keys`](#rebinding-keys) +rebinds any of them. + Your own messages are easy to find in it. A prompt is drawn as a right-aligned card titled `You` — as wide as it needs to be, up to about two thirds of the pane, wrapping inside itself, with how long ago you sent it on its top edge. The @@ -2946,7 +2957,7 @@ periodic repaint. | `alt+r` | Resume a paused queue — on an idle chat the next queued message starts now, paused or not: an idle chat holding an unpaused queue (left by a daemon crash mid-recovery) says so under the queue, and this starts it | | `alt+c` | Clear the queue: delete every queued message | | `ctrl+s` | Steer the running turn. No agent can yet, so the composer says why, the footer marks it `(unavailable)` beside a running turn, and the message can only be queued (rebindable as `steer`) | -| `ctrl+j` | Insert a newline in the draft. `shift+enter` and `alt+enter` do too where the terminal sends them; `ctrl+j` works in every terminal, and `alt+enter` on macOS needs the terminal's "Option as Meta" setting | +| `ctrl+j` | Insert a newline in the draft. `shift+enter` and `alt+enter` do too where the terminal sends them; `ctrl+j` works in every terminal, and `alt+enter` on macOS needs the terminal's "Option as Meta" setting, as every `alt+` key here does | | `tab` | The skills this chat's agent can run — type to filter, `enter` or `tab` inserts one. `f2` does the same, where the terminal swallows `tab`. The same list opens by itself when you type the agent's invocation sigil | | `ctrl+x` | Stop the running turn — its process tree is killed | | `ctrl+r` | How much of the conversation to show: quiet → compact → normal → verbose | diff --git a/internal/tui/chatview.go b/internal/tui/chatview.go index d77190e8..8846a4ea 100644 --- a/internal/tui/chatview.go +++ b/internal/tui/chatview.go @@ -1540,6 +1540,10 @@ func (v *chatView) updateKey(msg tea.KeyPressMsg) (panel, tea.Cmd) { } return v, openChatExportCmd(v.chat.ID, v.chat.Title) } + if note, ok := optionKeyNote(msg.Text); ok { + // Typed all the same: it may be meant (#1061). + v.note, v.noteBad = note, false + } var cmd tea.Cmd before := v.composer.Value() v.composer, cmd = v.composer.Update(msg) diff --git a/internal/tui/optionkeys.go b/internal/tui/optionkeys.go new file mode 100644 index 00000000..a719b426 --- /dev/null +++ b/internal/tui/optionkeys.go @@ -0,0 +1,80 @@ +package tui + +import ( + "runtime" + "sync/atomic" + + "github.com/lezli01/vincent/internal/keymap" +) + +// The chat's alt chords (diff, rewind, shell, model, the chat switch, …) +// reach it only when the terminal sends Option as Meta. On a stock macOS +// Terminal.app or iTerm2 it does not: Option+d types `∂` into the composer +// and nothing says why (#1061). The limit is the terminal's, and is recorded +// at chatswitch.go; the silence was the defect. When the composer receives +// the character a US-layout Option press produces for a letter whose alt +// chord the chat binds, it says once, per run, what to set and how to reach +// the command meanwhile. +// +// Requesting keyboard enhancements would not help: Bubble Tea already +// negotiates the kitty protocol where the terminal speaks it, and whether +// Option is a modifier or a character is the terminal's own setting either +// way — Terminal.app does not speak the protocol at all. + +// optionGlyphs is what Option+letter types on the macOS US layout. Dead +// keys (e, i, n, u, `) compose with the next key and type nothing alone, +// so they are not here. +var optionGlyphs = map[rune]rune{ + 'å': 'a', '∫': 'b', 'ç': 'c', '∂': 'd', 'ƒ': 'f', '©': 'g', '˙': 'h', + '∆': 'j', '˚': 'k', '¬': 'l', 'µ': 'm', 'ø': 'o', 'π': 'p', 'œ': 'q', + '®': 'r', 'ß': 's', '†': 't', '√': 'v', '∑': 'w', '≈': 'x', '¥': 'y', + 'Ω': 'z', +} + +// optionNoted is whether this run has said it already. +var optionNoted atomic.Bool + +// optionKeyNote is the note for text typed into the composer, and false when +// there is nothing to say: not macOS, not a lone Option glyph, a letter the +// chat binds no alt chord to, or said already. +func optionKeyNote(text string) (string, bool) { + return optionKeyNoteOn(runtime.GOOS, text) +} + +func optionKeyNoteOn(goos, text string) (string, bool) { + if goos != "darwin" || optionNoted.Load() { + return "", false + } + r := []rune(text) + if len(r) != 1 { + return "", false + } + letter, ok := optionGlyphs[r[0]] + if !ok { + return "", false + } + chord := "alt+" + string(letter) + if !chatBindsKey(chord) { + return "", false + } + optionNoted.Store(true) + palette := opKey(keymap.PaletteAlt) + if palette == "" { + palette = "ctrl+p" + } + return "Option+" + string(letter) + " typed " + string(r[0]) + " — the terminal sends Option as a character, so " + + chord + " never arrives. Set Terminal's “Use Option as Meta key” or iTerm2's Option key to “Esc+”; meanwhile " + + palette + " reaches every command", true +} + +// chatBindsKey is whether any chat surface's row answers key. +func chatBindsKey(key string) bool { + for _, ctx := range []bindingContext{ctxChat, ctxChatConversation} { + for _, b := range bindingsFor(ctx) { + if b.key == key { + return true + } + } + } + return false +} diff --git a/internal/tui/optionkeys_test.go b/internal/tui/optionkeys_test.go new file mode 100644 index 00000000..189885e3 --- /dev/null +++ b/internal/tui/optionkeys_test.go @@ -0,0 +1,48 @@ +package tui + +import ( + "strings" + "testing" +) + +// #1061: a macOS Option glyph for a letter the chat binds an alt chord to +// says once what to set, and nothing else does. +func TestOptionGlyphNotesOnce(t *testing.T) { + optionNoted.Store(false) + t.Cleanup(func() { optionNoted.Store(false) }) + + if _, ok := optionKeyNoteOn("linux", "∂"); ok { + t.Fatal("a non-macOS run noted an Option glyph") + } + for _, text := range []string{"d", "é", "∂∂", ""} { + if _, ok := optionKeyNoteOn("darwin", text); ok { + t.Fatalf("%q was taken for an Option glyph", text) + } + } + note, ok := optionKeyNoteOn("darwin", "∂") + if !ok { + t.Fatal("Option+d's ∂ said nothing") + } + for _, want := range []string{"Option+d typed ∂", "alt+d", "Use Option as Meta key", "Esc+", "ctrl+p"} { + if !strings.Contains(note, want) { + t.Errorf("note misses %q: %s", want, note) + } + } + if _, ok := optionKeyNoteOn("darwin", "ß"); ok { + t.Error("the note was said twice in one run") + } +} + +// Only a letter the chat binds is worth a note: a glyph whose alt chord does +// nothing in the chat is just a character. +func TestOptionGlyphNeedsABoundChord(t *testing.T) { + optionNoted.Store(false) + t.Cleanup(func() { optionNoted.Store(false) }) + for glyph, letter := range optionGlyphs { + _, ok := optionKeyNoteOn("darwin", string(glyph)) + optionNoted.Store(false) + if ok != chatBindsKey("alt+"+string(letter)) { + t.Errorf("%c: noted %v, but alt+%c bound %v", glyph, ok, letter, !ok) + } + } +}