kazbek/cli/component.cpp

91 lines
2.6 KiB
C++

#include "component.hpp"
#include <algorithm>
namespace katja
{
ScreenContainer::ScreenContainer(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter)
: on_enter(on_enter)
{
ftxui::Components menu_pages;
std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(menu_entries),
[](const std::pair<std::string, Page>& pair) { return pair.first; });
std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_pages),
[](const std::pair<std::string, Page>& pair) { return pair.second; });
std::copy(std::cbegin(this->menu_pages), std::cend(this->menu_pages), std::back_inserter(menu_pages));
ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
this->menu = ftxui::Toggle(&this->menu_entries, &this->menu_selected);
this->content = ftxui::Container::Tab(std::move(menu_pages), &this->menu_selected);
}
ftxui::Element ScreenContainer::OnRender()
{
return ftxui::vbox({
this->menu->Render(),
ftxui::separator(),
this->content->Render()
});
}
bool ScreenContainer::OnEvent(ftxui::Event event)
{
if (event.character() == "q" && this->on_enter)
{
on_enter();
return true;
}
int previously = this->menu_selected;
bool result = menu->OnEvent(event);
if (previously != this->menu_selected)
{
this->menu_pages.at(this->menu_selected)->Load();
}
return result;
}
ftxui::Component Screen(std::vector<std::pair<std::string, Page>> pages, std::function<void()> on_enter)
{
return std::make_shared<ScreenContainer>(std::move(pages), on_enter);
}
ftxui::Element WelcomePage::OnRender()
{
return ftxui::text("Select an action in the menu.");
}
void WelcomePage::Load()
{
}
UpdatesPage::UpdatesPage(std::vector<package_identifier>&& updatable)
: updatable(std::move(updatable))
{
}
void UpdatesPage::Load()
{
}
ftxui::Element UpdatesPage::OnRender()
{
std::vector<std::shared_ptr<ftxui::Node>> lines;
for (const auto& package_identifier : this->updatable)
{
auto line = ftxui::text(package_identifier.to_string()) | color(ftxui::Color::SkyBlue2);
lines.push_back(line);
}
ftxui::Element summary = ftxui::text(" Updates (" + std::to_string(lines.size()) + ")");
return ftxui::window(summary, ftxui::vbox(lines));
}
void SearchPage::Load()
{
}
}