generated from ArthurSonzogni/ftxui-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
log.cpp
328 lines (276 loc) · 9.21 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "log.hpp"
#include <stdlib.h> // for EXIT_SUCCESS
#include <algorithm> // for copy
#include <ftxui/component/component.hpp> // for Horizontal, Renderer, Button, Menu, CatchEvent, Checkbox, Vertical
#include <ftxui/component/menu.hpp> // for MenuBase
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
#include <ftxui/dom/elements.hpp> // for operator|, text, separator, Element, size, xflex, vbox, color, yframe, hbox, bold, EQUAL, WIDTH, filler, Elements, nothing, yflex, bgcolor
#include <ftxui/screen/string.hpp> // for to_wstring, to_string
#include <functional> // for function
#include <iostream> // for basic_istream, char_traits
#include <iterator> // for istreambuf_iterator, operator!=
#include <map> // for map, map<>::mapped_type
#include <memory> // for allocator, shared_ptr, __shared_ptr_access, unique_ptr, make_unique
#include <queue> // for queue
#include <string> // for wstring, basic_string, string, getline, operator+, operator<, to_string
#include <utility> // for move
#include <vector> // for vector
#include "diff.hpp" // for File, Parse, Render
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/screen/color.hpp" // for Color, Color::Green, Color::Red, Color::Black, Color::White
#include "process.hpp" // for process
#include "scroller.hpp" // for Scroller
using namespace ftxui;
namespace gittui::log {
std::string ResolveHead() {
procxx::process git("git");
git.add_argument("rev-list");
git.add_argument("HEAD");
git.add_argument("-1");
git.exec();
std::string line;
std::getline(git.output(), line);
return line;
}
struct Commit {
std::wstring hash;
std::wstring title;
std::wstring tree;
std::vector<std::wstring> authors;
std::vector<std::wstring> body;
std::vector<std::wstring> committers;
std::vector<std::wstring> parents;
};
Commit* GetCommit(std::wstring hash) {
static std::map<std::wstring, std::unique_ptr<Commit>> g_commit;
if (g_commit[hash])
return g_commit[hash].get();
g_commit[hash] = std::make_unique<Commit>();
Commit* commit = g_commit[hash].get();
commit->hash = hash;
procxx::process git("git");
git.add_argument("cat-file");
git.add_argument("commit");
git.add_argument(to_string(hash));
git.exec();
std::string line;
while (std::getline(git.output(), line)) {
if (line.find("tree ", 0) == 0) {
commit->tree = to_wstring(line.substr(5));
continue;
}
if (line.find("parent", 0) == 0) {
commit->parents.push_back(to_wstring(line.substr(7)));
continue;
}
if (line.find("author", 0) == 0) {
commit->authors.push_back(to_wstring(line.substr(7)));
continue;
}
if (line.find("committer", 0) == 0) {
commit->committers.push_back(to_wstring(line.substr(10)));
continue;
}
if (line.empty()) {
int index = -1;
while (std::getline(git.output(), line)) {
++index;
if (index == 0)
commit->title = to_wstring(line);
if (index >= 2)
commit->body.push_back(to_wstring(line));
}
break;
}
}
return commit;
}
int main(int argc, const char** argv) {
auto screen = ScreenInteractive::Fullscreen();
(void)argc;
(void)argv;
std::vector<Commit*> commits;
std::vector<std::wstring> menu_commit_entries;
int menu_commit_index = 0;
std::queue<std::wstring> todo;
todo.push(to_wstring(ResolveHead()));
auto refresh_commit_list = [&] {
while (!todo.empty() && menu_commit_index > (int)commits.size() - 80) {
std::wstring hash = todo.front();
todo.pop();
Commit* commit = GetCommit(hash);
for (auto& parent : commit->parents)
todo.push(parent);
menu_commit_entries.push_back(commit->title);
commits.push_back(commit);
}
};
refresh_commit_list();
auto menu_commit = Menu(&menu_commit_entries, &menu_commit_index);
int hunk_size = 3;
bool split = true;
std::vector<diff::File> files;
std::vector<std::wstring> menu_files_entries;
int menu_files_index = 0;
auto refresh_files = [&] {
Commit* commit = commits[menu_commit_index];
files.clear();
menu_files_entries.clear();
procxx::process git("git");
git.add_argument("diff");
git.add_argument("-U" + std::to_string(hunk_size));
git.add_argument(to_string(commit->hash) + "~..." +
to_string(commit->hash));
git.exec();
std::string diff(std::istreambuf_iterator<char>(git.output()),
std::istreambuf_iterator<char>());
files = diff::Parse(diff);
menu_files_entries.push_back(L"description");
for (const auto& file : files)
menu_files_entries.push_back(file.right_file);
};
refresh_files();
auto menu_files = Menu(&menu_files_entries, &menu_files_index);
MenuBase::From(menu_commit)->on_change = [&] {
refresh_files();
menu_files_index = 0;
};
auto commit_renderer = Renderer([&] {
if (menu_files_index != 0 && menu_files_index - 1 < (int)files.size())
return diff::Render(files[menu_files_index - 1], split);
Commit* commit = commits[menu_commit_index];
Elements elements;
for (const auto& committer : commit->committers) {
elements.push_back(hbox({
text(L"committer:") | bold | color(Color::Green),
text(committer) | xflex,
}));
}
for (const auto& author : commit->authors) {
elements.push_back(hbox({
text(L" author:") | bold | color(Color::Green),
text(author) | xflex,
}));
}
elements.push_back(hbox({
text(L" hash:") | bold | color(Color::Green),
text(commit->hash) | xflex,
}));
for (const auto& parent : commit->parents) {
elements.push_back(hbox({
text(L" parent:") | bold | color(Color::Green),
text(parent) | xflex,
}));
}
elements.push_back(hbox({
text(L" tree:") | bold | color(Color::Green),
text(commit->tree) | xflex,
}));
elements.push_back(hbox({
text(L" title:") | bold | color(Color::Red),
text(commit->title) | xflex,
}));
elements.push_back(separator());
if (commit->body.size() != 0) {
for (auto& it : commit->body)
elements.push_back(text(it));
}
auto content = vbox(std::move(elements));
return content;
});
auto scroller = Scroller(commit_renderer);
auto increase_hunk = [&] {
hunk_size++;
refresh_files();
};
auto decrease_hunk = [&] {
if (hunk_size != 0)
hunk_size--;
refresh_files();
};
auto split_checkbox = Checkbox("[S]plit", &split);
auto button_increase_hunk = Button("[+1]", increase_hunk, false);
auto button_decrease_hunk = Button("[-1]", decrease_hunk, false);
auto button_quit = Button("[Q]uit", screen.ExitLoopClosure(), false);
auto options = Container::Horizontal({
split_checkbox,
button_decrease_hunk,
button_increase_hunk,
button_quit,
});
auto option_renderer = Renderer(options, [&] {
return hbox({
text(L"[git tui log]"),
filler(),
split_checkbox->Render(),
text(L" Context:"),
button_decrease_hunk->Render(),
text(to_wstring(hunk_size)),
button_increase_hunk->Render(),
filler(),
button_quit->Render(),
}) |
bgcolor(Color::White) | color(Color::Black);
});
menu_files = Renderer(menu_files, [menu_files] {
return vbox({
text(L"Commit"),
separator(),
menu_files->Render() | yframe,
});
});
menu_commit = Renderer(menu_commit, [menu_commit] {
return vbox({
text(L"Files"),
separator(),
menu_commit->Render() | yframe,
});
});
scroller = Renderer(scroller, [scroller] {
return vbox({
text(L"Content"),
separator(),
scroller->Render() | flex,
});
});
int menu_commit_width = 25;
int menu_files_width = 25;
auto container = scroller;
container = ResizableSplitLeft(menu_files, container, &menu_files_width);
container = ResizableSplitLeft(menu_commit, container, &menu_commit_width);
container =
Renderer(container, [container] { return container->Render() | flex; });
container = Container::Vertical({
option_renderer,
container,
});
container = CatchEvent(container, [&](Event event) {
refresh_commit_list();
if (event == Event::Character('s')) {
split = !split;
return true;
}
if (event == Event::Character('-')) {
decrease_hunk();
return true;
}
if (event == Event::Character('+')) {
increase_hunk();
return true;
}
if (event == Event::Character('q') || event == Event::Escape) {
screen.ExitLoopClosure()();
return true;
}
return false;
});
menu_commit->TakeFocus();
screen.Loop(container);
return EXIT_SUCCESS;
}
} // namespace gittui::log
// Copyright 2021 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.