From 6adca1656480fb755a6b78c239732603c7e907ba Mon Sep 17 00:00:00 2001 From: sail3r <82087684+sail3r@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:30:02 +1300 Subject: [PATCH] text inputs: Delete, Home and End in prompt, login and settings add maya's InputParser already decodes Delete (CSI 3~, KKP 57365) and Home/End (CSI H/F, SS3 H/F, CSI 1~/4~), and the forms and the ^R thread rename consume them. Three caret-based text inputs did not, so the key reached the router, matched no arm, and was silently dropped: - Main prompt (on_composer): no Delete arm. New ComposerDeleteForward erases from the caret to ui::chip_next(), the forward mirror of ComposerBackspace's chip_prev, so Delete in front of an attachment placeholder removes the whole chip in one stroke. The caret stays put; begin_edit() makes it one undo unit, as Backspace does. - Login text fields (OAuth code, API key, custom host): no Delete, Home or End arms. New LoginDelete / LoginCursorHome / LoginCursorEnd. The three states differ only in the name of their text member, so a local with_login_field() visits them once and hands (text, caret) to each operation; the existing Backspace/Left/Right reducers are left as they were. - Settings list one-line add prompt (n, or a on Commands/Agents): kept a byte caret and rendered it with with_caret, but routed only printables and Backspace through the shared nav grammar, so there was no way to move the caret, and Home/End fell through to the list's jump verbs (a no-op while adding). New SettingsListDelete and SettingsListCursor{Left,Right,Home,End}, routed through NavSpec::extra in the same shape as the thread-list rename. All deletes step by whole UTF-8 code points (utf8_next), and are no-ops at the end of the text. Tests (key_routing_test.cpp) drive the real AgenttyApp::on -> update path for each surface, including a 2-byte code point and, for the prompt, an attachment chip. --- include/agentty/runtime/msg.hpp | 17 +++++- src/runtime/app/subscribe.cpp | 17 ++++++ src/runtime/app/update/composer.cpp | 11 ++++ src/runtime/app/update/login.cpp | 37 +++++++++++ src/runtime/app/update/settings_list.cpp | 22 +++++++ tests/key_routing_test.cpp | 78 ++++++++++++++++++++++++ 6 files changed, 179 insertions(+), 3 deletions(-) diff --git a/include/agentty/runtime/msg.hpp b/include/agentty/runtime/msg.hpp index a889471a..dcdf71e5 100644 --- a/include/agentty/runtime/msg.hpp +++ b/include/agentty/runtime/msg.hpp @@ -57,6 +57,9 @@ namespace agentty { // ── Composer ───────────────────────────────────────────────────────────── struct ComposerCharInput { char32_t ch; }; struct ComposerBackspace {}; +// Forward Delete: removes the code point (or whole attachment chip) after +// the caret; the caret stays put. +struct ComposerDeleteForward {}; struct ComposerEnter {}; struct ComposerNewline {}; struct ComposerSubmit {}; @@ -988,9 +991,12 @@ struct AccountRemove {}; struct LoginPickMethod { char32_t key; }; // '1' = ApiKey, '2' = OAuth, '3' = ChatGPT struct LoginCharInput { char32_t ch; }; struct LoginBackspace {}; +struct LoginDelete {}; struct LoginPaste { std::string text; }; struct LoginCursorLeft {}; struct LoginCursorRight {}; +struct LoginCursorHome {}; +struct LoginCursorEnd {}; struct LoginSubmit {}; // User pressed the "copy URL to clipboard" key while the OAuthCode // modal is up. The reducer issues a cmd::write_clipboard with @@ -1225,6 +1231,9 @@ struct PluginEdited { bool parse_error = false; }; struct SettingsListBackspace {}; +// Add prompt caret editing: forward Delete, and caret moves. +struct SettingsListDelete {}; +struct SettingsListCursor { enum class To { Left, Right, Home, End }; To to; }; // `p` on a plugin's tool row: pin it (always loaded) or unpin it (#98). struct SettingsListPin {}; // → / ← on Plugins: show or hide a server's tools. @@ -1435,7 +1444,7 @@ struct GitSignalsRefreshed {}; namespace msg { using ComposerMsg = std::variant< - ComposerCharInput, ComposerBackspace, ComposerEnter, ComposerNewline, + ComposerCharInput, ComposerBackspace, ComposerDeleteForward, ComposerEnter, ComposerNewline, ComposerSubmit, ComposerToggleExpand, ComposerToggleLoop, ComposerCursorLeft, ComposerCursorRight, ComposerCursorHome, ComposerCursorEnd, ComposerSlashMove, ComposerSlashComplete, ComposerSlashDismiss, SlashPromptRendered, @@ -1546,6 +1555,7 @@ using SettingsListMsg = std::variant< SettingsListEditOpen, SettingsListChar, SettingsListPaste, PluginsUpdated, PluginEdited, SettingsListBackspace, SettingsListSubmitInput, SettingsListCancelInput, + SettingsListDelete, SettingsListCursor, SettingsAddDone>; using ForkMsg = std::variant< @@ -1557,8 +1567,9 @@ using TodoMsg = std::variant< using LoginMsg = std::variant< OpenLogin, CloseLogin, LoginBack, SignOut, OpenAccounts, AccountMove, AccountSelect, AccountRemove, - LoginPickMethod, LoginCharInput, LoginBackspace, - LoginPaste, LoginCursorLeft, LoginCursorRight, LoginSubmit, + LoginPickMethod, LoginCharInput, LoginBackspace, LoginDelete, + LoginPaste, LoginCursorLeft, LoginCursorRight, + LoginCursorHome, LoginCursorEnd, LoginSubmit, LoginCopyAuthUrl, LoginCopyCode, LoginOpenBrowserAgain, LoginExchanged, LoginOAuthMinted, CodexDeviceCodeReady, CodexLoginDone, DeviceCodeReady, DeviceLoginDone, TokenRefreshed, OAuthRefreshNotDue, diff --git a/src/runtime/app/subscribe.cpp b/src/runtime/app/subscribe.cpp index 1f8c8aab..d057dd16 100644 --- a/src/runtime/app/subscribe.cpp +++ b/src/runtime/app/subscribe.cpp @@ -388,6 +388,19 @@ std::optional on_settings_list(const KeyEvent& ev, bool input_active, s.select = [] { return Msg{SettingsListSubmitInput{}}; }; s.filter_bs = [] { return Msg{SettingsListBackspace{}}; }; s.filter_ch = [](char32_t c) { return Msg{SettingsListChar{c}}; }; + s.extra = [](const KeyEvent& e) -> std::optional { + const auto* sk = std::get_if(&e.key); + if (!sk) return std::nullopt; + using To = SettingsListCursor::To; + switch (*sk) { + case SpecialKey::Left: return Msg{SettingsListCursor{To::Left}}; + case SpecialKey::Right: return Msg{SettingsListCursor{To::Right}}; + case SpecialKey::Home: return Msg{SettingsListCursor{To::Home}}; + case SpecialKey::End: return Msg{SettingsListCursor{To::End}}; + case SpecialKey::Delete: return Msg{SettingsListDelete{}}; + default: return std::nullopt; + } + }; return nav::translate(s, ev); } if (hub) { @@ -962,8 +975,11 @@ std::optional on_login(const ui::login::State& state, const KeyEvent& ev) { switch (std::get(ev.key)) { case SpecialKey::Enter: return LoginSubmit{}; case SpecialKey::Backspace: return LoginBackspace{}; + case SpecialKey::Delete: return LoginDelete{}; case SpecialKey::Left: return LoginCursorLeft{}; case SpecialKey::Right: return LoginCursorRight{}; + case SpecialKey::Home: return LoginCursorHome{}; + case SpecialKey::End: return LoginCursorEnd{}; default: return std::nullopt; } } @@ -1133,6 +1149,7 @@ std::optional on_composer(ComposerKeyState s, const KeyEvent& ev) { if (ev.mods.alt && s.text_empty && s.has_queued && !s.in_queue_peek) return Msg{ComposerQueuePopLast{}}; return ComposerBackspace{}; + case SpecialKey::Delete: return ComposerDeleteForward{}; case SpecialKey::Left: // Ctrl+Left = jump-by-word. Mirrors readline / every // text editor. Plain Left is per-character (chip-aware). diff --git a/src/runtime/app/update/composer.cpp b/src/runtime/app/update/composer.cpp index 582803e3..d882616f 100644 --- a/src/runtime/app/update/composer.cpp +++ b/src/runtime/app/update/composer.cpp @@ -554,6 +554,17 @@ Cmd composer_update(Model& m, msg::ComposerMsg cm) { } return Cmd::none(); }, + [&](ComposerDeleteForward) -> Cmd { + // Mirror of Backspace: chip_next spans a whole placeholder, so + // Delete in front of an attachment removes the chip in one key. + const int n = static_cast(m.ui.composer.text.size()); + if (m.ui.composer.cursor < n) { + begin_edit(m.ui.composer); + const int q = ui::chip_next(m.ui.composer.text, m.ui.composer.cursor); + m.ui.composer.text.erase(m.ui.composer.cursor, q - m.ui.composer.cursor); + } + return Cmd::none(); + }, [&](ComposerEnter) { return submit_message(m); }, [&](ComposerSubmit) { return submit_message(m); }, [&](ComposerNewline) -> Cmd { diff --git a/src/runtime/app/update/login.cpp b/src/runtime/app/update/login.cpp index f7cad036..4088d494 100644 --- a/src/runtime/app/update/login.cpp +++ b/src/runtime/app/update/login.cpp @@ -906,6 +906,40 @@ Cmd login_paste(Model& m, std::string text) { return Cmd::none(); } +// Every editable login field: (text, caret). Lets Delete/Home/End share one +// body instead of repeating the three-state visit per key. +template +void with_login_field(Model& m, F&& f) { + std::visit(overload{ + [&](login::OAuthCode& s) { f(s.code_input, s.cursor); }, + [&](login::ApiKeyInput& s) { f(s.key_input, s.cursor); }, + [&](login::CustomHostInput& s) { f(s.host_input, s.cursor); }, + [](auto&) {}, + }, m.ui.login); +} + +Cmd login_delete(Model& m) { + with_login_field(m, [](std::string& text, int& cursor) { + if (cursor >= static_cast(text.size())) return; + const int q = ui::utf8_next(text, cursor); + text.erase(static_cast(cursor), + static_cast(q - cursor)); + }); + return Cmd::none(); +} + +Cmd login_cursor_home(Model& m) { + with_login_field(m, [](std::string&, int& cursor) { cursor = 0; }); + return Cmd::none(); +} + +Cmd login_cursor_end(Model& m) { + with_login_field(m, [](std::string& text, int& cursor) { + cursor = static_cast(text.size()); + }); + return Cmd::none(); +} + Cmd login_cursor_left(Model& m) { std::visit(overload{ [](login::OAuthCode& s) { @@ -1390,6 +1424,9 @@ Cmd login_update(Model& m, msg::LoginMsg lm) { [&](LoginPaste& e) -> Cmd { return login_paste(m, std::move(e.text)); }, [&](LoginCursorLeft) -> Cmd { return login_cursor_left(m); }, [&](LoginCursorRight) -> Cmd { return login_cursor_right(m); }, + [&](LoginDelete) -> Cmd { return login_delete(m); }, + [&](LoginCursorHome) -> Cmd { return login_cursor_home(m); }, + [&](LoginCursorEnd) -> Cmd { return login_cursor_end(m); }, [&](LoginSubmit) -> Cmd { return login_submit(m); }, [&](LoginCopyAuthUrl) -> Cmd { return login_copy_auth_url(m); }, [&](LoginCopyCode) -> Cmd { return login_copy_code(m); }, diff --git a/src/runtime/app/update/settings_list.cpp b/src/runtime/app/update/settings_list.cpp index e2b64fe8..223f8c05 100644 --- a/src/runtime/app/update/settings_list.cpp +++ b/src/runtime/app/update/settings_list.cpp @@ -650,6 +650,28 @@ Cmd settings_list_update(Model& m, msg::SettingsListMsg sm) { o->cursor = p; return Cmd::none(); }, + [&](SettingsListDelete) -> Cmd { + auto* o = m.ui.panel.get(); + if (!o || !o->input_active + || o->cursor >= static_cast(o->input.size())) + return Cmd::none(); + const int q = ui::utf8_next(o->input, o->cursor); + o->input.erase(static_cast(o->cursor), + static_cast(q - o->cursor)); + return Cmd::none(); + }, + [&](SettingsListCursor& e) -> Cmd { + auto* o = m.ui.panel.get(); + if (!o || !o->input_active) return Cmd::none(); + using To = SettingsListCursor::To; + switch (e.to) { + case To::Left: o->cursor = ui::utf8_prev(o->input, o->cursor); break; + case To::Right: o->cursor = ui::utf8_next(o->input, o->cursor); break; + case To::Home: o->cursor = 0; break; + case To::End: o->cursor = static_cast(o->input.size()); break; + } + return Cmd::none(); + }, [&](SettingsListCancelInput) -> Cmd { auto* o = m.ui.panel.get(); if (!o) return Cmd::none(); diff --git a/tests/key_routing_test.cpp b/tests/key_routing_test.cpp index 390ee8bd..2b25b5b9 100644 --- a/tests/key_routing_test.cpp +++ b/tests/key_routing_test.cpp @@ -22,6 +22,8 @@ #include "agentty/runtime/app/update/internal.hpp" #include "agentty/runtime/app/subscribe.hpp" #include "agentty/runtime/panel/appearance.hpp" +#include "agentty/runtime/panel/settings/categories.hpp" +#include "agentty/runtime/composer_attachment.hpp" #include "agentty/runtime/app/deps.hpp" using namespace agentty; @@ -190,3 +192,79 @@ TEST_CASE("routing: every way ^/ arrives opens the model picker") { CHECK(m.ui.panel.get() != nullptr); } } + +// The caret-based text inputs must all answer Delete (forward erase) and +// Home/End, through the real router, not only the reducer. +TEST_CASE("routing: Delete in the main prompt erases forward, a chip whole") { + install_stub_deps(); + Model m; + m.ui.composer.text = "caf\xc3\xa9!"; // 2-byte code point before '!' + m.ui.composer.cursor = 3; + REQUIRE(press(m, special(maya::SpecialKey::Delete))); + CHECK(m.ui.composer.text == "caf!"); + CHECK(m.ui.composer.cursor == 3); + REQUIRE(press(m, special(maya::SpecialKey::End))); + REQUIRE(press(m, special(maya::SpecialKey::Delete))); // at end: no-op + CHECK(m.ui.composer.text == "caf!"); + + Model c; + Attachment att; + att.kind = Attachment::Kind::Paste; + att.body.set_bytes("x"); + c.ui.composer.attachments.push_back(std::move(att)); + c.ui.composer.text = "a" + attachment::make_placeholder(0) + "b"; + c.ui.composer.cursor = 1; + REQUIRE(press(c, special(maya::SpecialKey::Delete))); + CHECK(c.ui.composer.text == "ab"); + CHECK(c.ui.composer.cursor == 1); +} + +TEST_CASE("routing: login text fields answer Delete, Home and End") { + install_stub_deps(); + Model m; + m.ui.login = ui::login::ApiKeyInput{.key_input = "sk-\xc3\xa9x", .cursor = 0}; + const auto field = [&] { return std::get(m.ui.login); }; + REQUIRE(press(m, special(maya::SpecialKey::End))); + CHECK(field().cursor == 6); + REQUIRE(press(m, special(maya::SpecialKey::Home))); + CHECK(field().cursor == 0); + REQUIRE(press(m, special(maya::SpecialKey::Right))); + REQUIRE(press(m, special(maya::SpecialKey::Right))); + REQUIRE(press(m, special(maya::SpecialKey::Right))); + REQUIRE(press(m, special(maya::SpecialKey::Delete))); // the whole e-acute + CHECK(field().key_input == "sk-x"); + CHECK(field().cursor == 3); + REQUIRE(press(m, special(maya::SpecialKey::End))); + REQUIRE(press(m, special(maya::SpecialKey::Delete))); // at end: no-op + CHECK(field().key_input == "sk-x"); +} + +TEST_CASE("routing: the settings add prompt answers Delete, arrows, Home and End") { + install_stub_deps(); + Model m = app::update(Model{}, Msg{OpenSettingsList{settings::Category::Plugins}}).first; + // `n` is the one-line add prompt (`a` on Plugins opens the add form). + maya::KeyEvent a; + a.key = maya::CharKey{U'n'}; + REQUIRE(press(m, a)); + const auto list = [&] { return m.ui.panel.get(); }; + REQUIRE(list() != nullptr); + REQUIRE(list()->input_active); + for (char32_t c : std::u32string{U"abc"}) { + maya::KeyEvent ev; + ev.key = maya::CharKey{c}; + REQUIRE(press(m, ev)); + } + REQUIRE(press(m, special(maya::SpecialKey::Home))); + CHECK(list()->cursor == 0); + REQUIRE(press(m, special(maya::SpecialKey::Delete))); + CHECK(list()->input == "bc"); + REQUIRE(press(m, special(maya::SpecialKey::Right))); + CHECK(list()->cursor == 1); + REQUIRE(press(m, special(maya::SpecialKey::Left))); + CHECK(list()->cursor == 0); + REQUIRE(press(m, special(maya::SpecialKey::End))); + CHECK(list()->cursor == 2); + REQUIRE(press(m, special(maya::SpecialKey::Delete))); // at end: no-op + CHECK(list()->input == "bc"); + CHECK(list()->input_active); +}