Added no-color option

This commit is contained in:
zawz 2019-11-29 12:57:07 +01:00
parent 2973746acb
commit eb2a50ca95
6 changed files with 86 additions and 17 deletions

View file

@ -3,10 +3,19 @@
#include <ztd/color.hpp> #include <ztd/color.hpp>
#include <utility>
#include <string>
// Global constants // Global constants
extern const ztd::color no_color; extern const ztd::color no_color;
extern const ztd::color repo_color; extern const ztd::color repo_color;
extern const ztd::color aur_color; extern const ztd::color aur_color;
extern const ztd::color error_color; extern const ztd::color error_color;
extern const ztd::color new_version_color;
extern const ztd::color old_version_color;
extern const ztd::color neutral_version_color;
std::pair<std::string, std::string> color_diff(std::string const& str1, std::string const& str2);
#endif //COLORS_HPP #endif //COLORS_HPP

View file

@ -10,6 +10,7 @@ extern bool opt_aur;
//print //print
extern bool opt_pall; extern bool opt_pall;
extern bool opt_notitles; extern bool opt_notitles;
extern bool opt_nocolor;
extern bool opt_plist; extern bool opt_plist;
extern bool opt_plistraw; extern bool opt_plistraw;
extern bool opt_psizes; extern bool opt_psizes;

View file

@ -4,3 +4,52 @@ const ztd::color no_color(ztd::color::none);
const ztd::color repo_color(ztd::color::b_white); const ztd::color repo_color(ztd::color::b_white);
const ztd::color aur_color(ztd::color::b_cyan); const ztd::color aur_color(ztd::color::b_cyan);
const ztd::color error_color(ztd::color::b_red); const ztd::color error_color(ztd::color::b_red);
const ztd::color new_version_color(ztd::color::b_green);
const ztd::color old_version_color(ztd::color::b_red);
const ztd::color neutral_version_color(ztd::color::b_white);
std::string version_delimiters = ".-+_:";
static bool isDelim(const char c)
{
return version_delimiters.find(c) != std::string::npos;
}
std::pair<std::string, std::string> color_diff(std::string const& str1, std::string const& str2)
{
std::string r1, r2;
{
//CORE FUNCTION
unsigned int i=0, j=0;
while( i < str1.size() && i < str2.size() )
{
while( i < str1.size() && i < str2.size() && str1[i] == str2[i] && !isDelim(str1[i]) )
i++;
if( str1[i] != str2[i] ) //there is a difference
{
r1 += neutral_version_color;
r1 += str1.substr(0, j); //previous section
r1 += old_version_color;
r1 += str1.substr(j, str1.size() - j); //changed section and rest
r1 += no_color;
r2 += neutral_version_color;
r2 += str2.substr(0, j); //previous section
r2 += new_version_color;
r2 += str2.substr(j, str2.size() - j); //changed section and rest
r2 += no_color;
break;
}
i++;
j=i;
}
}
return std::make_pair(r1,r2);
}

View file

@ -9,6 +9,7 @@ bool opt_aur=false;
//print //print
bool opt_pall=false; bool opt_pall=false;
bool opt_notitles=false; bool opt_notitles=false;
bool opt_nocolor=false;
bool opt_plist=false; bool opt_plist=false;
bool opt_plistraw=false; bool opt_plistraw=false;
bool opt_psizes=false; bool opt_psizes=false;

View file

@ -26,6 +26,7 @@ void create_options()
options.add(ztd::option('i', "install-size", false, "Install size (repo only)")); options.add(ztd::option('i', "install-size", false, "Install size (repo only)"));
options.add(ztd::option('n', "net-size", false, "Net difference size (repo only)")); options.add(ztd::option('n', "net-size", false, "Net difference size (repo only)"));
options.add(ztd::option('k', "no-titles", false, "Don't print titles on -d -i and -n")); options.add(ztd::option('k', "no-titles", false, "Don't print titles on -d -i and -n"));
options.add(ztd::option('C', "no-color", false, "Don't print colors"));
options.add(ztd::option("\r [OPERATION]")); options.add(ztd::option("\r [OPERATION]"));
options.add(ztd::option('u', "update", false, "Update targeted repositories")); options.add(ztd::option('u', "update", false, "Update targeted repositories"));
options.add(ztd::option('y', "noconfirm", false, "Doesn't ask for confirmation")); options.add(ztd::option('y', "noconfirm", false, "Doesn't ask for confirmation"));
@ -57,6 +58,7 @@ void options_bool()
//print //print
opt_pall = options.find('p')->activated; opt_pall = options.find('p')->activated;
opt_notitles = options.find('k')->activated; opt_notitles = options.find('k')->activated;
opt_nocolor = options.find('C')->activated;
opt_plist = options.find('l')->activated; opt_plist = options.find('l')->activated;
opt_plistraw = options.find('L')->activated; opt_plistraw = options.find('L')->activated;
opt_psizes = options.find('s')->activated; opt_psizes = options.find('s')->activated;

View file

@ -34,40 +34,46 @@ std::pair<double, const char*> convertHReadable(const long int size)
return std::make_pair( ret, size_suffixes[pow1k] ); return std::make_pair( ret, size_suffixes[pow1k] );
} }
static const char* p_color(const ztd::color c)
{
if(!opt_nocolor)
return c.code();
else
return "";
}
void print_update(repo_update& ru, ztd::color color, bool dlsize, bool nisize, bool nusize) void print_update(repo_update& ru, ztd::color color, bool dlsize, bool nisize, bool nusize)
{ {
if(ru.packages.size() > 0) if(ru.packages.size() > 0)
{ {
printf("%s[%s] %lu updates%s\n", color.code(), ru.name.c_str(), ru.packages.size(), no_color.code()); printf("%s[%s] %lu updates%s\n", p_color(color), ru.name.c_str(), ru.packages.size(), p_color(no_color) );
for(auto it : ru.packages) for(auto it : ru.packages)
{ {
std::string v1; std::string v1;
std::string v2; std::string v2;
if(ztd::sh("command -v zdiffcolor").size() > 0) if(!opt_nocolor)
{ {
std::string command="zdiffcolor -n b_white -c b_red -s b_green " + it.current_version + ' ' + it.new_version; auto pv = color_diff(it.current_version, it.new_version);
std::string str=ztd::sh(command); v1 = pv.first;
auto newline = str.find('\n'); v2 = pv.second;
v1=str.substr(0, newline);
newline++;
v2=str.substr(newline, str.size()-newline-1);
} }
else else
{ {
ztd::color v1c = ztd::color::b_red, v2c = ztd::color::b_green, noc = ztd::color::none; v1 = it.current_version;
v1 = v1c.code() + it.current_version + noc.code(); v2 = it.new_version;
v2 = v2c.code() + it.new_version + noc.code();
} }
printf(" %*s %*s -> %*s | ", -1*(ru.name_max_length + 2), it.name.c_str(), -1*(ru.vcur_max_length + 20), v1.c_str(), -1*(ru.vnew_max_length + 20), v2.c_str()); int c_padsize = (opt_nocolor ? 0 : 20) ;
printf(" %*s %*s -> %*s | ", -1*(ru.name_max_length + 2), it.name.c_str(), -1*(ru.vcur_max_length + c_padsize) , v1.c_str(), -1*(ru.vnew_max_length + c_padsize) , v2.c_str());
if(dlsize) if(dlsize)
print_size(it.download_size, true, "", 0, ztd::color::none, 2, size_index, " : "); print_size(it.download_size, true, "", 0, ztd::color::none, 2, size_index, " : ");
if(nusize) if(nusize)
print_size(it.net_size, true, "", 0, ztd::color::none, 2, size_index, ""); print_size(it.net_size, true, "", 0, ztd::color::none, 2, size_index, "");
printf("\n"); printf("\n");
} }
std::cout << color; std::cout << p_color(color);
std::cout << "================================"; std::cout << "================================";
std::cout << no_color << std::endl; std::cout << p_color(no_color);
std::cout << std::endl;
} }
} }
@ -81,9 +87,10 @@ void print_update_sizes(repo_update& ru, ztd::color color, bool dlsize, bool nis
print_size(ru.net_size, !notitle, "Net Upgrade Size:", size_print_padding, color, 2, size_index); print_size(ru.net_size, !notitle, "Net Upgrade Size:", size_print_padding, color, 2, size_index);
if(!notitle && (dlsize || nisize || nusize)) if(!notitle && (dlsize || nisize || nusize))
{ {
std::cout << color; std::cout << p_color(color);
std::cout << "================================"; std::cout << "================================";
std::cout << no_color << std::endl; std::cout << p_color(no_color);
std::cout <<std::endl;
} }
} }
@ -92,7 +99,7 @@ void print_size(long int size, bool printTitle, std::string title, int padding,
auto tpair = convertN(size, sizepow); auto tpair = convertN(size, sizepow);
if( printTitle ) if( printTitle )
{ {
printf("%s%*s%s", color.code(), padding, title.c_str(), no_color.code()); printf("%s%*s%s", p_color(color), padding, title.c_str(), p_color(no_color) );
} }
unsigned int sizepad=precision+5; unsigned int sizepad=precision+5;
if(sizepow == 0) if(sizepow == 0)