diff options
Diffstat (limited to 'src/component.cpp')
| -rw-r--r-- | src/component.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/component.cpp b/src/component.cpp new file mode 100644 index 0000000..9247a1d --- /dev/null +++ b/src/component.cpp @@ -0,0 +1,76 @@ +#include <ftxui/component/screen_interactive.hpp> +#include "component.h" + +namespace katja +{ + PackageList::PackageList(const std::vector<std::string>& header, const std::vector<std::vector<std::string>>& data) + : m_data(data), m_header(header) + { + } + + ftxui::Element PackageList::Render() + { + std::vector<std::vector<std::string>> data{ m_header }; + + std::size_t dimy = ftxui::ScreenInteractive::Active()->dimy(); + if (dimy == 0) + { + dimy = ftxui::Terminal::Size().dimy; + } + if (dimy > 4) // 4 = headers and borders. + { + std::size_t row_count = std::min(m_data.size() - top_line, dimy - 4 + top_line); + + std::copy(m_data.cbegin() + top_line, m_data.cbegin() + row_count, std::back_inserter(data)); + } + ftxui::Table table{ data }; + table.SelectAll().Border(ftxui::LIGHT); + + // Add border around the first column. + table.SelectColumn(0).Border(ftxui::LIGHT); + + // Make first row bold with a double border. + table.SelectRow(0).Decorate(ftxui::bold); + table.SelectRow(0).SeparatorVertical(ftxui::LIGHT); + table.SelectRow(0).Border(ftxui::DOUBLE); + + // Align right the "Release date" column. + table.SelectColumn(2).DecorateCells(ftxui::align_right); + + // Select row from the second to the last. + auto content = table.SelectRows(1, -1); + // Alternate in between 3 colors. + content.DecorateCellsAlternateRow(color(ftxui::Color::Blue), 3, 0); + content.DecorateCellsAlternateRow(color(ftxui::Color::Cyan), 3, 1); + content.DecorateCellsAlternateRow(color(ftxui::Color::White), 3, 2); + + return table.Render(); + } + + bool PackageList::OnEvent(ftxui::Event event) + { + if (event == ftxui::Event::Character('q') || event == ftxui::Event::Escape) + { + event.screen_->ExitLoopClosure()(); + } + else if (event == ftxui::Event::Character('j') || event == ftxui::Event::ArrowDown) + { + if (m_data.size() > 0 && top_line < (m_data.size() - 1)) + { + ++top_line; + } + } + else if (event == ftxui::Event::Character('k') || event == ftxui::Event::ArrowUp) + { + if (top_line > 0) + { + --top_line; + } + } + else + { + return false; + } + return true; + } +} |
