Skip to content

Commit 4356f4e

Browse files
committed
add colours to status subcommand
1 parent f928517 commit 4356f4e

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

src/subcommand/log_subcommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void print_commit(const commit_wrapper& commit, std::string m_format_flag)
5454
signature_wrapper author = signature_wrapper::get_commit_author(commit);
5555
signature_wrapper committer = signature_wrapper::get_commit_committer(commit);
5656

57-
std::cout << "\033[0;33m" << "commit " << buf << "\033[0m" << std::endl;
57+
std::cout << message_colour.at("yellow") << "commit " << buf << message_colour.at("colour_close") << std::endl;
5858
if (m_format_flag=="fuller")
5959
{
6060
std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl;

src/subcommand/status_subcommand.cpp

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ struct status_messages
4545
const std::map<git_status_t, status_messages> status_msg_map = //TODO : check spaces in short_mod
4646
{
4747
{ 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:"} },
48+
{ GIT_STATUS_INDEX_NEW, {"A ", "\tnew file:"} },
49+
{ GIT_STATUS_INDEX_MODIFIED, {"M ", "\tmodified:"} },
50+
{ GIT_STATUS_INDEX_DELETED, {"D ", "\tdeleted:"} },
51+
{ GIT_STATUS_INDEX_RENAMED, {"R ", "\trenamed:"} },
52+
{ GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\ttypechange:"} },
5353
{ 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:"} },
54+
{ GIT_STATUS_WT_MODIFIED, {" M " , "\tmodified:"} },
55+
{ GIT_STATUS_WT_DELETED, {" D ", "\tdeleted:"} },
56+
{ GIT_STATUS_WT_TYPECHANGE, {" T ", "\ttypechange:"} },
57+
{ GIT_STATUS_WT_RENAMED, {" R ", "\trenamed:"} },
5858
{ GIT_STATUS_WT_UNREADABLE, {"", ""} },
5959
{ GIT_STATUS_IGNORED, {"!! ", ""} },
6060
{ GIT_STATUS_CONFLICTED, {"", ""} },
@@ -73,7 +73,7 @@ struct print_entry
7373
std::string item;
7474
};
7575

76-
std::string get_print_status(git_status_t status, output_format of)
76+
std::string get_print_status(git_status_t status, output_format of) //TODO: add colours, but depends on the status, so needs another parameter
7777
{
7878
std::string entry_status;
7979
if ((of == output_format::DEFAULT) || (of == output_format::LONG))
@@ -100,7 +100,7 @@ void update_tracked_dir_set(const char* old_path, const char* new_path, std::set
100100
}
101101
}
102102

103-
std::string get_print_item(const char* old_path, const char* new_path)
103+
std::string get_print_item(const char* old_path, const char* new_path) //TODO: add colours, but depends on the status, so needs another parameter
104104
{
105105
std::string entry_item;
106106
if (old_path && new_path && std::strcmp(old_path, new_path))
@@ -139,24 +139,27 @@ std::vector<print_entry> get_entries_to_print(git_status_t status, status_list_w
139139
return entries_to_print;
140140
}
141141

142-
void print_entries(std::vector<print_entry> entries_to_print)
142+
void print_entries(std::vector<print_entry> entries_to_print, bool is_long, std::string colour)
143143
{
144144
for (auto e: entries_to_print)
145145
{
146-
std::cout << e.status << e.item << std::endl;
146+
if (is_long)
147+
std::cout << colour << e.status << e.item << message_colour.at("colour_close") << std::endl;
148+
else
149+
std::cout << colour << e.status << message_colour.at("colour_close") << e.item << std::endl;
147150
}
148151
}
149152

150153
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)
154+
std::set<std::string>& untracked_dir_set, bool is_long, std::string colour)
152155
{
153156
std::vector<print_entry> not_tracked_entries_to_print{};
154157
for (auto e: entries_to_print)
155158
{
156159
const size_t first_slash_idx = e.item.find('/');
157160
if (std::string::npos != first_slash_idx)
158161
{
159-
auto directory = e.item.substr(0, first_slash_idx);
162+
auto directory = e.item.substr(0, first_slash_idx) + "/";
160163
if (tracked_dir_set.contains(directory))
161164
{
162165
not_tracked_entries_to_print.push_back(e);
@@ -177,7 +180,7 @@ void print_not_tracked(const std::vector<print_entry>& entries_to_print, const s
177180
not_tracked_entries_to_print.push_back(e);
178181
}
179182
}
180-
print_entries(not_tracked_entries_to_print);
183+
print_entries(not_tracked_entries_to_print, is_long, colour);
181184
}
182185

183186
void status_subcommand::run()
@@ -220,17 +223,19 @@ void status_subcommand::run()
220223
std::cout << "## " << branch_name << std::endl;
221224
}
222225
}
226+
223227
if (sl.has_tobecommited_header())
224228
{
229+
std::string colour = message_colour.at("green");
225230
if (is_long)
226231
{
227232
std::cout << tobecommited_header << std::endl;
228233
}
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));
234+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_NEW, sl, true, of, &tracked_dir_set), is_long, colour);
235+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_MODIFIED, sl, true, of, &tracked_dir_set), is_long, colour);
236+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_DELETED, sl, true, of, &tracked_dir_set), is_long, colour);
237+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_RENAMED, sl, true, of, &tracked_dir_set), is_long, colour);
238+
print_entries(get_entries_to_print(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of, &tracked_dir_set), is_long, colour);
234239
if (is_long)
235240
{
236241
std::cout << std::endl;
@@ -239,44 +244,46 @@ void status_subcommand::run()
239244

240245
if (sl.has_notstagged_header())
241246
{
247+
std::string colour = message_colour.at("red");
242248
if (is_long)
243249
{
244250
std::cout << notstagged_header << std::endl;
245251
}
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));
252+
print_entries(get_entries_to_print(GIT_STATUS_WT_MODIFIED, sl, false, of, &tracked_dir_set), is_long, colour);
253+
print_entries(get_entries_to_print(GIT_STATUS_WT_DELETED, sl, false, of, &tracked_dir_set), is_long, colour);
254+
print_entries(get_entries_to_print(GIT_STATUS_WT_TYPECHANGE, sl, false, of, &tracked_dir_set), is_long, colour);
255+
print_entries(get_entries_to_print(GIT_STATUS_WT_RENAMED, sl, false, of, &tracked_dir_set), is_long, colour);
250256
if (is_long)
251257
{
252258
std::cout << std::endl;
253259
}
254260
}
255261

256-
257262
if (sl.has_untracked_header())
258263
{
264+
std::string colour = message_colour.at("red");
259265
if (is_long)
260266
{
261267
std::cout << untracked_header << std::endl;
262268
}
263-
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set);
269+
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
264270
if (is_long)
265271
{
266272
std::cout << std::endl;
267273
}
268274
}
269275

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-
}
276+
// if (sl.has_ignored_header())
277+
// {
278+
// std::string colour = message_colour.at("red");
279+
// if (is_long)
280+
// {
281+
// std::cout << ignored_header << std::endl;
282+
// }
283+
// print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
284+
// if (is_long)
285+
// {
286+
// std::cout << std::endl;
287+
// }
288+
// }
282289
}

src/utils/common.hpp

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

3+
#include <map>
34
#include <string>
4-
#include <utility>
55
#include <vector>
66

77
#include <git2.h>
@@ -27,6 +27,14 @@ class libgit2_object : private noncopyable_nonmovable
2727
~libgit2_object();
2828
};
2929

30+
const std::map<std::string, std::string> message_colour =
31+
{
32+
{"red", "\033[0;31m"},
33+
{"green", "\033[0;32m"},
34+
{"yellow", "\033[0;33m"},
35+
{"colour_close","\033[0m"},
36+
};
37+
3038
std::string get_current_git_path();
3139

3240
class git_strarray_wrapper

0 commit comments

Comments
 (0)