FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add colours to status subcommand by SandrineP · Pull Request #41 · QuantStack/git2cpp · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (3) .hpp  (1) .txt  (1) .yml  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 2 additions & 1 deletion CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ message(STATUS "Building git2cpp v${CMAKE_PROJECT_VERSION}")
# ============

find_package(libgit2)
find_package(termcolor)
# CLI11 is a single header, not packaged for cmake

# Build
Expand Down Expand Up @@ -86,4 +87,4 @@ set(GIT2CPP_SRC
)

add_executable(git2cpp ${GIT2CPP_SRC})
target_link_libraries(git2cpp PRIVATE libgit2::libgit2package)
target_link_libraries(git2cpp PRIVATE libgit2::libgit2package termcolor::termcolor)
1 change: 1 addition & 0 deletions dev-environment.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ dependencies:
- pkg-config
- python
- pytest
- termcolor-cpp
# Missing dependency from libgit2
- zlib
5 changes: 4 additions & 1 deletion src/subcommand/log_subcommand.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <git2/types.h>
#include <string_view>

#include <termcolor/termcolor.hpp>

#include "log_subcommand.hpp"
#include "../wrapper/repository_wrapper.hpp"
#include "../wrapper/commit_wrapper.hpp"
Expand Down Expand Up @@ -54,7 +56,8 @@ 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;
stream_colour_fn colour = termcolor::yellow;
std::cout << colour << "commit " << buf << termcolor::reset << std::endl;
if (m_format_flag=="fuller")
{
std::cout << "Author:\t " << author.name() << " " << author.email() << std::endl;
Expand Down
94 changes: 53 additions & 41 deletions src/subcommand/status_subcommand.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>

#include <git2.h>
#include <termcolor/termcolor.hpp>

#include "status_subcommand.hpp"
#include "../wrapper/status_wrapper.hpp"
Expand Down Expand Up @@ -45,16 +46,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 @@ -78,7 +79,7 @@ std::string get_print_status(git_status_t status, output_format of)
std::string entry_status;
if ((of == output_format::DEFAULT) || (of == output_format::LONG))
{
entry_status = status_msg_map.at(status).long_mod + "\t";
entry_status = status_msg_map.at(status).long_mod + " ";
}
else if (of == output_format::SHORT)
{
Expand Down Expand Up @@ -139,24 +140,31 @@ 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, stream_colour_fn 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 << termcolor::reset << std::endl;
}
else
{
std::cout << colour << e.status << termcolor::reset << 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, stream_colour_fn 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 = "\t" + 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 +185,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 +228,19 @@ void status_subcommand::run()
std::cout << "## " << branch_name << std::endl;
}
}

if (sl.has_tobecommited_header())
{
stream_colour_fn colour = termcolor::green;
if (is_long)
{
std::cout << tobecommited_header << std::endl;
std::cout << tobecommited_header;
}
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 +249,46 @@ void status_subcommand::run()

if (sl.has_notstagged_header())
{
stream_colour_fn colour = termcolor::red;
if (is_long)
{
std::cout << notstagged_header << std::endl;
std::cout << notstagged_header;
}
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())
{
stream_colour_fn colour = termcolor::red;
if (is_long)
{
std::cout << untracked_header << std::endl;
std::cout << untracked_header;
}
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())
// {
// stream_colour_fn colour = termcolor::red;
// if (is_long)
// {
// std::cout << ignored_header;
// }
// 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;
// }
// }
}
2 changes: 2 additions & 0 deletions src/utils/common.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <filesystem>
#include <iostream>
#include <unistd.h>

#include <git2.h>

Expand Down
3 changes: 2 additions & 1 deletion src/utils/common.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

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

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

std::string get_current_git_path();

using stream_colour_fn = std::ostream& (*)(std::ostream&);

class git_strarray_wrapper
{
public:
Expand Down

Back | FazBrowse Home | New Git URL