Could I use a Vector ADT class or a string array for creating a menu? #934
-
Hi @ArthurSonzogni, I hope this message finds you well. I’m currently using your FTXUI library to develop a terminal UI for my project at university (Project By Learning 2), which strictly requires the use of C++ and abstract data types (ADTs) for the terminal interface. I noticed that the Menu only accepts Thank you very much for your assistance. I appreciate your help! Best regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @antialberteinstein
Cool! I would love to see the results ;-) Feel free to share it when you are done.
You have two options:
auto menu = Menu({
.entries = Adapter::From(&entries),
.selected = &selected,
}); Full example:// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <functional> // for function
#include <iostream> // for basic_ostream::operator<<, operator<<, endl, basic_ostream, basic_ostream<>::__ostream_type, cout, ostream
#include <string> // for string, basic_string, allocator
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu
#include "ftxui/component/component_options.hpp" // for MenuOption
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
// Bring your own adapter:
class Adapter : public ftxui::ConstStringListRef::Adapter {
public:
static std::unique_ptr<Adapter> From(std::vector<std::string>* entries) {
return std::make_unique<Adapter>(entries);
}
explicit Adapter(std::vector<std::string>* entries) : entries_(entries) {}
~Adapter() final = default;
size_t size() const final { return entries_->size(); }
std::string operator[](size_t i) const final { return entries_->at(i); }
private:
std::vector<std::string>* entries_;
};
int main() {
using namespace ftxui;
auto screen = ScreenInteractive::TerminalOutput();
std::vector<std::string> entries = {
"entry 1",
"entry 2",
"entry 3",
};
int selected = 0;
MenuOption menu_option = {
.entries = Adapter::From(&entries),
.selected = &selected,
};
auto menu = Menu(menu_option);
screen.Loop(menu);
std::cout << "Selected element = " << selected << std::endl;
} |
Beta Was this translation helpful? Give feedback.
Hello @antialberteinstein
Cool! I would love to see the results ;-) Feel free to share it when you are done.
You have two options:
Menu
is implemented using public APIs, so you should be able to copy-paste the implementation to make it your own.ConstStringListRef::Adapter
, and pass it to theMenuOption::entries
.Full example:
// Copyright 2020 Arthur So…