Skip to content

Commit

Permalink
add commit-drop window and keyboard shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 15, 2024
1 parent 9cf1f91 commit 85c663f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string>& st

// add application shortcuts
mainContainer = CatchEvent(container | border, [&](const Event& event) {
// start commit promotion window
if (event == Event::AltP) {
SetViewMode(ViewMode::COMMIT_PROMOTION, visibleCommitViewMap.at(selectedCommit));
}
// start commit deletion window
if (event == Event::AltD) {
SetViewMode(ViewMode::COMMIT_DROP, visibleCommitViewMap.at(selectedCommit));
}
// copy commit id
if (event == Event::AltC) {
std::string hash = visibleCommitViewMap.at(selectedCommit);
Expand Down Expand Up @@ -210,6 +215,10 @@ bool OSTreeTUI::RefreshOSTreeRepository() {
}

bool OSTreeTUI::SetViewMode(ViewMode newViewMode, const std::string& hash, bool setModeBranch) {
// nothing to change
if (newViewMode == viewMode && hash == modeHash) {
return false;
}
// deactivate promotion mode
if (newViewMode == ViewMode::DEFAULT) {
viewMode = ViewMode::DEFAULT;
Expand All @@ -218,14 +227,19 @@ bool OSTreeTUI::SetViewMode(ViewMode newViewMode, const std::string& hash, bool
return true;
}
// set promotion mode
if (viewMode == ViewMode::DEFAULT || hash != modeHash) {
if (newViewMode == ViewMode::COMMIT_PROMOTION) {
viewMode = ViewMode::COMMIT_PROMOTION;
if (setModeBranch) {
modeBranch = modeBranch.empty() ? columnToBranchMap.at(0) : modeBranch;
}
modeHash = hash;
return true;
}
// set deletion mode
if (newViewMode == ViewMode::COMMIT_DROP) {
viewMode = newViewMode;
modeHash = hash;
}
// nothing to update
return false;
}
Expand All @@ -250,7 +264,7 @@ bool OSTreeTUI::PromoteCommit(const std::string& hash,

bool OSTreeTUI::DropLastCommit(const cpplibostree::Commit& commit) {
bool success = ostreeRepo.DropLastCommit(commit);
// SetDeletionMode(false);
SetViewMode(ViewMode::DEFAULT);
// reload repository
if (success) {
scrollOffset = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/OSTreeTUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class OSTreeTUI {

// view states
int scrollOffset{0};
ViewMode viewMode = DEFAULT;
ViewMode viewMode = ViewMode::DEFAULT;
std::string modeHash;
std::string modeBranch;

Expand Down
45 changes: 41 additions & 4 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
width() = PROMOTION_WINDOW_WIDTH;
height() = PROMOTION_WINDOW_HEIGHT;
// change inner to promotion layout
simpleCommit = inner;
DetachAllChildren();
Add(promotionView);
if (!Focused()) {
Expand All @@ -135,6 +134,16 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
}
}

void startDeletionWindow() {
left() = std::max(left(), -2);
width() = DELETION_WINDOW_WIDTH;
height() = DELETION_WINDOW_HEIGHT;
// change inner to deletion layout
DetachAllChildren();
Add(deletionView);
TakeFocus();
}

void executePromotion() {
// promote on the ostree repo
std::vector<std::string> metadataStrings;
Expand All @@ -145,7 +154,13 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
resetWindow();
}

void cancelPromotion() {
void executeDeletion() {
// delete on the ostree repo
ostreetui.DropLastCommit(ostreetui.GetOstreeRepo().getCommitList().at(hash));
resetWindow();
}

void cancelSpecialWindow() {
ostreetui.SetViewMode(ViewMode::DEFAULT);
resetWindow();
}
Expand All @@ -159,6 +174,9 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
} else {
resetWindow(false);
}
} else if (ostreetui.GetViewMode() == ViewMode::COMMIT_DROP &&
ostreetui.GetModeHash() == hash) {
startDeletionWindow();
}

auto element = ComponentBase::Render();
Expand Down Expand Up @@ -211,7 +229,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
}
// cancel
if (event == Event::Escape) {
cancelPromotion();
cancelSpecialWindow();
return true;
}
}
Expand Down Expand Up @@ -356,9 +374,28 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
text("" + ostreetui.GetModeBranch()) | bold, text("") | bold});
}),
Container::Horizontal({
Button(" Cancel ", [&] { cancelPromotion(); }) | color(Color::Red) | flex,
Button(" Cancel ", [&] { cancelSpecialWindow(); }) | color(Color::Red) | flex,
Button(" Promote ", [&] { executePromotion(); }) | color(Color::Green) | flex,
})});
Component deletionView = Container::Vertical(
{Renderer([&] {
return vbox({text(""), text(" Remove Commit...") | bold, text(""),
hbox({
text("") | color(Color::Red),
text(hash.substr(0, 8)) | bold | color(Color::Red),
}),
text("" + commit.subject) | color(Color::Red),
text("") | color(Color::Red),
hbox({
text("") | color(Color::Red),
text("from branch:") | dim,
}),
text("" + ostreetui.GetModeBranch()) | dim, text("") | dim});
}),
Container::Horizontal({
Button(" Cancel ", [&] { cancelSpecialWindow(); }) | color(Color::Red) | flex,
Button(" Remove ", [&] { executeDeletion(); }) | color(Color::Green) | flex,
})});
};

} // namespace
Expand Down
2 changes: 2 additions & 0 deletions src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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};
constexpr int DELETION_WINDOW_HEIGHT{COMMIT_WINDOW_HEIGHT + 11};
constexpr int DELETION_WINDOW_WIDTH{COMMIT_WINDOW_WIDTH + 8};
// render tree types
enum RenderTree : uint8_t {
TREE_LINE_NODE, // ☐ | |
Expand Down
4 changes: 2 additions & 2 deletions src/core/footer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Footer {

private:
const std::string DEFAULT_CONTENT{
" || Alt+Q : Quit || Alt+R : Refresh || Alt+C : Copy commit hash || Alt+P : Promote "
"Commit "};
" || Alt+Q : Quit || Alt+R : Refresh || Alt+C : Copy Hash || Alt+P : Promote || Alt+D: "
"Drop || "};
std::string content{DEFAULT_CONTENT};
};

0 comments on commit 85c663f

Please sign in to comment.