Skip to content

Commit

Permalink
refactor commitRender to use ostreetui class
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 6, 2024
1 parent 9b0d1d8 commit cb15e43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ OSTreeTUI::OSTreeTUI (const std::string& repo, const std::vector<std::string> 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({
Expand Down
26 changes: 11 additions & 15 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,19 @@ ftxui::Component CommitComponent(int position, std::string commit, OSTreeTUI& os
return ftxui::Make<CommitComponentImpl>(position, commit, ostreetui);
}

ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo,
const std::vector<std::string>& visibleCommitMap,
const std::unordered_map<std::string, bool>& visibleBranches,
std::vector<std::string>& columnToBranchMap,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap,
int scrollOffset,
size_t selectedCommit) {
ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::string, ftxui::Color>& 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<std::string, int> usedBranches{};
for (const auto& branchPair : visibleBranches) {
for (const auto& branchPair : ostreetui.visibleBranches) {
if (branchPair.second) {
usedBranches[branchPair.first] = -1;
}
Expand All @@ -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,
Expand All @@ -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)),
Expand Down
13 changes: 2 additions & 11 deletions src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& visibleCommitMap,
const std::unordered_map<std::string, bool>& visibleBranches,
std::vector<std::string>& columnToBranchMap,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap,
int scrollOffset,
size_t selectedCommit = 0);
ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::string, ftxui::Color>& branchColorMap, size_t selectedCommit = 0);

/**
* @brief build a commit-tree line
Expand Down

0 comments on commit cb15e43

Please sign in to comment.