mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-17 08:28:09 +08:00
Merge remote-tracking branch 'origin' into feature/mouse-support
This commit is contained in:
@@ -81,6 +81,11 @@ const Event Event::F9 = Event::Special("\x1B[20~");
|
||||
const Event Event::F10 = Event::Special("\x1B[21~");
|
||||
const Event Event::F11 = Event::Special("\x1B[21~"); // Doesn't exist
|
||||
const Event Event::F12 = Event::Special("\x1B[24~");
|
||||
const Event Event::Home = Event::Special({27, 91, 72});
|
||||
const Event Event::End = Event::Special({27, 91, 70});
|
||||
const Event Event::PageUp = Event::Special({27, 91, 53, 126});
|
||||
const Event Event::PageDown = Event::Special({27, 91, 54, 126});
|
||||
|
||||
Event Event::Custom = Event::Special({0});
|
||||
|
||||
} // namespace ftxui
|
||||
|
@@ -94,6 +94,16 @@ bool Input::OnEvent(Event event) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event == Event::Home) {
|
||||
cursor_position = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event == Event::End) {
|
||||
cursor_position = (int)content.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Content
|
||||
if (event.is_character()) {
|
||||
content.insert(cursor_position, 1, event.character());
|
||||
|
159
src/ftxui/component/input_test.cpp
Normal file
159
src/ftxui/component/input_test.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
#include "ftxui/component/input.hpp"
|
||||
#include "ftxui/component/event.hpp"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
TEST(InputTest, Init) {
|
||||
Input input;
|
||||
|
||||
EXPECT_EQ(input.content, L"");
|
||||
EXPECT_EQ(input.placeholder, L"");
|
||||
EXPECT_EQ(input.cursor_position, 0);
|
||||
}
|
||||
|
||||
TEST(InputTest, Type) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
EXPECT_EQ(input.content, L"a");
|
||||
EXPECT_EQ(input.cursor_position, 1u);
|
||||
|
||||
input.OnEvent(Event::Character('b'));
|
||||
EXPECT_EQ(input.content, L"ab");
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
}
|
||||
|
||||
TEST(InputTest, Arrow) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
input.OnEvent(Event::Character('b'));
|
||||
input.OnEvent(Event::Character('c'));
|
||||
|
||||
EXPECT_EQ(input.cursor_position, 3u);
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
EXPECT_EQ(input.cursor_position, 1u);
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
EXPECT_EQ(input.cursor_position, 0u);
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
EXPECT_EQ(input.cursor_position, 0u);
|
||||
|
||||
input.OnEvent(Event::ArrowRight);
|
||||
EXPECT_EQ(input.cursor_position, 1u);
|
||||
|
||||
input.OnEvent(Event::ArrowRight);
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
|
||||
input.OnEvent(Event::ArrowRight);
|
||||
EXPECT_EQ(input.cursor_position, 3u);
|
||||
|
||||
input.OnEvent(Event::ArrowRight);
|
||||
EXPECT_EQ(input.cursor_position, 3u);
|
||||
}
|
||||
|
||||
TEST(InputTest, Insert) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
input.OnEvent(Event::Character('b'));
|
||||
input.OnEvent(Event::Character('c'));
|
||||
EXPECT_EQ(input.content, L"abc");
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(input.content, L"a-bc");
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(input.content, L"a--bc");
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(input.content, L"-a--bc");
|
||||
}
|
||||
|
||||
TEST(InputTest, Home) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
input.OnEvent(Event::Character('b'));
|
||||
input.OnEvent(Event::Character('c'));
|
||||
EXPECT_EQ(input.content, L"abc");
|
||||
|
||||
EXPECT_EQ(input.cursor_position, 3u);
|
||||
input.OnEvent(Event::Home);
|
||||
EXPECT_EQ(input.cursor_position, 0u);
|
||||
|
||||
input.OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(input.content, L"-abc");
|
||||
}
|
||||
|
||||
TEST(InputTest, End) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
input.OnEvent(Event::Character('b'));
|
||||
input.OnEvent(Event::Character('c'));
|
||||
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
|
||||
EXPECT_EQ(input.cursor_position, 1u);
|
||||
input.OnEvent(Event::End);
|
||||
EXPECT_EQ(input.cursor_position, 3u);
|
||||
}
|
||||
|
||||
TEST(InputTest, Delete) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
input.OnEvent(Event::Character('b'));
|
||||
input.OnEvent(Event::Character('c'));
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
|
||||
EXPECT_EQ(input.content, L"abc");
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
|
||||
input.OnEvent(Event::Delete);
|
||||
EXPECT_EQ(input.content, L"ab");
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
|
||||
input.OnEvent(Event::Delete);
|
||||
EXPECT_EQ(input.content, L"ab");
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
}
|
||||
|
||||
TEST(InputTest, Backspace) {
|
||||
Input input;
|
||||
|
||||
input.OnEvent(Event::Character('a'));
|
||||
input.OnEvent(Event::Character('b'));
|
||||
input.OnEvent(Event::Character('c'));
|
||||
input.OnEvent(Event::ArrowLeft);
|
||||
|
||||
EXPECT_EQ(input.content, L"abc");
|
||||
EXPECT_EQ(input.cursor_position, 2u);
|
||||
|
||||
input.OnEvent(Event::Backspace);
|
||||
EXPECT_EQ(input.content, L"ac");
|
||||
EXPECT_EQ(input.cursor_position, 1u);
|
||||
|
||||
input.OnEvent(Event::Backspace);
|
||||
EXPECT_EQ(input.content, L"c");
|
||||
EXPECT_EQ(input.cursor_position, 0u);
|
||||
|
||||
input.OnEvent(Event::Backspace);
|
||||
EXPECT_EQ(input.content, L"c");
|
||||
EXPECT_EQ(input.cursor_position, 0u);
|
||||
}
|
@@ -110,7 +110,7 @@ void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
|
||||
|
||||
char c;
|
||||
while (!*quit) {
|
||||
while (read(STDIN_FILENO, &c, 1), c)
|
||||
while(read(STDIN_FILENO, &c, 1), c)
|
||||
parser.Add(c);
|
||||
|
||||
emscripten_sleep(1);
|
||||
|
@@ -5,7 +5,11 @@
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#include <Windows.h>
|
||||
#else
|
||||
#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
|
||||
|
Reference in New Issue
Block a user