aboutsummaryrefslogtreecommitdiff
path: root/cli/main.cpp
blob: d5f8cf3c663969a740f5f3036183fe1a228ffb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <filesystem>

#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <toml.hpp>

#include "katja/sbo.hpp"
#include "katja/database.hpp"
#include "component.hpp"

class MyContainer final : public ftxui::ComponentBase
{
public:
    std::vector<std::string> menu_entries;
    std::vector<ftxui::Component> menu_pages;
    int menu_selected{ 0 };
    ftxui::Component menu;
    std::function<void()> on_enter;

    MyContainer(std::vector<std::pair<std::string, ftxui::Component>> pages)
    {
        std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_entries),
                [](const std::pair<std::string, ftxui::Component>& pair) { return pair.first; });
        std::transform(std::cbegin(pages), std::cend(pages), std::back_inserter(this->menu_pages),
                [](const std::pair<std::string, ftxui::Component>& pair) { return pair.second; });

        ftxui::MenuOption menu_option = ftxui::MenuOption::Horizontal();
        menu = ftxui::Menu(&this->menu_entries, &this->menu_selected, menu_option);
    }

    ftxui::Element Render() override
    {
        return ftxui::vbox({
            this->menu->Render(),
            this->menu_pages.at(this->menu_selected)->Render()
        });
    }

    bool OnEvent(ftxui::Event event) override
    {
        if (event.character() == "q")
        {
            on_enter();
            return true;
        }
        return menu->OnEvent(event);
    }
};


int main(int argc, const char **argv)
{
    auto configuration = toml::parse("config/katja.toml");
    std::multimap<std::string, katja::database_package> installed_database = katja::read_installed_database();

    for (const auto& [repository_name, repository_value] : configuration.as_table())
    {
        std::filesystem::path slackbuild_repository{ repository_value.at("path").as_string() };
        katja::sbo_repository repository{ slackbuild_repository };
        auto updates = repository.get_updates(installed_database);

        auto screen = ftxui::ScreenInteractive::Fullscreen();

        auto container = std::make_shared<MyContainer>(std::vector<std::pair<std::string, ftxui::Component>>{
            { "Home", std::make_shared<katja::WelcomePage>() },
            { "Updates", std::make_shared<katja::UpdatesPage>(std::move(updates)) },
            { "Search", std::make_shared<katja::SearchPage>() }
        });
        container->on_enter = screen.ExitLoopClosure();

        screen.Loop(container);
    }
    return EXIT_SUCCESS;
}