ArthurSonzogni / FTXUI

:computer: C++ Functional Terminal User Interface. :heart:
MIT License
6.64k stars 399 forks source link

Creating an input window, and outputting entered text after hitting enter or special key. #862

Closed dr-mrsthemonarch closed 3 months ago

dr-mrsthemonarch commented 3 months ago

Hello! I'm a weekish old new user of FTXUI, which is simply amazing, All of your examples are pretty helpful to understand the library. and I'm really excited to use this for many of my future projects. But I'm seriously having problems achieving a simple task of adding a command line window/input to the program, to explain:

I'm trying to design a server to do testing with devices over a network. It will have 3 main windows, one with some buttons for commands, one for input, to send a more advanced command to, that may require too many clicks or menus, what-have-you, and an output window that will display what the server is doing at a timed interval, who may have connected to it, and some response to the sent command within the input window.

Through your examples i'm slowly hacking it all together, but I can't seem to get the syntax correct for how I catch an event like '\n' take that input typed, clear the input window, and have the output window react to it, or in my trivial testing case, simply paste the text entered, unfortunately, I can't find a single example, that does this, except for a mix between pressed_key and input, but they render live the event/text captured.

I'm also unsure how to really implement things like .on_enter, .push_back, which I feel are probably the methods I'm missing to complete the code. How would I go about doing this? Below is a snippet of what I'm doing wrong, I can post the full code if needed.

Thank you for the time and help if you can!

    auto textarea_1 = Input(&content_1);

    textarea_1 |= CatchEvent([&](Event event) {
        if (event == Event::Character('\n')) {
            //can't figure out what to do here
            return true;
        }
        return false;
    });

    auto create_right_window_elements = [&] {
      Elements elements;
      for (const auto& line : content_1) {
        elements.push_back(text(std::to_string(line)));
      }
      return elements;
    };

    auto right = Renderer([&] { return text(content_1); }); // Here this clearly just renders what is typed
ArthurSonzogni commented 3 months ago

Hello

I am not entirely sure I understood every questions. Feel free to ask more, or share the full code.

About catching the return key

I recently added:

/// @brief Return a string representation of the event.
std::string Event::DebugString() const;

This shows you have to use the key Event::Return instead of Event::Character('\n').

About creating a modal window

You can check the demo code documentation

dr-mrsthemonarch commented 3 months ago

I've managed to solve this problem with a push_back().

    input_option.on_enter = [&] {
        input_entries.push_back(input_add_content);
        input_add_content = "";
    };
image
dr-mrsthemonarch commented 3 months ago

thanks for the help