diff --git a/src/core/OSTreeTUI.cpp b/src/core/OSTreeTUI.cpp index 8512c8c..2d905a9 100644 --- a/src/core/OSTreeTUI.cpp +++ b/src/core/OSTreeTUI.cpp @@ -182,7 +182,7 @@ int OSTreeTUI::run() { using namespace std::chrono_literals; // notification is set if (notificationText != "") { - footer.content = notificationText; + footer.setContent(notificationText); screen.Post(Event::Custom); std::this_thread::sleep_for(2s); // clear notification diff --git a/src/core/OSTreeTUI.hpp b/src/core/OSTreeTUI.hpp index 6ab09ab..b1b2187 100644 --- a/src/core/OSTreeTUI.hpp +++ b/src/core/OSTreeTUI.hpp @@ -5,9 +5,9 @@ #pragma once +#include #include #include -#include #include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop #include "ftxui/component/component_base.hpp" // for ComponentBase @@ -28,7 +28,7 @@ class OSTreeTUI { * @param repo ostree repository (OSTreeRepo) * @param startupBranches optional list of branches to pre-select at startup (providing nothing will display all branches) */ - OSTreeTUI (const std::string& repo, const std::vector startupBranches = {}); + explicit OSTreeTUI (const std::string& repo, const std::vector startupBranches = {}); /** * @brief Runs the OSTreeTUI (starts the ftxui screen loop). @@ -90,20 +90,20 @@ class OSTreeTUI { private: // model cpplibostree::OSTreeRepo ostreeRepo; - + // backend states size_t selectedCommit; - std::unordered_map visibleBranches{}; // map branch -> visibe - std::vector columnToBranchMap{}; // map branch -> column in commit-tree (may be merged into one data-structure with visibleBranches) - std::vector visibleCommitViewMap{}; // map view-index -> commit-hash - std::unordered_map branchColorMap{}; // map branch -> color - std::string notificationText = ""; // footer notification + std::unordered_map visibleBranches; // map branch -> visibe + std::vector columnToBranchMap; // map branch -> column in commit-tree (may be merged into one data-structure with visibleBranches) + std::vector visibleCommitViewMap; // map view-index -> commit-hash + std::unordered_map branchColorMap; // map branch -> color + std::string notificationText; // footer notification // view states int scrollOffset{0}; bool inPromotionSelection{false}; - std::string promotionHash{""}; - std::string promotionBranch{""}; + std::string promotionHash; + std::string promotionBranch; // view constants int logSize{45}; diff --git a/src/core/commit.cpp b/src/core/commit.cpp index 79ee0df..b9fadcd 100644 --- a/src/core/commit.cpp +++ b/src/core/commit.cpp @@ -1,25 +1,24 @@ #include "commit.hpp" +#include +#include #include +#include #include #include +#include #include -#include #include // for Component, ComponentBase #include "ftxui/component/component.hpp"// for Make #include // for Event, Event::ArrowDown, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::WheelDown, Mouse::WheelUp -#include "ftxui/dom/deprecated.hpp" // for text #include "ftxui/dom/elements.hpp" // for operator|, Element, size, vbox, EQUAL, HEIGHT, dbox, reflect, focus, inverted, nothing, select, vscroll_indicator, yflex, yframe #include "ftxui/dom/node.hpp" // for Node -#include "ftxui/dom/requirement.hpp" // for Requirement #include "ftxui/screen/box.hpp" // for Box #include #include // for ScreenInteractive -#include "ftxui/dom/elements.hpp" // for text, window, hbox, vbox, size, clear_under, reflect, emptyElement #include "ftxui/screen/color.hpp" // for Color -#include "ftxui/screen/screen.hpp" // for Screen #include "../util/cpplibostree.hpp" @@ -67,7 +66,12 @@ Element DefaultRenderState(const WindowRenderState& state) { class CommitComponentImpl : public ComponentBase, public WindowOptions { public: explicit CommitComponentImpl(int position, std::string commit, OSTreeTUI& ostreetui) : - hash(commit), ostreetui(ostreetui), commit(ostreetui.getOstreeRepo().getCommitList().at(hash)), newVersion(this->commit.version) + hash(std::move(commit)), + ostreetui(ostreetui), + commit(ostreetui.getOstreeRepo().getCommitList().at(hash)), + newVersion(this->commit.version), + drag_initial_y(position * COMMIT_WINDOW_HEIGHT), + drag_initial_x(1) { inner = Renderer([&] { return vbox({ @@ -79,10 +83,8 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { Add(inner); title = hash.substr(0, 8); - top = position * COMMIT_WINDOW_HEIGHT; - drag_initial_y = top(); - left = 1; - drag_initial_x = left(); + top = drag_initial_y; + left = drag_initial_x; width = COMMIT_WINDOW_WIDTH; height = COMMIT_WINDOW_HEIGHT; } @@ -157,12 +159,12 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { if (ostreetui.getInPromotionSelection()) { // navigate promotion branches if (event == Event::ArrowLeft) { - long int it = std::find(ostreetui.getColumnToBranchMap().begin(), ostreetui.getColumnToBranchMap().end(), ostreetui.getPromotionBranch()) - ostreetui.getColumnToBranchMap().begin(); + const long int it = std::find(ostreetui.getColumnToBranchMap().begin(), ostreetui.getColumnToBranchMap().end(), ostreetui.getPromotionBranch()) - ostreetui.getColumnToBranchMap().begin(); ostreetui.setPromotionBranch(ostreetui.getColumnToBranchMap().at((it - 1) % ostreetui.getColumnToBranchMap().size())); return true; } if (event == Event::ArrowRight) { - long int it = std::find(ostreetui.getColumnToBranchMap().begin(), ostreetui.getColumnToBranchMap().end(), ostreetui.getPromotionBranch()) - ostreetui.getColumnToBranchMap().begin(); + const long int it = std::find(ostreetui.getColumnToBranchMap().begin(), ostreetui.getColumnToBranchMap().end(), ostreetui.getPromotionBranch()) - ostreetui.getColumnToBranchMap().begin(); ostreetui.setPromotionBranch(ostreetui.getColumnToBranchMap().at((it + 1) % ostreetui.getColumnToBranchMap().size())); return true; } @@ -195,12 +197,11 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { // reset mouse captured_mouse_ = nullptr; // check if position matches branch & do something if it does - if (ostreetui.getPromotionBranch().size() != 0) { - ostreetui.setPromotionMode(true, hash); - } else { - // not promotion + if (ostreetui.getPromotionBranch().empty()) { ostreetui.setPromotionMode(false, hash); resetWindow(); + } else { + ostreetui.setPromotionMode(true, hash); } return true; } @@ -212,9 +213,9 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { ostreetui.setPromotionMode(true, hash); // TODO switch to ostreetui call // calculate which branch currently is hovered over ostreetui.setPromotionBranch(""); - int branch_pos = event.mouse().x / 2; + const int branch_pos = event.mouse().x / 2; int count{0}; - for (auto& [branch,visible] : ostreetui.getVisibleBranches()) { + for (const auto& [branch,visible] : ostreetui.getVisibleBranches()) { if (visible) { ++count; } @@ -260,7 +261,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { drag_start_x = event.mouse().x - left() - box_.x_min; drag_start_y = event.mouse().y - top() - box_.y_min; - bool drag_old = drag_; + const bool drag_old = drag_; drag_ = true; if (!drag_old && drag_) { // if we start dragging drag_initial_x = left(); @@ -277,10 +278,10 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { int drag_start_x = 0; int drag_start_y = 0; - int drag_initial_x = 0; - int drag_initial_y = 0; - int width_initial = 30; - int height_initial = 4; + int drag_initial_x; + int drag_initial_y; + int width_initial = COMMIT_WINDOW_WIDTH; + int height_initial = COMMIT_WINDOW_HEIGHT; bool mouse_hover_ = false; bool drag_ = false; @@ -341,7 +342,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions { } // namespace -ftxui::Component CommitComponent(int position, std::string commit, OSTreeTUI& ostreetui) { +ftxui::Component CommitComponent(int position, const std::string& commit, OSTreeTUI& ostreetui) { return ftxui::Make(position, commit, ostreetui); } @@ -351,7 +352,7 @@ ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map& branchColorMap) { using namespace ftxui; - std::string relevantBranch = commit.branch; + const std::string relevantBranch = commit.branch; // create an empty branch tree line Elements tree(usedBranches.size(), text(COMMIT_NONE)); @@ -413,7 +413,7 @@ ftxui::Element addTreeLine(const RenderTree& treeLineType, continue; } - if (treeLineType == RenderTree::TREE_LINE_IGNORE_BRANCH && branch.first != relevantBranch) { + if (treeLineType == RenderTree::TREE_LINE_TREE || (treeLineType == RenderTree::TREE_LINE_IGNORE_BRANCH && branch.first != relevantBranch)) { tree.at(branch.second) = (text(COMMIT_TREE) | color(branchColorMap.at(branch.first))); } else if (treeLineType == RenderTree::TREE_LINE_NODE) { if (branch.first == relevantBranch) { @@ -421,8 +421,6 @@ ftxui::Element addTreeLine(const RenderTree& treeLineType, } else { tree.at(branch.second) = (text(COMMIT_TREE) | color(branchColorMap.at(branch.first))); } - } else if (treeLineType == RenderTree::TREE_LINE_TREE) { - tree.at(branch.second) = (text(COMMIT_TREE) | color(branchColorMap.at(branch.first))); } } diff --git a/src/core/commit.hpp b/src/core/commit.hpp index 58fa684..bbb2f6e 100644 --- a/src/core/commit.hpp +++ b/src/core/commit.hpp @@ -14,9 +14,9 @@ #include #include -#include #include #include "ftxui/component/component_base.hpp" +#include #include "../util/cpplibostree.hpp" @@ -28,12 +28,12 @@ namespace CommitRender { constexpr std::string COMMIT_TREE {" │"}; constexpr std::string COMMIT_NONE {" "}; // window dimensions - constexpr size_t COMMIT_WINDOW_HEIGHT {4}; - constexpr size_t COMMIT_WINDOW_WIDTH {32}; - constexpr size_t PROMOTION_WINDOW_HEIGHT {COMMIT_WINDOW_HEIGHT + 11}; - constexpr size_t PROMOTION_WINDOW_WIDTH {COMMIT_WINDOW_WIDTH + 8}; + constexpr int COMMIT_WINDOW_HEIGHT {4}; + constexpr int COMMIT_WINDOW_WIDTH {32}; + constexpr int PROMOTION_WINDOW_HEIGHT {COMMIT_WINDOW_HEIGHT + 11}; + constexpr int PROMOTION_WINDOW_WIDTH {COMMIT_WINDOW_WIDTH + 8}; // render tree types - enum RenderTree { + enum RenderTree : uint8_t { TREE_LINE_NODE, // ☐ | | TREE_LINE_TREE, // | | | TREE_LINE_IGNORE_BRANCH // | | @@ -49,7 +49,7 @@ namespace CommitRender { * * @return Component */ - ftxui::Component CommitComponent(int position, std::string commit, OSTreeTUI& ostreetui); + ftxui::Component CommitComponent(int position, const std::string& commit, OSTreeTUI& ostreetui); /** * @brief create a Renderer for the commit section diff --git a/src/core/footer.cpp b/src/core/footer.cpp index ebb006f..ed7ace0 100644 --- a/src/core/footer.cpp +++ b/src/core/footer.cpp @@ -15,3 +15,7 @@ ftxui::Element Footer::footerRender() { void Footer::resetContent() { content = DEFAULT_CONTENT; } + +void Footer::setContent(std::string content) { + this->content = content; +} diff --git a/src/core/footer.hpp b/src/core/footer.hpp index 801df3f..ba978e0 100644 --- a/src/core/footer.hpp +++ b/src/core/footer.hpp @@ -6,10 +6,6 @@ class Footer { -public: - const std::string DEFAULT_CONTENT {" || Alt+Q : Quit || Alt+R : Refresh || Alt+C : Copy commit hash || Alt+P : Promote Commit "}; - std::string content {DEFAULT_CONTENT}; - public: Footer() = default; @@ -18,4 +14,12 @@ class Footer { /// create a Renderer for the footer section ftxui::Element footerRender(); + + // Setter + void setContent(std::string content); + +private: + const std::string DEFAULT_CONTENT {" || Alt+Q : Quit || Alt+R : Refresh || Alt+C : Copy commit hash || Alt+P : Promote Commit "}; + std::string content {DEFAULT_CONTENT}; + }; diff --git a/src/core/manager.hpp b/src/core/manager.hpp index 81d5004..b254494 100644 --- a/src/core/manager.hpp +++ b/src/core/manager.hpp @@ -19,6 +19,9 @@ class OSTreeTUI; /// Interchangeable View class Manager { public: + Manager(OSTreeTUI& ostreetui, const ftxui::Component& infoView, const ftxui::Component& filterView); + +private: OSTreeTUI& ostreetui; int tab_index{0}; @@ -30,12 +33,10 @@ class Manager { ftxui::Component tabSelection; ftxui::Component tabContent; +public: // because the combination of all interchangeable views is very simple, // we can (in contrast to the other ones) render this one immediately ftxui::Component managerRenderer; - -public: - Manager(OSTreeTUI& ostreetui, const ftxui::Component& infoView, const ftxui::Component& filterView); }; class CommitInfoManager {