Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input can now use overwrite mode when toggled with insert key #735

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions include/ftxui/component/component_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [] {};
Expand Down
1 change: 1 addition & 0 deletions include/ftxui/component/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/ftxui/component/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions src/ftxui/component/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -342,10 +343,13 @@ class InputBase : public ComponentBase, public InputOption {
}

bool HandleCharacter(const std::string& character) {
mingsheng13 marked this conversation as resolved.
Show resolved Hide resolved
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;
}

Expand Down Expand Up @@ -391,7 +395,9 @@ class InputBase : public ComponentBase, public InputOption {
if (event == Event::ArrowRightCtrl) {
return HandleRightCtrl();
}

if (event == Event::Insert) {
return HandleInsert();
}
return false;
}

Expand Down Expand Up @@ -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;
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
Loading