diff --git a/include/commands.h b/include/commands.h index 097d858..09c8ab0 100644 --- a/include/commands.h +++ b/include/commands.h @@ -10,22 +10,16 @@ #define AUR_UPDATE_COMMAND "yay -Sau" #define AUR_UPDATE_COMMAND_NOCONFIRM "yay -Sau --noconfirm" -#define PACMAN_EXT_INFO_COMMAND "pacman -Si --dbpath \"${TMPDIR:-/tmp}/checkup-db-${USER}/\" --logfile /dev/null " -#define PACMAN_LOCAL_INFO_COMMAND "pacman -Qi " - -#define PACMAN_EXT_SIZE_CUT_COMMAND " |grep -E 'Download Size|Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec" -#define PACMAN_LOCAL_SIZE_CUT_COMMAND " |grep 'Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec|tr -d '\n'" +#define PACMAN_EXT_SIZE_COMMAND "pacman -Si --dbpath \"${TMPDIR:-/tmp}/checkup-db-${USER}/\" --logfile /dev/null %s |grep -E 'Download Size|Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec" +#define PACMAN_LOCAL_SIZE_COMMAND "pacman -Qi %s |grep 'Installed Size'|cut -d':' -f2|tr -d ' '|cut -d'i' -f1 | tr -d 'B'|numfmt --from=iec|tr -d '\n'" // apt/dpkg #define APT_FETCH_COMMAND "sudo apt update >/dev/null 2>&1 || return $?\napt list --upgradable 2>/dev/null | tail -n +2 | awk -F \"/\" '{print $1\" \"$2}' | tr -d ']' | awk '{print $1\" \"$7\" -> \"$3}'" #define APT_UPDATE_COMMAND "sudo apt update && sudo apt upgrade" #define APT_UPDATE_COMMAND_NOCONFIRM "sudo apt update && sudo apt -y upgrade" -#define APT_EXT_INFO_COMMAND "apt show " -#define APT_LOCAL_INFO_COMMAND "dpkg -s " - -#define APT_EXT_SIZE_CUT_COMMAND " 2>/dev/null| grep -E 'Installed-Size:|Download-Size:' | cut -d' ' -f2- | tr -d ', B' | tr 'k' 'K' | numfmt --from=iec | awk '{s=$0;getline;s=$0\"\\n\"s;print s}'" -#define APT_LOCAL_SIZE_CUT_COMMAND " 2>/dev/null| grep 'Installed-Size:' | cut -d' ' -f2 | xargs echo '1024 *' | bc" +#define APT_EXT_SIZE_COMMAND "apt show %s 2>/dev/null| grep -E 'Installed-Size:|Download-Size:' | cut -d' ' -f2- | tr -d ', B' | tr 'k' 'K' | numfmt --from=iec | awk '{s=$0;getline;s=$0\"\\n\"s;print s}'" +#define APT_LOCAL_SIZE_COMMAND "dpkg -s %s 2>/dev/null| grep 'Installed-Size:' | cut -d' ' -f2 | xargs echo '1024 *' | bc" diff --git a/include/fetch.hpp b/include/fetch.hpp index 19a140b..47070c0 100644 --- a/include/fetch.hpp +++ b/include/fetch.hpp @@ -6,6 +6,6 @@ //functions int fetch_update(repo_update* r, const std::string& name, const std::string& command); -int import_sizes(repo_update* ru, const char* ext_info_command, const char* loc_info_command, const char* ext_cut_command, const char* loc_cut_command); +int import_sizes(repo_update* ru, const char* ext_size_command, const char* loc_size_command); #endif //FETCH_HPP diff --git a/include/print.hpp b/include/print.hpp index 5b2b1b2..d6a7af3 100644 --- a/include/print.hpp +++ b/include/print.hpp @@ -12,6 +12,9 @@ extern const char* size_suffixes[6]; extern const int size_print_padding; +//tool +std::string strpf(std::string const& format, std::string const& var); + //functions void help(); diff --git a/src/fetch.cpp b/src/fetch.cpp index f7bdb36..89e0147 100644 --- a/src/fetch.cpp +++ b/src/fetch.cpp @@ -8,6 +8,7 @@ #include #include "commands.h" +#include "print.hpp" //functions int fetch_update(repo_update* r, const std::string& name, const std::string& command) @@ -67,9 +68,9 @@ int fetch_update(repo_update* r, const std::string& name, const std::string& com -void get_ext_sizes(package_update* pkg, const char* info_command, const char* cut_command) +void get_ext_sizes(package_update* pkg, const char* command) { - std::string sizes=ztd::sh(info_command + pkg->name + cut_command); + std::string sizes=ztd::sh(strpf(command, pkg->name)); unsigned int i=0, j=0; while(sizes[i]!='\n') i++; @@ -80,24 +81,24 @@ void get_ext_sizes(package_update* pkg, const char* info_command, const char* cu i++; pkg->new_install_size = std::stoul(sizes.substr(j,i-j)); } -void get_loc_size(package_update* pkg, const char* info_command, const char* cut_command) +void get_loc_size(package_update* pkg, const char* command) { - std::string size=ztd::sh(info_command + pkg->name + cut_command); + std::string size=ztd::sh(strpf(command, pkg->name)); if(size.size() > 0) pkg->current_install_size = std::stoul(size); else pkg->current_install_size = 0; } -int import_sizes(repo_update* ru, const char* ext_info_command, const char* loc_info_command, const char* ext_cut_command, const char* loc_cut_command) +int import_sizes(repo_update* ru, const char* ext_size_command, const char* loc_size_command) { const unsigned int n=ru->packages.size(); #pragma omp parallel for for(unsigned int i=0; ipackages[i]); - get_ext_sizes(pkg, ext_info_command, ext_cut_command); - get_loc_size(pkg, loc_info_command, loc_cut_command); + get_ext_sizes(pkg, ext_size_command); + get_loc_size(pkg, loc_size_command); pkg->net_size = (long int) pkg->new_install_size - (long int) pkg->current_install_size; } ru->download_size = 0; diff --git a/src/package_man.cpp b/src/package_man.cpp index df0794f..b05aef3 100644 --- a/src/package_man.cpp +++ b/src/package_man.cpp @@ -71,7 +71,7 @@ int pacman_process(bool yay) //size fetch if( combine_size ) { - r = import_sizes(&repo, PACMAN_EXT_INFO_COMMAND, PACMAN_LOCAL_INFO_COMMAND, PACMAN_EXT_SIZE_CUT_COMMAND, PACMAN_LOCAL_SIZE_CUT_COMMAND); + r = import_sizes(&repo, PACMAN_EXT_SIZE_COMMAND, PACMAN_LOCAL_SIZE_COMMAND); } if(r!=0) return r; @@ -164,7 +164,7 @@ int apt_process() if( combine_size ) { - r = import_sizes(&repo, APT_EXT_INFO_COMMAND, APT_LOCAL_INFO_COMMAND, APT_EXT_SIZE_CUT_COMMAND, APT_LOCAL_SIZE_CUT_COMMAND); + r = import_sizes(&repo, APT_EXT_SIZE_COMMAND, APT_LOCAL_SIZE_COMMAND); } if(r!=0) return r; diff --git a/src/print.cpp b/src/print.cpp index 7ff5908..3683389 100644 --- a/src/print.cpp +++ b/src/print.cpp @@ -1,7 +1,7 @@ #include "print.hpp" #include -#include +#include #include @@ -11,6 +11,18 @@ const char* size_suffixes[6] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB"}; const int size_print_padding=-21; +//tool +std::string strpf(std::string const& format, std::string const& var) +{ + std::string ret; + size_t bufsize = format.size()-1 + var.size(); + char* buf = (char*) malloc( sizeof(char)*bufsize ); + snprintf(buf, bufsize, format.c_str(), var.c_str()); + ret = buf; + free(buf); + return ret; +} + //functions std::pair convertN(const long int size, unsigned int n) {