summaryrefslogtreecommitdiff
path: root/cli/component.cpp
blob: 6735077ba0fef8eceff145d18a9c279456a12966 (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
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */
module;

#include <optional>
#include <sstream>

#include <ftxui/component/event.hpp>
#include <ftxui/component/component.hpp>
#include <ftxui/dom/elements.hpp>

import katja.repository;

export module katja.component;

export namespace katja
{
    class PackageListBase : public ftxui::ComponentBase
    {
        std::string title;
        const std::vector<package_identifier> packages;
        std::optional<std::size_t> selected;

    public:
        PackageListBase(const std::string& title, const std::vector<package_identifier>& packages = {})
            : title(title), packages(packages)
        {
        }

        ftxui::Element OnRender() override
        {
            std::vector<ftxui::Element> lines;

            for (const auto& package_identifier : this->packages)
            {
                auto line = ftxui::text(package_identifier.to_string()) | color(ftxui::Color::SkyBlue2);
                lines.push_back(line);
            }
            if (this->selected.has_value() && this->selected.value() < lines.size())
            {
                lines[this->selected.value()] |= ftxui::focus;
            }
            std::stringstream summary;

            summary << title << '(' << packages.size() << ')';

            return ftxui::window(ftxui::text(summary.str()), ftxui::vbox(lines) | ftxui::yframe);
        }

        bool OnEvent(ftxui::Event event) override
        {
            if (event == ftxui::Event::ArrowDown)
            {
                if (!this->selected.has_value() && !this->packages.empty())
                {
                    this->selected = std::make_optional<std::size_t>(0);
                }
                else if (this->selected.has_value() && this->selected.value() + 1 < this->packages.size())
                {
                    this->selected = std::make_optional<std::size_t>(this->selected.value() + 1);
                }
                return true;
            }
            else if (event == ftxui::Event::ArrowUp)
            {
                if (!this->selected.has_value() && !this->packages.empty())
                {
                    this->selected = std::make_optional<std::size_t>(0);
                }
                else if (this->selected.has_value()
                        && this->selected.value() < this->packages.size()
                        && this->selected.value() > 0)
                {
                    this->selected = std::make_optional<std::size_t>(this->selected.value() - 1);
                }
                return true;
            }
            return false;
        }
    };

    ftxui::Component PackageList(const std::string& title, const std::vector<package_identifier>& packages = {})
    {
        return ftxui::Make<PackageListBase>(title, packages);
    }
}