Skip to content

Commit

Permalink
a little code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 3, 2024
1 parent 3e80145 commit f374d6a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 62 deletions.
50 changes: 18 additions & 32 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ std::vector<std::string> OSTreeTUI::parseVisibleCommitMap(cpplibostree::OSTreeRe
return visibleCommitViewMap;
}

int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& startupBranches, bool showTooltips) {
int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& startupBranches) {
using namespace ftxui;

// - STATES ---------- ----------
Expand Down Expand Up @@ -86,25 +86,19 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
visibleCommitViewMap = parseVisibleCommitMap(ostreeRepo, visibleBranches); // TODO This update shouldn't be made here...

// COMMIT TREE
/* TODO The commit-tree is currentrly under a heavy rebuild, see implementation To-Dos below.
* For a general list of To-Dos refer to https://github.com/AP-Sensing/ostree-tui/pull/21
/* The commit-tree is currentrly under a heavy rebuild, see implementation To-Dos below.
* For a general list of To-Dos refer to https://github.com/AP-Sensing/ostree-tui/pull/21
*
* TODO extend with keyboard functionality:
* normal scrolling through commits (should also highlight selected commit)
* if 'p' is pressed: start promotion
* if 'd' is pressed: open deletion window
* TODO add commit deletion
* add deletion button & ask for confirmation (also add keyboard functionality)
* TODO maybe re-arrange the manager window to remove the tabs again
* now that promotion and deletion is gone / changed, we have enough rooom to fit both the info and the filter
* TODO update the ostree-tui
* after promotion & after filtering branches
* TOOD code cleanup:
* snake_case to camelCase (consistent)
* make const& where applicable
*/
// commit promotion state
// TODO store shared information about which commit is in which state
// TODO especially needed for keyboard shortcuts
// store shared information about which commit is in which state
// each commit can then display itself the way it should
// * is promotion action active?
// * keyboard or mouse?
Expand All @@ -121,15 +115,15 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
Component commitList;
Component tree;

int scroll_offset{0};
int scrollOffset{0};

auto refresh_commitComponents = [&] {
commitComponents.clear();
int i{0};
visibleCommitViewMap = parseVisibleCommitMap(ostreeRepo, visibleBranches);
for (auto& hash : visibleCommitViewMap) {
commitComponents.push_back(
CommitComponent(i, scroll_offset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, hash, ostreeRepo, refresh)
CommitComponent(i, scrollOffset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, hash, ostreeRepo, refresh)
);
i++;
}
Expand All @@ -154,18 +148,16 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
promotionBranchColorMap.insert({str,Color::GrayDark});
}
}
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, columnToBranchMap, promotionBranchColorMap, scroll_offset, selectedCommit);
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, columnToBranchMap, promotionBranchColorMap, scrollOffset, selectedCommit);
}
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, columnToBranchMap, branchColorMap, scroll_offset, selectedCommit);
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, columnToBranchMap, branchColorMap, scrollOffset, selectedCommit);
});

Component commitListComponent = Container::Horizontal({
tree,
commitList
});

// - UPDATES ---------- ----------

/// refresh all graphical components in the commit-tree
auto refresh_commitListComoponent = [&] {
commitListComponent->DetachAllChildren();
Expand All @@ -187,36 +179,31 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
commitListComponent = CatchEvent(commitListComponent, [&](Event event) {
// scroll
if (event.is_mouse() && event.mouse().button == Mouse::WheelUp) {
if (scroll_offset < 0) {
++scroll_offset;
if (scrollOffset < 0) {
++scrollOffset;
}
selectedCommit = -scroll_offset / 4;
selectedCommit = -scrollOffset / 4;
return true;
}
if (event.is_mouse() && event.mouse().button == Mouse::WheelDown) {
--scroll_offset;
selectedCommit = -scroll_offset / 4;
--scrollOffset;
selectedCommit = -scrollOffset / 4;
return true;
}
// switch through commits
if (event == Event::ArrowUp || event == Event::Character('k')) {
scroll_offset = std::min(0, scroll_offset + 4);
selectedCommit = -scroll_offset / 4;
scrollOffset = std::min(0, scrollOffset + 4);
selectedCommit = -scrollOffset / 4;
return true;
}
if (event == Event::ArrowDown || event == Event::Character('j')) {
scroll_offset -= 4;
selectedCommit = -scroll_offset / 4;
scrollOffset -= 4;
selectedCommit = -scrollOffset / 4;
return true;
}
return false;
});

/*
* END of commit-tree TODO
* Probably shouldn't have to change anything outside of this.
*/

// INTERCHANGEABLE VIEW
// info
Component infoView = Renderer([&] {
Expand Down Expand Up @@ -319,7 +306,6 @@ int OSTreeTUI::showHelp(const std::string& caller, const std::string& errorMessa
// option, arguments, meaning
{"-h, --help", "", "Show help options. The REPOSITORY_PATH can be omitted"},
{"-r, --refs", "REF [REF...]", "Specify a list of visible refs at startup if not specified, show all refs"},
{"-n, --no-tooltips", "", "Hide Tooltips in promotion view."}
};

Elements options {text("Options:")};
Expand Down
2 changes: 1 addition & 1 deletion src/core/OSTreeTUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace OSTreeTUI {
*
* @param repo ostree repository path
*/
int main(const std::string& repo, const std::vector<std::string>& startupBranches = {}, bool showTooltips = true);
int main(const std::string& repo, const std::vector<std::string>& startupBranches = {});

/**
* @brief Print help page
Expand Down
6 changes: 3 additions & 3 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo,
const std::unordered_map<std::string, bool>& visibleBranches,
std::vector<std::string>& columnToBranchMap,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap,
int scroll_offset,
int scrollOffset,
size_t selectedCommit) {
using namespace ftxui;

Expand Down Expand Up @@ -54,11 +54,11 @@ ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo,
// treeElements, commElements, commit, highlight, usedBranches, branchColorMap);
}
// commit
if (scroll_offset++ >= 0) {
if (scrollOffset++ >= 0) {
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_NODE, commit, usedBranches, branchColorMap));
}
for (int i{0}; i < 3; i++) {
if (scroll_offset++ >= 0) {
if (scrollOffset++ >= 0) {
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_TREE, commit, usedBranches, branchColorMap));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace CommitRender {
const std::unordered_map<std::string, bool>& visibleBranches,
std::vector<std::string>& columnToBranchMap,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap,
int scroll_offset,
int scrollOffset,
size_t selectedCommit = 0);

/**
Expand Down
25 changes: 9 additions & 16 deletions src/core/commitComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*
* This SnappyWindow Component is a modified version of the Window in the following repository
* Many parts of this CommitComponentImpl are heavily inspired from the Window implementation in the following repository:
* Title: ftxui
* Author: Arthur Sonzogni
* Date: 2023
* Availability: https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
*
* TODO - This should probably be set to inheritance or something.
* Currently, this is A LOT of duplicate code and only for testing.
*/

#include "commitComponent.hpp"
Expand Down Expand Up @@ -36,7 +34,7 @@ namespace ftxui {

namespace {

/// unmodified copy from https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
/// from https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
Decorator PositionAndSize(int left, int top, int width, int height) {
return [=](Element element) {
element |= size(WIDTH, EQUAL, width);
Expand All @@ -55,7 +53,7 @@ Decorator PositionAndSize(int left, int top, int width, int height) {
};
}

/// unmodified copy from https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
/// from https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
Element DefaultRenderState(const WindowRenderState& state) {
Element element = state.inner;
if (!state.active) {
Expand All @@ -65,16 +63,14 @@ Element DefaultRenderState(const WindowRenderState& state) {
element = window(text(state.title), element);
element |= clear_under;

const Color color = Color::Red;

return element;
}

/// @brief Draggable commit window, including ostree-tui logic for overlap detection, etc.
class CommitComponentImpl : public ComponentBase, public WindowOptions {
public:
explicit CommitComponentImpl(int position,
int& scroll_offset,
int& scrollOffset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
Expand All @@ -83,7 +79,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
bool& refresh) :
scroll_offset(scroll_offset),
scrollOffset(scrollOffset),
inPromotionSelection(inPromotionSelection),
promotionHash(promotionHash),
promotionBranch(promotionBranch),
Expand Down Expand Up @@ -112,9 +108,6 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
Element Render() final {
auto element = ComponentBase::Render();

const bool captureable =
captured_mouse_ || ScreenInteractive::Active()->CaptureMouse();

const WindowRenderState state = {
element,
title(),
Expand All @@ -126,7 +119,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {

// Position and record the drawn area of the window.
element |= reflect(box_window_);
element |= PositionAndSize(left(), top() + scroll_offset, width(), height());
element |= PositionAndSize(left(), top() + scrollOffset, width(), height());
element |= reflect(box_);

return element;
Expand Down Expand Up @@ -272,7 +265,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
bool mouse_hover_ = false;
bool drag_ = false;

int& scroll_offset;
int& scrollOffset;

// ostree-tui specific members
// TODO store commit data
Expand Down Expand Up @@ -338,7 +331,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {


Component CommitComponent(int position,
int& scroll_offset,
int& scrollOffset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
Expand All @@ -347,7 +340,7 @@ Component CommitComponent(int position,
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
bool& refresh) {
return Make<CommitComponentImpl>(position, scroll_offset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, commit, ostreerepo, refresh);
return Make<CommitComponentImpl>(position, scrollOffset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, commit, ostreerepo, refresh);
}

}; // namespace ftxui
Expand Down
14 changes: 8 additions & 6 deletions src/core/commitComponent.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#ifndef COMMITCOMPONENT_H
#define COMMITCOMPONENT_H
#pragma once

#include <ftxui/component/component.hpp>

#include "ftxui/component/component_base.hpp" // for Component

#include "../util/cpplibostree.hpp"

namespace ftxui {
/**
* @brief
* @brief Creates a window, containing a hash and some of its details.
* The window has a pre-defined position and snaps back to it,
* after being dragged & let go. When hovering over a branch,
* defined in `columnToBranchMap`, the window expands to a commit
* promotion window.
* To be used with other windows, use a ftxui::Component::Stacked.
*
* @return Component
*/
Component CommitComponent(int position,
int& scroll_offset,
int& scrollOffset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
Expand All @@ -25,4 +28,3 @@ namespace ftxui {
bool& refresh);

} // namespace ftxui
#endif /* end of include guard: COMMITCOMPONENT_H */
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ int main(int argc, const char** argv) {
std::string repo = args.at(0);
// -r, --refs
std::vector<std::string> startupBranches = getArgOptions(args, {"-r", "--refs"});
// -n, --no-tooltips
bool hideTooltips = argExists(args, "-n") || argExists(args, "--no-tooltips");

// OSTree TUI
return OSTreeTUI::main(repo, startupBranches, !hideTooltips);
return OSTreeTUI::main(repo, startupBranches);
}

0 comments on commit f374d6a

Please sign in to comment.