Skip to content

Commit

Permalink
Add selected-commit highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 11, 2024
1 parent 9a383ec commit b03fa37
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ OSTreeTUI::OSTreeTUI (const std::string& repo, const std::vector<std::string> st
promotionBranchColorMap.insert({str,Color::GrayDark});
}
}
return CommitRender::commitRender(*this, promotionBranchColorMap, selectedCommit);
return CommitRender::commitRender(*this, promotionBranchColorMap);
}
return CommitRender::commitRender(*this, branchColorMap, selectedCommit);
return CommitRender::commitRender(*this, branchColorMap);
});

commitListComponent = Container::Horizontal({
Expand All @@ -84,12 +84,12 @@ OSTreeTUI::OSTreeTUI (const std::string& repo, const std::vector<std::string> st
if (scrollOffset < 0) {
++scrollOffset;
}
selectedCommit = -scrollOffset / 4;
selectedCommit = (-scrollOffset + 3) / 4;
return true;
}
if (event.is_mouse() && event.mouse().button == Mouse::WheelDown) {
--scrollOffset;
selectedCommit = -scrollOffset / 4;
selectedCommit = (-scrollOffset + 3) / 4;
return true;
}
// switch through commits
Expand Down Expand Up @@ -304,6 +304,10 @@ const cpplibostree::OSTreeRepo& OSTreeTUI::getOstreeRepo() const {
return ostreeRepo;
}

const size_t& OSTreeTUI::getSelectedCommit() const {
return selectedCommit;
}

const std::string& OSTreeTUI::getPromotionBranch() const {
return promotionBranch;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/OSTreeTUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class OSTreeTUI {

// GETTER
const cpplibostree::OSTreeRepo& getOstreeRepo() const;
const size_t& getSelectedCommit() const;
const std::string& getPromotionBranch() const;
const std::unordered_map<std::string, bool>& getVisibleBranches() const;
const std::vector<std::string>& getColumnToBranchMap() const;
Expand Down
18 changes: 13 additions & 5 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ Decorator PositionAndSize(int left, int top, int width, int height) {
}

/// Partially inspired from https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
Element DefaultRenderState(const WindowRenderState& state) {
Element DefaultRenderState(const WindowRenderState& state, ftxui::Color selectedColor = Color::White) {
Element element = state.inner;
//if (!state.active) {
// element |= dim;
//}
if (selectedColor == Color::White) {
element |= dim;
} else {
element |= bold;
}
element |= color(selectedColor);

element = window(text(state.title), element);
element |= clear_under;
Expand Down Expand Up @@ -147,7 +151,11 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
drag_
};

element = render ? render(state) : DefaultRenderState(state);
if (drag_initial_y / COMMIT_WINDOW_HEIGHT == ostreetui.getSelectedCommit()) { // selected
element = render ? render(state) : DefaultRenderState(state, ostreetui.getBranchColorMap().at(commit.branch));
} else {
element = render ? render(state) : DefaultRenderState(state);
}

// Position and record the drawn area of the window.
element |= reflect(box_window_);
Expand Down Expand Up @@ -352,7 +360,7 @@ ftxui::Component CommitComponent(int position, const std::string& commit, OSTree
return ftxui::Make<CommitComponentImpl>(position, commit, ostreetui);
}

ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::string, ftxui::Color>& branchColorMap, size_t selectedCommit) {
ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::string, ftxui::Color>& branchColorMap) {
using namespace ftxui;

int scrollOffset = ostreetui.getScrollOffset();
Expand Down
2 changes: 1 addition & 1 deletion src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace CommitRender {
* @return ftxui::Element
*/
ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::string,
ftxui::Color>& branchColorMap, size_t selectedCommit = 0);
ftxui::Color>& branchColorMap);

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

0 comments on commit b03fa37

Please sign in to comment.