Skip to content

Commit a930ee2

Browse files
authored
Add colours to status subcommand (#41)
* add colours to status subcommand * address review comment * remove useless comments * switch to use termcolor * fix log colour * remove useless include * remove useleess space
1 parent f928517 commit a930ee2

File tree

6 files changed

+64
-44
lines changed

6 files changed

+64
-44
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ message(STATUS "Building git2cpp v${CMAKE_PROJECT_VERSION}")
3333
# ============
3434

3535
find_package(libgit2)
36+
find_package(termcolor)
3637
# CLI11 is a single header, not packaged for cmake
3738

3839
# Build
@@ -86,4 +87,4 @@ set(GIT2CPP_SRC
8687
)
8788

8889
add_executable(git2cpp ${GIT2CPP_SRC})
89-
target_link_libraries(git2cpp PRIVATE libgit2::libgit2package)
90+
target_link_libraries(git2cpp PRIVATE libgit2::libgit2package termcolor::termcolor)

dev-environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ dependencies:
88
- pkg-config
99
- python
1010
- pytest
11+
- termcolor-cpp
1112
# Missing dependency from libgit2
1213
- zlib

src/subcommand/log_subcommand.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <git2/types.h>
55
#include <string_view>
66

7+
#include <termcolor/termcolor.hpp>
8+
79
#include "log_subcommand.hpp"
810
#include "../wrapper/repository_wrapper.hpp"
911
#include "../wrapper/commit_wrapper.hpp"
@@ -54,7 +56,8 @@ void print_commit(const commit_wrapper& commit, std::string m_format_flag)
5456
signature_wrapper author = signature_wrapper::get_commit_author(commit);
5557
signature_wrapper committer = signature_wrapper::get_commit_committer(commit);
5658

57-
std::cout << "\033[0;33m" << "commit " << buf << "\033[0m" << std::endl;
59+
stream_colour_fn colour = termcolor::yellow;
60+
std::cout << colour << "commit " << buf << termcolor::reset << std::endl;
5861
if (m_format_flag=="fuller")
5962
{
6063
std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl;

src/subcommand/status_subcommand.cpp

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55

66
#include <git2.h>
7+
#include <termcolor/termcolor.hpp>
78

89
#include "status_subcommand.hpp"
910
#include "../wrapper/status_wrapper.hpp"
@@ -45,16 +46,16 @@ struct status_messages
4546
const std::map<git_status_t, status_messages> status_msg_map = //TODO : check spaces in short_mod
4647
{
4748
{ GIT_STATUS_CURRENT, {"", ""} },
48-
{ GIT_STATUS_INDEX_NEW, {"A ", "\t new file:"} },
49-
{ GIT_STATUS_INDEX_MODIFIED, {"M ", "\t modified:"} },
50-
{ GIT_STATUS_INDEX_DELETED, {"D ", "\t deleted:"} },
51-
{ GIT_STATUS_INDEX_RENAMED, {"R ", "\t renamed:"} },
52-
{ GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\t typechange:"} },
49+
{ GIT_STATUS_INDEX_NEW, {"A ", "\tnew file:"} },
50+
{ GIT_STATUS_INDEX_MODIFIED, {"M ", "\tmodified:"} },
51+
{ GIT_STATUS_INDEX_DELETED, {"D ", "\tdeleted:"} },
52+
{ GIT_STATUS_INDEX_RENAMED, {"R ", "\trenamed:"} },
53+
{ GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\ttypechange:"} },
5354
{ GIT_STATUS_WT_NEW, {"?? ", ""} },
54-
{ GIT_STATUS_WT_MODIFIED, {" M " , "\t modified:"} },
55-
{ GIT_STATUS_WT_DELETED, {" D ", "\t deleted:"} },
56-
{ GIT_STATUS_WT_TYPECHANGE, {" T ", "\t typechange:"} },
57-
{ GIT_STATUS_WT_RENAMED, {" R ", "\t renamed:"} },
55+
{ GIT_STATUS_WT_MODIFIED, {" M " , "\tmodified:"} },
56+
{ GIT_STATUS_WT_DELETED, {" D ", "\tdeleted:"} },
57+
{ GIT_STATUS_WT_TYPECHANGE, {" T ", "\ttypechange:"} },
58+
{ GIT_STATUS_WT_RENAMED, {" R ", "\trenamed:"} },
5859
{ GIT_STATUS_WT_UNREADABLE, {"", ""} },
5960
{ GIT_STATUS_IGNORED, {"!! ", ""} },
6061
{ GIT_STATUS_CONFLICTED, {"", ""} },
@@ -78,7 +79,7 @@ std::string get_print_status(git_status_t status, output_format of)
7879
std::string entry_status;
7980
if ((of == output_format::DEFAULT) || (of == output_format::LONG))
8081
{
81-
entry_status = status_msg_map.at(status).long_mod + "\t";
82+
entry_status = status_msg_map.at(status).long_mod + " ";
8283
}
8384
else if (of == output_format::SHORT)
8485
{
@@ -139,24 +140,31 @@ std::vector<print_entry> get_entries_to_print(git_status_t status, status_list_w
139140
return entries_to_print;
140141
}
141142

142-
void print_entries(std::vector<print_entry> entries_to_print)
143+
void print_entries(std::vector<print_entry> entries_to_print, bool is_long, stream_colour_fn colour)
143144
{
144145
for (auto e: entries_to_print)
145146
{
146-
std::cout << e.status << e.item << std::endl;
147+
if (is_long)
148+
{
149+
std::cout << colour << e.status << e.item << termcolor::reset << std::endl;
150+
}
151+
else
152+
{
153+
std::cout << colour << e.status << termcolor::reset << e.item << std::endl;
154+
}
147155
}
148156
}
149157

150158
void print_not_tracked(const std::vector<print_entry>& entries_to_print, const std::set<std::string>& tracked_dir_set,
151-
std::set<std::string>& untracked_dir_set)
159+
std::set<std::string>& untracked_dir_set, bool is_long, stream_colour_fn colour)
152160
{
153161
std::vector<print_entry> not_tracked_entries_to_print{};
154162
for (auto e: entries_to_print)
155163
{
156164
const size_t first_slash_idx = e.item.find('/');
157165
if (std::string::npos != first_slash_idx)
158166
{
159-
auto directory = e.item.substr(0, first_slash_idx);
167+
auto directory = "\t" + e.item.substr(0, first_slash_idx) + "/";
160168
if (tracked_dir_set.contains(directory))
161169
{
162170
not_tracked_entries_to_print.push_back(e);
@@ -177,7 +185,7 @@ void print_not_tracked(const std::vector<print_entry>& entries_to_print, const s
177185
not_tracked_entries_to_print.push_back(e);
178186
}
179187
}
180-
print_entries(not_tracked_entries_to_print);
188+
print_entries(not_tracked_entries_to_print, is_long, colour);
181189
}
182190

183191
void status_subcommand::run()
@@ -220,17 +228,19 @@ void status_subcommand::run()
220228
std::cout << "## " << branch_name << std::endl;
221229
}
222230
}
231+
223232
if (sl.has_tobecommited_header())
224233
{
234+
stream_colour_fn colour = termcolor::green;
225235
if (is_long)
226236
{
227-
std::cout << tobecommited_header << std::endl;
237+
std::cout << tobecommited_header;
228238
}
229-
print_entries(get_entries_to_print(GIT_STATUS_INDEX_NEW, sl, true, of, &tracked_dir_set));
230-
print_entries(get_entries_to_print(GIT_STATUS_INDEX_MODIFIED, sl, true, of, &tracked_dir_set));
231-
print_entries(get_entries_to_print(GIT_STATUS_INDEX_DELETED, sl, true, of, &tracked_dir_set));
232-
print_entries(get_entries_to_print(GIT_STATUS_INDEX_RENAMED, sl, true, of, &tracked_dir_set));
233-
print_entries(get_entries_to_print(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of, &tracked_dir_set));
239+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_NEW, sl, true, of, &tracked_dir_set), is_long, colour);
240+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_MODIFIED, sl, true, of, &tracked_dir_set), is_long, colour);
241+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_DELETED, sl, true, of, &tracked_dir_set), is_long, colour);
242+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_RENAMED, sl, true, of, &tracked_dir_set), is_long, colour);
243+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of, &tracked_dir_set), is_long, colour);
234244
if (is_long)
235245
{
236246
std::cout << std::endl;
@@ -239,44 +249,46 @@ void status_subcommand::run()
239249

240250
if (sl.has_notstagged_header())
241251
{
252+
stream_colour_fn colour = termcolor::red;
242253
if (is_long)
243254
{
244-
std::cout << notstagged_header << std::endl;
255+
std::cout << notstagged_header;
245256
}
246-
print_entries(get_entries_to_print(GIT_STATUS_WT_MODIFIED, sl, false, of, &tracked_dir_set));
247-
print_entries(get_entries_to_print(GIT_STATUS_WT_DELETED, sl, false, of, &tracked_dir_set));
248-
print_entries(get_entries_to_print(GIT_STATUS_WT_TYPECHANGE, sl, false, of, &tracked_dir_set));
249-
print_entries(get_entries_to_print(GIT_STATUS_WT_RENAMED, sl, false, of, &tracked_dir_set));
257+
print_entries(get_entries_to_print(GIT_STATUS_WT_MODIFIED, sl, false, of, &tracked_dir_set), is_long, colour);
258+
print_entries(get_entries_to_print(GIT_STATUS_WT_DELETED, sl, false, of, &tracked_dir_set), is_long, colour);
259+
print_entries(get_entries_to_print(GIT_STATUS_WT_TYPECHANGE, sl, false, of, &tracked_dir_set), is_long, colour);
260+
print_entries(get_entries_to_print(GIT_STATUS_WT_RENAMED, sl, false, of, &tracked_dir_set), is_long, colour);
250261
if (is_long)
251262
{
252263
std::cout << std::endl;
253264
}
254265
}
255266

256-
257267
if (sl.has_untracked_header())
258268
{
269+
stream_colour_fn colour = termcolor::red;
259270
if (is_long)
260271
{
261-
std::cout << untracked_header << std::endl;
272+
std::cout << untracked_header;
262273
}
263-
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set);
274+
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
264275
if (is_long)
265276
{
266277
std::cout << std::endl;
267278
}
268279
}
269280

270-
if (sl.has_ignored_header())
271-
{
272-
if (is_long)
273-
{
274-
std::cout << ignored_header << std::endl;
275-
}
276-
print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set);
277-
if (is_long)
278-
{
279-
std::cout << std::endl;
280-
}
281-
}
281+
// if (sl.has_ignored_header())
282+
// {
283+
// stream_colour_fn colour = termcolor::red;
284+
// if (is_long)
285+
// {
286+
// std::cout << ignored_header;
287+
// }
288+
// print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
289+
// if (is_long)
290+
// {
291+
// std::cout << std::endl;
292+
// }
293+
// }
282294
}

src/utils/common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <filesystem>
2+
#include <iostream>
3+
#include <unistd.h>
24

35
#include <git2.h>
46

src/utils/common.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <string>
4-
#include <utility>
54
#include <vector>
65

76
#include <git2.h>
@@ -29,6 +28,8 @@ class libgit2_object : private noncopyable_nonmovable
2928

3029
std::string get_current_git_path();
3130

31+
using stream_colour_fn = std::ostream& (*)(std::ostream&);
32+
3233
class git_strarray_wrapper
3334
{
3435
public:

0 commit comments

Comments
 (0)