diff --git a/CHANGELOG.md b/CHANGELOG.md index fa4ae1bb3..a094fd120 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/src/ftxui/component/input.cpp b/src/ftxui/component/input.cpp index 63aab69a9..caef66c10 100644 --- a/src/ftxui/component/input.cpp +++ b/src/ftxui/component/input.cpp @@ -343,7 +343,8 @@ class InputBase : public ComponentBase, public InputOption { } bool HandleCharacter(const std::string& character) { - if (!insert()) { + if (!insert() && cursor_position() < (int)content->size() && + content()[cursor_position()] != '\n') { HandleDelete(); } content->insert(cursor_position(), character); diff --git a/src/ftxui/component/input_test.cpp b/src/ftxui/component/input_test.cpp index 96a63b38a..863569a2e 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