diff --git a/include/colors.hpp b/include/colors.hpp index b0dbc0c..6c66d6f 100644 --- a/include/colors.hpp +++ b/include/colors.hpp @@ -3,10 +3,19 @@ #include +#include +#include + // Global constants extern const ztd::color no_color; extern const ztd::color repo_color; extern const ztd::color aur_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 color_diff(std::string const& str1, std::string const& str2); + #endif //COLORS_HPP diff --git a/include/opt_bool.h b/include/opt_bool.h index 48fec5a..c26a899 100644 --- a/include/opt_bool.h +++ b/include/opt_bool.h @@ -10,6 +10,7 @@ extern bool opt_aur; //print extern bool opt_pall; extern bool opt_notitles; +extern bool opt_nocolor; extern bool opt_plist; extern bool opt_plistraw; extern bool opt_psizes; diff --git a/src/colors.cpp b/src/colors.cpp index 7b91c32..b2b3980 100644 --- a/src/colors.cpp +++ b/src/colors.cpp @@ -4,3 +4,52 @@ const ztd::color no_color(ztd::color::none); const ztd::color repo_color(ztd::color::b_white); const ztd::color aur_color(ztd::color::b_cyan); 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 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); + +} diff --git a/src/opt_bool.c b/src/opt_bool.c index 99a72ba..0d80d6e 100644 --- a/src/opt_bool.c +++ b/src/opt_bool.c @@ -9,6 +9,7 @@ bool opt_aur=false; //print bool opt_pall=false; bool opt_notitles=false; +bool opt_nocolor=false; bool opt_plist=false; bool opt_plistraw=false; bool opt_psizes=false; diff --git a/src/options.cpp b/src/options.cpp index 7b786d4..f580a02 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -26,6 +26,7 @@ void create_options() 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('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('u', "update", false, "Update targeted repositories")); options.add(ztd::option('y', "noconfirm", false, "Doesn't ask for confirmation")); @@ -57,6 +58,7 @@ void options_bool() //print opt_pall = options.find('p')->activated; opt_notitles = options.find('k')->activated; + opt_nocolor = options.find('C')->activated; opt_plist = options.find('l')->activated; opt_plistraw = options.find('L')->activated; opt_psizes = options.find('s')->activated; diff --git a/src/print.cpp b/src/print.cpp index 39b3a7f..e3aec3b 100644 --- a/src/print.cpp +++ b/src/print.cpp @@ -34,40 +34,46 @@ std::pair convertHReadable(const long int size) 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) { 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) { std::string v1; 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; - std::string str=ztd::sh(command); - auto newline = str.find('\n'); - v1=str.substr(0, newline); - newline++; - v2=str.substr(newline, str.size()-newline-1); + auto pv = color_diff(it.current_version, it.new_version); + v1 = pv.first; + v2 = pv.second; } else { - ztd::color v1c = ztd::color::b_red, v2c = ztd::color::b_green, noc = ztd::color::none; - v1 = v1c.code() + it.current_version + noc.code(); - v2 = v2c.code() + it.new_version + noc.code(); + v1 = it.current_version; + v2 = it.new_version; } - 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) print_size(it.download_size, true, "", 0, ztd::color::none, 2, size_index, " : "); if(nusize) print_size(it.net_size, true, "", 0, ztd::color::none, 2, size_index, ""); printf("\n"); } - std::cout << color; + std::cout << p_color(color); 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); if(!notitle && (dlsize || nisize || nusize)) { - std::cout << color; + std::cout << p_color(color); std::cout << "================================"; - std::cout << no_color << std::endl; + std::cout << p_color(no_color); + std::cout <