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

Ip input component #737

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ add_library(component
src/ftxui/component/event.cpp
src/ftxui/component/hoverable.cpp
src/ftxui/component/input.cpp
src/ftxui/component/ip_input.cpp
src/ftxui/component/loop.cpp
src/ftxui/component/maybe.cpp
src/ftxui/component/menu.cpp
Expand Down
2 changes: 1 addition & 1 deletion examples/component/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main() {
std::string password;

Component input_first_name = Input(&first_name, "first name");
Component input_last_name = Input(&last_name, "last name");
Component input_last_name = IpInput(&last_name);

InputOption password_option;
password_option.password = true;
Expand Down
2 changes: 2 additions & 0 deletions include/ftxui/component/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Component Input(StringRef content,
StringRef placeholder,
InputOption options = {});

Component IpInput(StringRef content);

Component Menu(MenuOption options);
Component Menu(ConstStringListRef entries,
int* selected_,
Expand Down
113 changes: 113 additions & 0 deletions src/ftxui/component/ip_input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <sstream>
#include <vector>

#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/event.hpp"
#include "ftxui/screen/util.hpp"

namespace ftxui {

bool isNumber(const std::string& character) {

Check failure on line 11 in src/ftxui/component/ip_input.cpp

View workflow job for this annotation

GitHub Actions / Tests (Linux GCC, ubuntu-latest, gcc, gcov)

no previous declaration for ‘bool ftxui::isNumber(const std::string&)’ [-Werror=missing-declarations]
const char* charArr = character.c_str();
while (*charArr != 0) {
if (*charArr < '0' || *charArr > '9') {
return false;
}
charArr++; // NOLINT
}
return true;
}

std::vector<std::string> split(const std::string& content) {

Check failure on line 22 in src/ftxui/component/ip_input.cpp

View workflow job for this annotation

GitHub Actions / Tests (Linux GCC, ubuntu-latest, gcc, gcov)

no previous declaration for ‘std::vector<std::__cxx11::basic_string<char> > ftxui::split(const std::string&)’ [-Werror=missing-declarations]
std::vector<std::string> output;
std::stringstream ss(content);
std::string line;
while (std::getline(ss, line, '.')) {
output.push_back(line);
}
return output;
}

Component IpInput(StringRef content) {
class Impl : public ComponentBase {
public:
// NOLINTNEXTLINE
Impl(StringRef content) : content_(std::move(content)) {
input_ = Input(content_, "___.___.___.___");
Add(input_);
}

Element Render() override {
return vbox({
input_->Render()
});
}

bool OnEvent(Event event) override {
if (event.is_character()) {
std::vector<std::string> parts = split(*content_);
parts.reserve(4);

//ipv4 maximum length
if (parts[3].size() == 3) {
return false;
}

// when user input '.' manually
if (event.character() == ".") {
if (dotsCounter >= 3) {
return false;
}
if (content_->back() == '.') {
return false;
}
if (content_->empty()) {
return false;
}
dots[dotsCounter++] = true;
return ComponentBase::OnEvent(event);
}

for (int i = 0; i < 3; ++i) {
if (parts[i].size() == 3 && !dots[i]) {
if (event.character() != ".") {
return false;
}
}
// add '.' automatically after every 3 character
if (parts[i].size() == 2 && !dots[i]) {
dots[dotsCounter++] = true;
ComponentBase::OnEvent(event);
return ComponentBase::OnEvent(Event::Character('.'));
}
}
if (!isNumber(event.character())) {
return false;
}
}

if (event == Event::Backspace) {
if (content_->back() == '.') {
dots[--dotsCounter] = false;
util::clamp(dotsCounter, 0, 3);
return ComponentBase::OnEvent(event);
}
}
return ComponentBase::OnEvent(event);
}

bool Focusable() const override
{
return true;
}
std::vector<bool> dots = {false, false, false};
int dotsCounter = 0;
StringRef content_;
Component input_;
};

return Make<Impl>(content);
}

} //namespace ftxui
Loading