From 5a4c882d409e4051001cdd64cbcef9a98fc2c6cf Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 11 Apr 2025 21:45:05 +0200 Subject: [PATCH] Katja: Allow the search by name in the TUI --- cli/component.cpp | 47 +++++++++++++++++++++++++++++++++++++++++------ cli/component.hpp | 9 ++++++++- cli/main.cpp | 2 +- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/cli/component.cpp b/cli/component.cpp index db29052..ef93e87 100644 --- a/cli/component.cpp +++ b/cli/component.cpp @@ -66,7 +66,7 @@ namespace katja ftxui::Element WelcomePage::OnRender() { - return ftxui::text("Select an action in the menu."); + return ftxui::paragraph("Select an action in the menu."); } void WelcomePage::Load() @@ -85,7 +85,7 @@ namespace katja ftxui::Element UpdatesPage::OnRender() { - std::vector> lines; + std::vector lines; for (const auto& package_identifier : this->updatable) { @@ -97,21 +97,56 @@ namespace katja return ftxui::window(summary, ftxui::vbox(lines)); } + SearchPage::SearchPage(std::shared_ptr repository, const std::string& architecture) + : repository(repository), architecture(architecture) + { + ftxui::InputOption search_input_option = { .multiline = false }; + this->search_input = ftxui::Input(&this->needle, "Search", search_input_option); + this->type_input = ftxui::Radiobox(std::vector{ "Names", "Description" }, &this->search_type); + } + void SearchPage::Load() { + this->needle.erase(); } ftxui::Element SearchPage::OnRender() { + std::vector lines; + + for (const auto& package_identifier : this->search_results) + { + auto line = ftxui::text(package_identifier.to_string()) | color(ftxui::Color::SkyBlue2); + lines.push_back(line); + } + ftxui::FlexboxConfig config; + config.justify_content = ftxui::FlexboxConfig::JustifyContent::FlexStart; + config.align_items = ftxui::FlexboxConfig::AlignItems::FlexStart; + config.direction = ftxui::FlexboxConfig::Direction::Row; + return ftxui::vbox({ - ftxui::hbox({ - this->search_input->Render() - }) + ftxui::flexbox({ + this->search_input->Render(), + ftxui::window(ftxui::text("Search in"), type_input->Render()) + }, config), + ftxui::vbox(lines) }); } bool SearchPage::OnEvent(ftxui::Event event) { - return this->search_input->OnEvent(event); + if (event == ftxui::Event::Return) + { + if (!this->needle.empty()) + { + this->search_results = this->repository->search_names(this->architecture, this->needle); + } + return true; + } + else + { + return this->search_input->OnEvent(event); + } + return false; } } diff --git a/cli/component.hpp b/cli/component.hpp index 226e7f7..c419020 100644 --- a/cli/component.hpp +++ b/cli/component.hpp @@ -59,9 +59,16 @@ namespace katja class SearchPage final : public PageBase { std::string needle; - ftxui::Component search_input = ftxui::Input(&this->needle, "Search"); + ftxui::Component search_input; + ftxui::Component type_input; + std::shared_ptr repository; + std::string architecture; + std::vector search_results; + int search_type{ 0 }; public: + SearchPage(std::shared_ptr repository, const std::string& architecture); + void Load() override; ftxui::Element OnRender() override; bool OnEvent(ftxui::Event event) override; diff --git a/cli/main.cpp b/cli/main.cpp index f145905..aa0394b 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -23,7 +23,7 @@ int main(int argc, const char **argv) auto container = Screen(std::vector>{ { "Home", ftxui::Make() }, { "Updates", ftxui::Make(repository, std::move(installed_database)) }, - { "Search", ftxui::Make() } + { "Search", ftxui::Make(repository, "x86-64") } }, screen.ExitLoopClosure()); screen.Loop(container);