Skip to content

Commit 2076b04

Browse files
committed
Some code cleanup
1 parent e8d5d13 commit 2076b04

12 files changed

+218
-236
lines changed

src/core/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ add_library(ostree-tui_core commit.cpp
99
manager.cpp
1010
manager.hpp
1111
OSTreeTUI.cpp
12-
OSTreeTUI.hpp
13-
)
12+
OSTreeTUI.hpp)
1413

1514
target_link_libraries(ostree-tui_core
1615
PRIVATE clip

src/core/OSTreeTUI.cpp

+50-61
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> startupBranches)
23+
OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string>& startupBranches)
2424
: ostreeRepo(repo), selectedCommit(0), screen(ftxui::ScreenInteractive::Fullscreen()) {
2525
using namespace ftxui;
2626

@@ -38,18 +38,16 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> sta
3838
}
3939
}
4040

41-
// - UI ELEMENTS ---------- ----------
42-
4341
// COMMIT TREE
44-
refresh_commitComponents();
42+
RefreshCommitComponents();
4543

4644
tree = Renderer([&] {
47-
refresh_commitComponents();
45+
RefreshCommitComponents();
4846
selectedCommit = std::min(selectedCommit, visibleCommitViewMap.size() - 1);
49-
// TODO check for promotion & pass information if needed
47+
// check for promotion & gray-out branch-colors if needed
5048
if (inPromotionSelection && promotionBranch.size() != 0) {
5149
std::unordered_map<std::string, Color> promotionBranchColorMap{};
52-
for (auto& [str, col] : branchColorMap) {
50+
for (const auto& [str, col] : branchColorMap) {
5351
if (str == promotionBranch) {
5452
promotionBranchColorMap.insert({str, col});
5553
} else {
@@ -74,7 +72,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> sta
7472
}
7573
if ((!inPromotionSelection && event == Event::ArrowDown) ||
7674
(event.is_mouse() && event.mouse().button == Mouse::WheelDown)) {
77-
selectedCommit = std::min(selectedCommit + 1, getVisibleCommitViewMap().size() - 1);
75+
selectedCommit = std::min(selectedCommit + 1, GetVisibleCommitViewMap().size() - 1);
7876
adjustScrollToSelectedCommit();
7977
return true;
8078
}
@@ -84,7 +82,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> sta
8482
// INTERCHANGEABLE VIEW
8583
// info
8684
infoView = Renderer([&] {
87-
visibleCommitViewMap = parseVisibleCommitMap(ostreeRepo, visibleBranches);
85+
parseVisibleCommitMap();
8886
if (visibleCommitViewMap.size() <= 0) {
8987
return text(" no commit info available ") | color(Color::RedLight) | bold | center;
9088
}
@@ -94,34 +92,28 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> sta
9492

9593
// filter
9694
filterManager =
97-
std::unique_ptr<BranchBoxManager>(new BranchBoxManager(ostreeRepo, visibleBranches));
95+
std::unique_ptr<BranchBoxManager>(new BranchBoxManager(*this, ostreeRepo, visibleBranches));
9896
filterView =
9997
Renderer(filterManager->branchBoxes, [&] { return filterManager->branchBoxRender(); });
100-
filterView = CatchEvent(filterView, [&](Event event) {
101-
if (event.is_mouse() && event.mouse().button == Mouse::Button::Left) {
102-
refresh_commitListComoponent();
103-
}
104-
return false;
105-
});
10698

10799
// interchangeable view (composed)
108100
manager = std::unique_ptr<Manager>(new Manager(*this, infoView, filterView));
109101
managerRenderer = manager->getManagerRenderer();
110102

111103
// FOOTER
112-
footerRenderer = Renderer([&] { return footer.footerRender(); });
104+
FooterRenderer = Renderer([&] { return footer.FooterRender(); });
113105

114106
// BUILD MAIN CONTAINER
115107
container = Component(managerRenderer);
116108
container = ResizableSplitLeft(commitListComponent, container, &logSize);
117-
container = ResizableSplitBottom(footerRenderer, container, &footerSize);
109+
container = ResizableSplitBottom(FooterRenderer, container, &footerSize);
118110

119111
commitListComponent->TakeFocus();
120112

121113
// add application shortcuts
122114
mainContainer = CatchEvent(container | border, [&](const Event& event) {
123115
if (event == Event::AltP) {
124-
setPromotionMode(true, visibleCommitViewMap.at(selectedCommit));
116+
SetPromotionMode(true, visibleCommitViewMap.at(selectedCommit));
125117
}
126118
// copy commit id
127119
if (event == Event::AltC) {
@@ -132,7 +124,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> sta
132124
}
133125
// refresh repository
134126
if (event == Event::AltR) {
135-
refresh_repository();
127+
RefreshOSTreeRepository();
136128
notificationText = " Refreshed Repository Data ";
137129
return true;
138130
}
@@ -151,7 +143,7 @@ OSTreeTUI::OSTreeTUI(const std::string& repo, const std::vector<std::string> sta
151143
});
152144
}
153145

154-
int OSTreeTUI::run() {
146+
int OSTreeTUI::Run() {
155147
using namespace ftxui;
156148
// footer notification update loader
157149
// Probably not the best solution, having an active wait and should maybe
@@ -162,12 +154,12 @@ int OSTreeTUI::run() {
162154
using namespace std::chrono_literals;
163155
// notification is set
164156
if (notificationText != "") {
165-
footer.setContent(notificationText);
157+
footer.SetContent(notificationText);
166158
screen.Post(Event::Custom);
167159
std::this_thread::sleep_for(2s);
168160
// clear notification
169161
notificationText = "";
170-
footer.resetContent();
162+
footer.ResetContent();
171163
screen.Post(Event::Custom);
172164
}
173165
std::this_thread::sleep_for(0.2s);
@@ -181,12 +173,12 @@ int OSTreeTUI::run() {
181173
return EXIT_SUCCESS;
182174
}
183175

184-
void OSTreeTUI::refresh_commitComponents() {
176+
void OSTreeTUI::RefreshCommitComponents() {
185177
using namespace ftxui;
186178

187179
commitComponents.clear();
188180
int i{0};
189-
visibleCommitViewMap = parseVisibleCommitMap(ostreeRepo, visibleBranches);
181+
parseVisibleCommitMap();
190182
for (auto& hash : visibleCommitViewMap) {
191183
commitComponents.push_back(CommitRender::CommitComponent(i, hash, *this));
192184
i++;
@@ -198,22 +190,24 @@ void OSTreeTUI::refresh_commitComponents() {
198190
: Container::Stacked(commitComponents);
199191
}
200192

201-
void OSTreeTUI::refresh_commitListComoponent() {
193+
void OSTreeTUI::RefreshCommitListComponent() {
202194
using namespace ftxui;
203195

196+
parseVisibleCommitMap();
197+
204198
commitListComponent->DetachAllChildren();
205-
refresh_commitComponents();
199+
RefreshCommitComponents();
206200
Component tmp = Container::Horizontal({tree, commitList});
207201
commitListComponent->Add(tmp);
208202
}
209203

210-
bool OSTreeTUI::refresh_repository() {
204+
bool OSTreeTUI::RefreshOSTreeRepository() {
211205
ostreeRepo.updateData();
212-
refresh_commitListComoponent();
206+
RefreshCommitListComponent();
213207
return true;
214208
}
215209

216-
bool OSTreeTUI::setPromotionMode(bool active, std::string hash, bool setPromotionBranch) {
210+
bool OSTreeTUI::SetPromotionMode(bool active, const std::string& hash, bool SetPromotionBranch) {
217211
// deactivate promotion mode
218212
if (!active) {
219213
inPromotionSelection = false;
@@ -224,7 +218,7 @@ bool OSTreeTUI::setPromotionMode(bool active, std::string hash, bool setPromotio
224218
// set promotion mode
225219
if (!inPromotionSelection || hash != promotionHash) {
226220
inPromotionSelection = true;
227-
if (setPromotionBranch) {
221+
if (SetPromotionBranch) {
228222
promotionBranch = promotionBranch.empty() ? columnToBranchMap.at(0) : promotionBranch;
229223
}
230224
promotionHash = hash;
@@ -234,14 +228,14 @@ bool OSTreeTUI::setPromotionMode(bool active, std::string hash, bool setPromotio
234228
return false;
235229
}
236230

237-
bool OSTreeTUI::promoteCommit(std::string hash,
238-
std::string branch,
239-
std::vector<std::string> metadataStrings,
240-
std::string newSubject,
231+
bool OSTreeTUI::PromoteCommit(const std::string& hash,
232+
const std::string& targetBranch,
233+
const std::vector<std::string>& metadataStrings,
234+
const std::string& newSubject,
241235
bool keepMetadata) {
242236
bool success =
243-
ostreeRepo.promoteCommit(hash, branch, metadataStrings, newSubject, keepMetadata);
244-
setPromotionMode(false);
237+
ostreeRepo.PromoteCommit(hash, targetBranch, metadataStrings, newSubject, keepMetadata);
238+
SetPromotionMode(false);
245239
// reload repository
246240
if (success) {
247241
scrollOffset = 0;
@@ -251,25 +245,20 @@ bool OSTreeTUI::promoteCommit(std::string hash,
251245
return success;
252246
}
253247

254-
std::vector<std::string> OSTreeTUI::parseVisibleCommitMap(
255-
cpplibostree::OSTreeRepo& repo,
256-
std::unordered_map<std::string, bool>& visibleBranches) {
257-
std::vector<std::string> visibleCommitViewMap{};
248+
void OSTreeTUI::parseVisibleCommitMap() {
258249
// get filtered commits
259250
visibleCommitViewMap = {};
260-
for (const auto& commitPair : repo.getCommitList()) {
251+
for (const auto& commitPair : ostreeRepo.getCommitList()) {
261252
if (visibleBranches[commitPair.second.branch]) {
262253
visibleCommitViewMap.push_back(commitPair.first);
263254
}
264255
}
265256
// sort by date
266257
std::sort(visibleCommitViewMap.begin(), visibleCommitViewMap.end(),
267258
[&](const std::string& a, const std::string& b) {
268-
return repo.getCommitList().at(a).timestamp >
269-
repo.getCommitList().at(b).timestamp;
259+
return ostreeRepo.getCommitList().at(a).timestamp >
260+
ostreeRepo.getCommitList().at(b).timestamp;
270261
});
271-
272-
return visibleCommitViewMap;
273262
}
274263

275264
void OSTreeTUI::adjustScrollToSelectedCommit() {
@@ -287,61 +276,61 @@ void OSTreeTUI::adjustScrollToSelectedCommit() {
287276
}
288277

289278
// SETTER & non-const GETTER
290-
void OSTreeTUI::setPromotionBranch(std::string promotionBranch) {
279+
void OSTreeTUI::SetPromotionBranch(const std::string& promotionBranch) {
291280
this->promotionBranch = promotionBranch;
292281
}
293282

294-
void OSTreeTUI::setSelectedCommit(size_t selectedCommit) {
283+
void OSTreeTUI::SetSelectedCommit(size_t selectedCommit) {
295284
this->selectedCommit = selectedCommit;
296285
adjustScrollToSelectedCommit();
297286
}
298287

299-
std::vector<std::string>& OSTreeTUI::getColumnToBranchMap() {
288+
std::vector<std::string>& OSTreeTUI::GetColumnToBranchMap() {
300289
return columnToBranchMap;
301290
}
302291

303-
ftxui::ScreenInteractive& OSTreeTUI::getScreen() {
292+
ftxui::ScreenInteractive& OSTreeTUI::GetScreen() {
304293
return screen;
305294
}
306295

307296
// GETTER
308-
const cpplibostree::OSTreeRepo& OSTreeTUI::getOstreeRepo() const {
297+
const cpplibostree::OSTreeRepo& OSTreeTUI::GetOstreeRepo() const {
309298
return ostreeRepo;
310299
}
311300

312-
const size_t& OSTreeTUI::getSelectedCommit() const {
301+
const size_t& OSTreeTUI::GetSelectedCommit() const {
313302
return selectedCommit;
314303
}
315304

316-
const std::string& OSTreeTUI::getPromotionBranch() const {
305+
const std::string& OSTreeTUI::GetPromotionBranch() const {
317306
return promotionBranch;
318307
}
319308

320-
const std::unordered_map<std::string, bool>& OSTreeTUI::getVisibleBranches() const {
309+
const std::unordered_map<std::string, bool>& OSTreeTUI::GetVisibleBranches() const {
321310
return visibleBranches;
322311
}
323312

324-
const std::vector<std::string>& OSTreeTUI::getColumnToBranchMap() const {
313+
const std::vector<std::string>& OSTreeTUI::GetColumnToBranchMap() const {
325314
return columnToBranchMap;
326315
}
327316

328-
const std::vector<std::string>& OSTreeTUI::getVisibleCommitViewMap() const {
317+
const std::vector<std::string>& OSTreeTUI::GetVisibleCommitViewMap() const {
329318
return visibleCommitViewMap;
330319
}
331320

332-
const std::unordered_map<std::string, ftxui::Color>& OSTreeTUI::getBranchColorMap() const {
321+
const std::unordered_map<std::string, ftxui::Color>& OSTreeTUI::GetBranchColorMap() const {
333322
return branchColorMap;
334323
}
335324

336-
const int& OSTreeTUI::getScrollOffset() const {
325+
int OSTreeTUI::GetScrollOffset() const {
337326
return scrollOffset;
338327
}
339328

340-
const bool& OSTreeTUI::getInPromotionSelection() const {
329+
bool OSTreeTUI::GetInPromotionSelection() const {
341330
return inPromotionSelection;
342331
}
343332

344-
const std::string& OSTreeTUI::getPromotionHash() const {
333+
const std::string& OSTreeTUI::GetPromotionHash() const {
345334
return promotionHash;
346335
}
347336

@@ -393,7 +382,7 @@ int OSTreeTUI::showHelp(const std::string& caller, const std::string& errorMessa
393382
int OSTreeTUI::showVersion() {
394383
using namespace ftxui;
395384

396-
auto versionText = text("ostree-tui 0.2.1");
385+
auto versionText = text("ostree-tui 0.3.0");
397386

398387
auto screen = Screen::Create(Dimension::Fit(versionText));
399388
Render(screen, versionText);

0 commit comments

Comments
 (0)