Skip to content

Commit

Permalink
Add-tests, fix-new-line-bug, add-changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Aug 28, 2023
1 parent e7db462 commit 59b12d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
3 changes: 2 additions & 1 deletion src/ftxui/component/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/ftxui/component/input_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 59b12d5

Please sign in to comment.