diff --git a/CHANGELOG.md b/CHANGELOG.md index fa4ae1bb3076a91922f2c89fb489bf13bd68e256..a094fd120038e325ce5da8c60e515196c2daeb4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Changelog current (development) --------------------- +### Component +- Feature: Add support for `Input`'s insert mode. Add `InputOption::insert` + option. Added by @mingsheng13. + 5.0.0 ----- diff --git a/include/ftxui/component/component_options.hpp b/include/ftxui/component/component_options.hpp index b401b6d1fa67ce91bc56afee2d7abaed4d3a28b6..c437bf05fc4377a785c8037c6f3ed2e6db378d78 100644 --- a/include/ftxui/component/component_options.hpp +++ b/include/ftxui/component/component_options.hpp @@ -177,6 +177,7 @@ struct InputOption { std::function<Element(InputState)> transform; Ref<bool> password = false; /// < Obscure the input content using '*'. Ref<bool> multiline = true; /// < Whether the input can be multiline. + Ref<bool> insert = true; /// < Insert or overtype character mode. /// Called when the content changes. std::function<void()> on_change = [] {}; diff --git a/include/ftxui/component/event.hpp b/include/ftxui/component/event.hpp index 504bee093a8b217f242ca49d97d00731f36183f9..b48162259f8a7d6b2c3e1075eb95cb3bcec459e0 100644 --- a/include/ftxui/component/event.hpp +++ b/include/ftxui/component/event.hpp @@ -55,6 +55,7 @@ struct Event { static const Event TabReverse; static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12; + static const Event Insert; static const Event Home; static const Event End; diff --git a/src/ftxui/component/event.cpp b/src/ftxui/component/event.cpp index b6530746a0ddef2f74ecbe53b93acd632f1475cb..72a57f135e3412d345011a757bb21ccca1b25830 100644 --- a/src/ftxui/component/event.cpp +++ b/src/ftxui/component/event.cpp @@ -100,6 +100,7 @@ const Event Event::F10 = Event::Special("\x1B[21~"); // NOLINT const Event Event::F11 = Event::Special("\x1B[23~"); // NOLINT const Event Event::F12 = Event::Special("\x1B[24~"); // NOLINT +const Event Event::Insert = Event::Special("\x1B[2~"); // NOLINT const Event Event::Home = Event::Special({27, 91, 72}); // NOLINT const Event Event::End = Event::Special({27, 91, 70}); // NOLINT const Event Event::PageUp = Event::Special({27, 91, 53, 126}); // NOLINT diff --git a/src/ftxui/component/input.cpp b/src/ftxui/component/input.cpp index 12122146775ec6b294872ee79c750ff64d75f8ac..caef66c1039789a34333e7fbd14640a810c4de11 100644 --- a/src/ftxui/component/input.cpp +++ b/src/ftxui/component/input.cpp @@ -100,8 +100,9 @@ class InputBase : public ComponentBase, public InputOption { // Component implementation: Element Render() override { const bool is_focused = Focused(); - const auto focused = - (is_focused || hovered_) ? focusCursorBarBlinking : select; + const auto focused = (!is_focused && !hovered_) ? select + : insert() ? focusCursorBarBlinking + : focusCursorBlockBlinking; auto transform_func = transform ? transform : InputOption::Default().transform; @@ -342,10 +343,13 @@ class InputBase : public ComponentBase, public InputOption { } bool HandleCharacter(const std::string& character) { + if (!insert() && cursor_position() < (int)content->size() && + content()[cursor_position()] != '\n') { + HandleDelete(); + } content->insert(cursor_position(), character); cursor_position() += character.size(); on_change(); - return true; } @@ -391,7 +395,9 @@ class InputBase : public ComponentBase, public InputOption { if (event == Event::ArrowRightCtrl) { return HandleRightCtrl(); } - + if (event == Event::Insert) { + return HandleInsert(); + } return false; } @@ -509,6 +515,11 @@ class InputBase : public ComponentBase, public InputOption { return true; } + bool HandleInsert() { + insert() = !insert(); + return true; + } + bool Focusable() const final { return true; } bool hovered_ = false; diff --git a/src/ftxui/component/input_test.cpp b/src/ftxui/component/input_test.cpp index 96a63b38a692632c8d1c0755c43e15626d00870a..863569a2e64e9e365d4efdea7a79fbeb400fcad0 100644 --- a/src/ftxui/component/input_test.cpp +++ b/src/ftxui/component/input_test.cpp @@ -754,4 +754,32 @@ TEST(InputTest, OnEnter) { EXPECT_TRUE(on_enter_called); } +TEST(InputTest, InsertMode) { + std::string content = "abc\nefg"; + bool insert = true; + int cursor_position = 1; + Component input = Input({ + .content = &content, + .insert = &insert, + .cursor_position = &cursor_position, + }); + + EXPECT_TRUE(insert); + EXPECT_TRUE(input->OnEvent(Event::Insert)); + EXPECT_FALSE(insert); + + EXPECT_EQ(content, "abc\nefg"); + EXPECT_TRUE(input->OnEvent(Event::Character('x'))); + EXPECT_EQ(content, "axc\nefg"); + EXPECT_TRUE(input->OnEvent(Event::Character('y'))); + EXPECT_EQ(content, "axy\nefg"); + EXPECT_TRUE(input->OnEvent(Event::Character('z'))); + EXPECT_EQ(content, "axyz\nefg"); + + EXPECT_TRUE(input->OnEvent(Event::ArrowDown)); + EXPECT_EQ(content, "axyz\nefg"); + EXPECT_TRUE(input->OnEvent(Event::Character('X'))); + EXPECT_EQ(content, "axyz\nefgX"); +} + } // namespace ftxui