Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add colours to status subcommand
  • Loading branch information
SandrineP committed Sep 1, 2025
commit 4356f4e8674ad0b49cf40de1b2c783589315db58
2 changes: 1 addition & 1 deletion src/subcommand/log_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void print_commit(const commit_wrapper& commit, std::string m_format_flag)
signature_wrapper author = signature_wrapper::get_commit_author(commit);
signature_wrapper committer = signature_wrapper::get_commit_committer(commit);

std::cout << "\033[0;33m" << "commit " << buf << "\033[0m" << std::endl;
std::cout << message_colour.at("yellow") << "commit " << buf << message_colour.at("colour_close") << std::endl;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see use of colour rather than color!

if (m_format_flag=="fuller")
{
std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl;
Expand Down
85 changes: 46 additions & 39 deletions src/subcommand/status_subcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ struct status_messages
const std::map<git_status_t, status_messages> status_msg_map = //TODO : check spaces in short_mod
{
{ GIT_STATUS_CURRENT, {"", ""} },
{ GIT_STATUS_INDEX_NEW, {"A ", "\t new file:"} },
{ GIT_STATUS_INDEX_MODIFIED, {"M ", "\t modified:"} },
{ GIT_STATUS_INDEX_DELETED, {"D ", "\t deleted:"} },
{ GIT_STATUS_INDEX_RENAMED, {"R ", "\t renamed:"} },
{ GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\t typechange:"} },
{ GIT_STATUS_INDEX_NEW, {"A ", "\tnew file:"} },
{ GIT_STATUS_INDEX_MODIFIED, {"M ", "\tmodified:"} },
{ GIT_STATUS_INDEX_DELETED, {"D ", "\tdeleted:"} },
{ GIT_STATUS_INDEX_RENAMED, {"R ", "\trenamed:"} },
{ GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\ttypechange:"} },
{ GIT_STATUS_WT_NEW, {"?? ", ""} },
{ GIT_STATUS_WT_MODIFIED, {" M " , "\t modified:"} },
{ GIT_STATUS_WT_DELETED, {" D ", "\t deleted:"} },
{ GIT_STATUS_WT_TYPECHANGE, {" T ", "\t typechange:"} },
{ GIT_STATUS_WT_RENAMED, {" R ", "\t renamed:"} },
{ GIT_STATUS_WT_MODIFIED, {" M " , "\tmodified:"} },
{ GIT_STATUS_WT_DELETED, {" D ", "\tdeleted:"} },
{ GIT_STATUS_WT_TYPECHANGE, {" T ", "\ttypechange:"} },
{ GIT_STATUS_WT_RENAMED, {" R ", "\trenamed:"} },
{ GIT_STATUS_WT_UNREADABLE, {"", ""} },
{ GIT_STATUS_IGNORED, {"!! ", ""} },
{ GIT_STATUS_CONFLICTED, {"", ""} },
Expand All @@ -73,7 +73,7 @@ struct print_entry
std::string item;
};

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

std::string get_print_item(const char* old_path, const char* new_path)
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
{
std::string entry_item;
if (old_path && new_path && std::strcmp(old_path, new_path))
Expand Down Expand Up @@ -139,24 +139,27 @@ std::vector<print_entry> get_entries_to_print(git_status_t status, status_list_w
return entries_to_print;
}

void print_entries(std::vector<print_entry> entries_to_print)
void print_entries(std::vector<print_entry> entries_to_print, bool is_long, std::string colour)
{
for (auto e: entries_to_print)
{
std::cout << e.status << e.item << std::endl;
if (is_long)
std::cout << colour << e.status << e.item << message_colour.at("colour_close") << std::endl;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this line be indented a few spaces? And the line 2 below this.

else
std::cout << colour << e.status << message_colour.at("colour_close") << e.item << std::endl;
}
}

void print_not_tracked(const std::vector<print_entry>& entries_to_print, const std::set<std::string>& tracked_dir_set,
std::set<std::string>& untracked_dir_set)
std::set<std::string>& untracked_dir_set, bool is_long, std::string colour)
{
std::vector<print_entry> not_tracked_entries_to_print{};
for (auto e: entries_to_print)
{
const size_t first_slash_idx = e.item.find('/');
if (std::string::npos != first_slash_idx)
{
auto directory = e.item.substr(0, first_slash_idx);
auto directory = e.item.substr(0, first_slash_idx) + "/";
if (tracked_dir_set.contains(directory))
{
not_tracked_entries_to_print.push_back(e);
Expand All @@ -177,7 +180,7 @@ void print_not_tracked(const std::vector<print_entry>& entries_to_print, const s
not_tracked_entries_to_print.push_back(e);
}
}
print_entries(not_tracked_entries_to_print);
print_entries(not_tracked_entries_to_print, is_long, colour);
}

void status_subcommand::run()
Expand Down Expand Up @@ -220,17 +223,19 @@ void status_subcommand::run()
std::cout << "## " << branch_name << std::endl;
}
}

if (sl.has_tobecommited_header())
{
std::string colour = message_colour.at("green");
if (is_long)
{
std::cout << tobecommited_header << std::endl;
}
print_entries(get_entries_to_print(GIT_STATUS_INDEX_NEW, sl, true, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_INDEX_MODIFIED, sl, true, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_INDEX_DELETED, sl, true, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_INDEX_RENAMED, sl, true, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_INDEX_NEW, sl, true, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_INDEX_MODIFIED, sl, true, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_INDEX_DELETED, sl, true, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_INDEX_RENAMED, sl, true, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of, &tracked_dir_set), is_long, colour);
if (is_long)
{
std::cout << std::endl;
Expand All @@ -239,44 +244,46 @@ void status_subcommand::run()

if (sl.has_notstagged_header())
{
std::string colour = message_colour.at("red");
if (is_long)
{
std::cout << notstagged_header << std::endl;
}
print_entries(get_entries_to_print(GIT_STATUS_WT_MODIFIED, sl, false, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_WT_DELETED, sl, false, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_WT_TYPECHANGE, sl, false, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_WT_RENAMED, sl, false, of, &tracked_dir_set));
print_entries(get_entries_to_print(GIT_STATUS_WT_MODIFIED, sl, false, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_WT_DELETED, sl, false, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_WT_TYPECHANGE, sl, false, of, &tracked_dir_set), is_long, colour);
print_entries(get_entries_to_print(GIT_STATUS_WT_RENAMED, sl, false, of, &tracked_dir_set), is_long, colour);
if (is_long)
{
std::cout << std::endl;
}
}


if (sl.has_untracked_header())
{
std::string colour = message_colour.at("red");
if (is_long)
{
std::cout << untracked_header << std::endl;
}
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set);
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
if (is_long)
{
std::cout << std::endl;
}
}

if (sl.has_ignored_header())
{
if (is_long)
{
std::cout << ignored_header << std::endl;
}
print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set);
if (is_long)
{
std::cout << std::endl;
}
}
// if (sl.has_ignored_header())
// {
// std::string colour = message_colour.at("red");
// if (is_long)
// {
// std::cout << ignored_header << std::endl;
// }
// print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set, is_long, colour);
// if (is_long)
// {
// std::cout << std::endl;
// }
// }
}
10 changes: 9 additions & 1 deletion src/utils/common.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <map>
#include <string>
#include <utility>
#include <vector>

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

const std::map<std::string, std::string> message_colour =
{
{"red", "\033[0;31m"},
{"green", "\033[0;32m"},
{"yellow", "\033[0;33m"},
{"colour_close","\033[0m"},
};

std::string get_current_git_path();

class git_strarray_wrapper
Expand Down