Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 10, 2024
1 parent 5dda232 commit 597ce18
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/core/OSTreeTUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

#pragma once

#include <memory>
#include <string>
#include <vector>
#include <memory>

#include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
#include "ftxui/component/component_base.hpp" // for ComponentBase
Expand All @@ -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<std::string> startupBranches = {});
explicit OSTreeTUI (const std::string& repo, const std::vector<std::string> startupBranches = {});

/**
* @brief Runs the OSTreeTUI (starts the ftxui screen loop).
Expand Down Expand Up @@ -90,20 +90,20 @@ class OSTreeTUI {
private:
// model
cpplibostree::OSTreeRepo ostreeRepo;

// backend states
size_t selectedCommit;
std::unordered_map<std::string, bool> visibleBranches{}; // map branch -> visibe
std::vector<std::string> columnToBranchMap{}; // map branch -> column in commit-tree (may be merged into one data-structure with visibleBranches)
std::vector<std::string> visibleCommitViewMap{}; // map view-index -> commit-hash
std::unordered_map<std::string, ftxui::Color> branchColorMap{}; // map branch -> color
std::string notificationText = ""; // footer notification
std::unordered_map<std::string, bool> visibleBranches; // map branch -> visibe
std::vector<std::string> columnToBranchMap; // map branch -> column in commit-tree (may be merged into one data-structure with visibleBranches)
std::vector<std::string> visibleCommitViewMap; // map view-index -> commit-hash
std::unordered_map<std::string, ftxui::Color> 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};
Expand Down
62 changes: 30 additions & 32 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#include "commit.hpp"

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <format>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <algorithm>

#include <ftxui/component/component_base.hpp> // for Component, ComponentBase
#include "ftxui/component/component.hpp"// for Make
#include <ftxui/component/event.hpp> // 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 <ftxui/component/component_options.hpp>
#include <ftxui/component/screen_interactive.hpp> // 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"

Expand Down Expand Up @@ -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({
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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<CommitComponentImpl>(position, commit, ostreetui);
}

Expand All @@ -351,7 +352,7 @@ ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::
int scrollOffset = ostreetui.getScrollOffset();

// check empty commit list
if (ostreetui.getVisibleCommitViewMap().size() <= 0 || ostreetui.getVisibleBranches().size() <= 0) {
if (ostreetui.getVisibleCommitViewMap().empty() || ostreetui.getVisibleBranches().empty()) {
return color(Color::RedLight, text(" no commits to be shown ") | bold | center);
}

Expand All @@ -369,12 +370,11 @@ ftxui::Element commitRender(OSTreeTUI& ostreetui, const std::unordered_map<std::
Elements treeElements{};
Elements commElements{};

std::string markedString = ostreetui.getOstreeRepo().getCommitList().at(ostreetui.getVisibleCommitViewMap().at(selectedCommit)).hash;
ostreetui.getColumnToBranchMap().clear();
for (const auto& visibleCommitIndex : ostreetui.getVisibleCommitViewMap()) {
cpplibostree::Commit commit = ostreetui.getOstreeRepo().getCommitList().at(visibleCommitIndex);
const cpplibostree::Commit commit = ostreetui.getOstreeRepo().getCommitList().at(visibleCommitIndex);
// branch head if it is first branch usage
std::string relevantBranch = commit.branch;
const std::string relevantBranch = commit.branch;
if (usedBranches.at(relevantBranch) == -1) {
ostreetui.getColumnToBranchMap().push_back(relevantBranch);
usedBranches.at(relevantBranch) = nextAvailableSpace--;
Expand Down Expand Up @@ -403,7 +403,7 @@ ftxui::Element addTreeLine(const RenderTree& treeLineType,
const std::unordered_map<std::string, ftxui::Color>& 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));

Expand All @@ -413,16 +413,14 @@ 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) {
tree.at(branch.second) = (text(COMMIT_NODE) | color(branchColorMap.at(branch.first)));
} 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)));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include <string>
#include <vector>

#include <ftxui/dom/elements.hpp>
#include <ftxui/component/component.hpp>
#include "ftxui/component/component_base.hpp"
#include <ftxui/dom/elements.hpp>

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

Expand All @@ -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 // | |
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/core/footer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ ftxui::Element Footer::footerRender() {
void Footer::resetContent() {
content = DEFAULT_CONTENT;
}

void Footer::setContent(std::string content) {
this->content = content;
}
12 changes: 8 additions & 4 deletions src/core/footer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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};

};
7 changes: 4 additions & 3 deletions src/core/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand Down

0 comments on commit 597ce18

Please sign in to comment.