From cb15e43191d40923221b4bbcebb69a959a4138fc Mon Sep 17 00:00:00 2001 From: Timon Ensel Date: Wed, 6 Nov 2024 18:37:39 +0100 Subject: [PATCH] refactor commitRender to use ostreetui class --- src/core/OSTreeTUI.cpp | 4 ++-- src/core/commit.cpp | 26 +++++++++++--------------- src/core/commit.hpp | 13 ++----------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/core/OSTreeTUI.cpp b/src/core/OSTreeTUI.cpp index 994f627..2a445f9 100644 --- a/src/core/OSTreeTUI.cpp +++ b/src/core/OSTreeTUI.cpp @@ -86,9 +86,9 @@ OSTreeTUI::OSTreeTUI (const std::string& repo, const std::vector st promotionBranchColorMap.insert({str,Color::GrayDark}); } } - return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, columnToBranchMap, promotionBranchColorMap, scrollOffset, selectedCommit); + return CommitRender::commitRender(*this, promotionBranchColorMap, selectedCommit); } - return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, columnToBranchMap, branchColorMap, scrollOffset, selectedCommit); + return CommitRender::commitRender(*this, branchColorMap, selectedCommit); }); commitListComponent = Container::Horizontal({ diff --git a/src/core/commit.cpp b/src/core/commit.cpp index e9252a0..85c0b5c 100644 --- a/src/core/commit.cpp +++ b/src/core/commit.cpp @@ -318,23 +318,19 @@ ftxui::Component CommitComponent(int position, std::string commit, OSTreeTUI& os return ftxui::Make(position, commit, ostreetui); } -ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo, - const std::vector& visibleCommitMap, - const std::unordered_map& visibleBranches, - std::vector& columnToBranchMap, - const std::unordered_map& branchColorMap, - int scrollOffset, - size_t selectedCommit) { +ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map& branchColorMap, size_t selectedCommit) { using namespace ftxui; + int scrollOffset = ostreetui.scrollOffset; + // check empty commit list - if (visibleCommitMap.size() <= 0 || visibleBranches.size() <= 0) { + if (ostreetui.visibleCommitViewMap.size() <= 0 || ostreetui.visibleBranches.size() <= 0) { return color(Color::RedLight, text(" no commits to be shown ") | bold | center); } // stores the dedicated tree-column of each branch, -1 meaning not displayed yet std::unordered_map usedBranches{}; - for (const auto& branchPair : visibleBranches) { + for (const auto& branchPair : ostreetui.visibleBranches) { if (branchPair.second) { usedBranches[branchPair.first] = -1; } @@ -346,14 +342,14 @@ ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo, Elements treeElements{}; Elements commElements{}; - std::string markedString = repo.getCommitList().at(visibleCommitMap.at(selectedCommit)).hash; - columnToBranchMap.clear(); - for (const auto& visibleCommitIndex : visibleCommitMap) { - cpplibostree::Commit commit = repo.getCommitList().at(visibleCommitIndex); + std::string markedString = ostreetui.ostreeRepo.getCommitList().at(ostreetui.visibleCommitViewMap.at(selectedCommit)).hash; + ostreetui.columnToBranchMap.clear(); + for (const auto& visibleCommitIndex : ostreetui.visibleCommitViewMap) { + cpplibostree::Commit commit = ostreetui.ostreeRepo.getCommitList().at(visibleCommitIndex); // branch head if it is first branch usage std::string relevantBranch = commit.branch; if (usedBranches.at(relevantBranch) == -1) { - columnToBranchMap.push_back(relevantBranch); + ostreetui.columnToBranchMap.push_back(relevantBranch); usedBranches.at(relevantBranch) = nextAvailableSpace--; // TODO somehow incorporate the branch name into the commit-tree //addLine(RenderTree::TREE_LINE_IGNORE_BRANCH, RenderLine::BRANCH_HEAD, @@ -369,7 +365,7 @@ ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo, } } } - std::reverse(columnToBranchMap.begin(), columnToBranchMap.end()); + std::reverse(ostreetui.columnToBranchMap.begin(), ostreetui.columnToBranchMap.end()); return hbox({ vbox(std::move(treeElements)), diff --git a/src/core/commit.hpp b/src/core/commit.hpp index 8fa1926..2015767 100644 --- a/src/core/commit.hpp +++ b/src/core/commit.hpp @@ -51,21 +51,12 @@ namespace CommitRender { /** * @brief create a Renderer for the commit section * - * @param repo OSTree repository - * @param visibleCommitMap List of visible commit hashes - * @param visibleBranches List of visible branches + * @param ostreetui OSTreeTUI containing OSTreeRepo and UI info * @param branchColorMap Map from branch to its display color * @param selectedCommit Commit that should be marked as selected * @return ftxui::Element */ - ftxui::Element commitRender( - cpplibostree::OSTreeRepo& repo, - const std::vector& visibleCommitMap, - const std::unordered_map& visibleBranches, - std::vector& columnToBranchMap, - const std::unordered_map& branchColorMap, - int scrollOffset, - size_t selectedCommit = 0); + ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map& branchColorMap, size_t selectedCommit = 0); /** * @brief build a commit-tree line