aboutsummaryrefslogtreecommitdiff
path: root/cli/component.cpp
blob: db29052d69bacc8d7bae6bcb9b2d8800a21f7704 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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 == ftxui::Event::CtrlQ && this->on_enter)
        {
            on_enter();
            return true;
        }
        int previously = this->menu_selected;
        bool result{ false };

        if (event == ftxui::Event::CtrlP)
        {
            result = menu->OnEvent(ftxui::Event::ArrowLeft);
        }
        else if (event == ftxui::Event::CtrlN)
        {
            result = menu->OnEvent(ftxui::Event::ArrowRight);
        }
        if (previously != this->menu_selected)
        {
            this->menu_pages.at(this->menu_selected)->Load();
        }
        if (!result)
        {
            result = this->menu_pages.at(this->menu_selected)->OnEvent(event);
        }
        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::shared_ptr<struct repository> repository, package_database database)
        : repository(repository), database(database)
    {
    }

    void UpdatesPage::Load()
    {
        this->updatable = repository->get_updates(this->database);
    }

    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()
    {
    }

    ftxui::Element SearchPage::OnRender()
    {
        return ftxui::vbox({
            ftxui::hbox({
                this->search_input->Render()
            })
        });
    }

    bool SearchPage::OnEvent(ftxui::Event event)
    {
        return this->search_input->OnEvent(event);
    }
}