mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-18 17:18:08 +08:00
Feature: input can now use overwrite mode when toggled with insert key (#735)
Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
@@ -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;
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user