From 60f500e3c701952675843c7d2bbd7f1a75495cac Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:55:30 +0300 Subject: [PATCH] Yes. --- include/config.hpp | 2 + include/taur.hpp | 6 +- include/util.hpp | 4 +- src/main.cpp | 140 ++++++++++++++++++++++++--------------------- src/taur.cpp | 17 +++--- src/util.cpp | 10 ++-- 6 files changed, 97 insertions(+), 82 deletions(-) diff --git a/include/config.hpp b/include/config.hpp index f37d7e0..ab4676d 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -131,6 +131,7 @@ blue = "#00aaff" cyan = "#00ffff" yellow = "#ffff00" magenta = "#ff11cc" +gray = "#5a5a5a" # custom DB colors such as extra, aur, etc. # Unfortunatly toml doesn't support things like `aur = blue` @@ -152,6 +153,7 @@ inline string blue = "#00aaff"; inline string cyan = "#00ffff"; inline string yellow = "#ffff00"; inline string magenta = "#ff11cc"; +inline string gray = "#5a5a5a"; inline string configfile; inline string themefile; diff --git a/include/taur.hpp b/include/taur.hpp index 1dc4498..bd2f289 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -1,6 +1,7 @@ #ifndef GIT_HPP #define GIT_HPP +#include #include #include #include @@ -24,6 +25,7 @@ struct TaurPkg_t { string desc; float popularity = 1; // normal vector depends; + bool installed = false; string db_name = "aur"; }; @@ -34,13 +36,13 @@ class TaurBackend { // They are different because we found that fetching each AUR pkg is very time consuming, so we store the name and look it up later. vector getPkgFromJson(rapidjson::Document& doc, bool useGit); vector search_pac(string query); - vector search(string query, bool useGit); + vector search(string query, bool useGit); bool download_tar(string url, string out_path); bool download_git(string url, string out_path); bool download_pkg(string url, string out_path); optional fetch_pkg(string pkg, bool returnGit); vector fetch_pkgs(vector pkgs, bool returnGit); - bool remove_pkg(string name, bool searchForeignPackages); + bool remove_pkg(alpm_list_t *name, bool searchForeignPackages); bool handle_aur_depends(TaurPkg_t pkg, path out_path, vector localPkgs, bool useGit); bool install_pkg(string pkg_name, string extracted_path, bool onlydownload); bool update_all_pkgs(string cacheDir, bool useGit); diff --git a/include/util.hpp b/include/util.hpp index cd35702..f0a4714 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -98,8 +98,10 @@ void log_printf(int log, string fmt, Args&&... args) { // could use fmt::join, but doesn't work with vector template void print_vec(std::vector vec) { - if(!config->debug) + if(!config->debug) { + std::cout << std::endl; return; + } for(auto& i : vec) std::cout << i << " "; diff --git a/src/main.cpp b/src/main.cpp index ac855e3..84c0a79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,6 +74,7 @@ void test_colors() { "A customizable and lightweight AUR helper, designed to be simple but powerful.", // desc 10, // Popularity vector(), // depends + true, "aur", // db_name }; @@ -129,47 +130,46 @@ bool execPacman(int argc, char* argv[]) { return false; } -int installPkg(string pkgName) { +int installPkg(alpm_list_t *pkgNames) { + if (!pkgNames) + return false; + bool useGit = config->useGit; string cacheDir = config->cacheDir; - vector pkgs = backend->search(pkgName, useGit); + bool returnStatus = true; - if (pkgs.empty() && !op.op_s_upgrade) { - log_printf(LOG_WARN, "No results found, Exiting!\n"); - return false; - } else if (pkgs.empty()) { - if (!config->aurOnly) { - vector cmd = {config->sudo.c_str(), "pacman", "-S"}; - if(op.op_s_sync) - cmd.push_back("-y"); - if(op.op_s_upgrade) - cmd.push_back("-u"); - - if (!taur_exec(cmd)) - return false; + for (; pkgNames; pkgNames = pkgNames->next) { + string pkgName = string((char *)(pkgNames->data)); + vector pkgs = backend->search(pkgName, useGit); + + if (pkgs.empty()) { + log_printf(LOG_WARN, "No results found!\n"); + returnStatus = false; + continue; } - return backend->update_all_pkgs(cacheDir, useGit); - } - // ./taur -Ss -- list only, don't install. - if (op.op_s_search) { - for (size_t i = 0; i < pkgs.size(); i++) - printPkgInfo(pkgs[i], pkgs[i].db_name); - return true; - } + // ./taur -Ss -- list only, don't install. + if (op.op_s_search) { + for (size_t i = 0; i < pkgs.size(); i++) + printPkgInfo(pkgs[i], pkgs[i].db_name); + returnStatus = false; + continue; + } - optional> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit); + optional> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit); - if (!oSelectedPkgs) - return false; + if (!oSelectedPkgs) { + returnStatus = false; + continue; + } - vector selectedPkgs = oSelectedPkgs.value(); + vector selectedPkgs = oSelectedPkgs.value(); - for (size_t i = 0; i < selectedPkgs.size(); i++) { - TaurPkg_t pkg = selectedPkgs[i]; + for (size_t i = 0; i < selectedPkgs.size(); i++) { + TaurPkg_t pkg = selectedPkgs[i]; - string url = pkg.url; + string url = pkg.url; if (url == "") { vector cmd = {config->sudo.c_str(), "pacman", "-S"}; @@ -180,58 +180,66 @@ int installPkg(string pkgName) { cmd.push_back(pkg.name.c_str()); - cmd.push_back(NULL); - - if (taur_exec(cmd)) + if (taur_exec(cmd, false)) + continue; + else { + returnStatus = false; continue; - else - return false; + } } - string filename = path(cacheDir) / url.substr(url.rfind("/") + 1); + string filename = path(cacheDir) / url.substr(url.rfind("/") + 1); - if (useGit) - filename = filename.substr(0, filename.rfind(".git")); + if (useGit) + filename = filename.substr(0, filename.rfind(".git")); - bool stat = backend->download_pkg(url, filename); + bool stat = backend->download_pkg(url, filename); - if (!stat) { - log_printf(LOG_ERROR, "An error has occurred and we could not download your package.\n"); - return false; - } - - if (!useGit){ - stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); - stat = backend->install_pkg(pkg.name, filename.substr(0, filename.rfind(".tar.gz")), false); - } - else { - stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); - stat = backend->install_pkg(pkg.name, filename, false); - } + if (!stat) { + log_printf(LOG_ERROR, "An error has occurred and we could not download your package.\n"); + returnStatus = false; + continue; + } + + if (!useGit){ + stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); + stat = backend->install_pkg(pkg.name, filename.substr(0, filename.rfind(".tar.gz")), false); + } + else { + stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); + stat = backend->install_pkg(pkg.name, filename, false); + } + + if (!stat) { + log_printf(LOG_ERROR, "Building your package has failed.\n"); + returnStatus = false; + continue; + } - if (!stat) { - log_printf(LOG_ERROR, "Building your package has failed.\n"); - return false; - } + log_printf(LOG_DEBUG, "Installing {}.\n", pkg.name); + if (!taur_exec({config->sudo.c_str(), "pacman", "-U", built_pkg.c_str()}, false)) { + log_printf(LOG_ERROR, "Failed to install {}.\n", pkg.name); + returnStatus = false; + continue; + } - log_printf(LOG_DEBUG, "Installing {}.\n", pkg.name); - if (!taur_exec({config->sudo.c_str(), "pacman", "-U", built_pkg.c_str()})) { - log_printf(LOG_ERROR, "Failed to install {}.\n", pkg.name); - return false; } } if (op.op_s_upgrade) { log_printf(LOG_INFO, "-u flag specified, upgrading AUR packages.\n"); - return backend->update_all_pkgs(cacheDir, useGit); + return backend->update_all_pkgs(cacheDir, useGit) && returnStatus; } - return true; + return returnStatus; } -bool removePkg(string pkgName) { - return backend->remove_pkg(pkgName, config->aurOnly); +bool removePkg(alpm_list_t *pkgNames) { + if (!pkgNames) + return false; + + return backend->remove_pkg(pkgNames, config->aurOnly); } bool updateAll() { @@ -432,9 +440,9 @@ int main(int argc, char* argv[]) { switch (op.op) { case OP_SYNC: - return (installPkg(taur_targets ? string((const char *)(taur_targets->data)) : "")) ? 0 : 1; + return installPkg(taur_targets.get()) ? 0 : 1; case OP_REM: - return (removePkg(taur_targets ? string((const char *)(taur_targets->data)) : "")) ? 0 : 1; + return removePkg(taur_targets.get()) ? 0 : 1; case OP_QUERY: return (queryPkgs()) ? 0 : 1; case OP_SYSUPGRADE: diff --git a/src/taur.cpp b/src/taur.cpp index 13b1f30..75a4a06 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -81,7 +81,9 @@ TaurPkg_t parsePkg(rapidjson::Value& pkgJson, bool returnGit = false) { getUrl(pkgJson, returnGit), // url pkgJson["Description"].IsString() ? pkgJson["Description"].GetString() : "", // description pkgJson["Popularity"].GetFloat(), // popularity - depends}; // depends + depends, // depends + alpm_db_get_pkg(alpm_get_localdb(config->handle), pkgJson["Name"].GetString()) != nullptr, + }; return out; } @@ -130,15 +132,10 @@ vector TaurBackend::fetch_pkgs(vector pkgs, bool returnGit) { return out; } -bool TaurBackend::remove_pkg(string pkgName, bool aurOnly) { - alpm_list_smart_pointer list(alpm_list_add(nullptr, (void *)((".*" + pkgName + ".*").c_str())), alpm_list_free); +bool TaurBackend::remove_pkg(alpm_list_t *pkgQueries, bool aurOnly) { alpm_list_t *temp_ret = nullptr; - alpm_pkg_t *potential_pkg = alpm_db_get_pkg(alpm_get_localdb(config.handle), pkgName.c_str()); - - if (potential_pkg) - temp_ret = alpm_list_add(temp_ret, potential_pkg); - else if (alpm_db_search(alpm_get_localdb(config.handle), list.get(), &temp_ret) != 0) + if (alpm_db_search(alpm_get_localdb(config.handle), pkgQueries, &temp_ret) != 0) return false; alpm_list_smart_pointer ret(temp_ret, alpm_list_free); @@ -506,6 +503,7 @@ bool TaurBackend::update_all_pkgs(string cacheDir, bool useGit) { } // all AUR local packages +// returns a barebones TaurPkg_t structure, with no description/depends list vector TaurBackend::get_all_local_pkgs(bool aurOnly) { vector pkgs; @@ -522,7 +520,7 @@ vector TaurBackend::get_all_local_pkgs(bool aurOnly) { vector out; for (size_t i = 0; i < pkgs.size(); i++) { alpm_pkg_t *pkg = pkgs[i]; - out.push_back({alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg), "https://aur.archlinux.org/" + cpr::util::urlEncode(alpm_pkg_get_name(pkg)) + ".git"}); + out.push_back({alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg), "https://aur.archlinux.org/" + cpr::util::urlEncode(alpm_pkg_get_name(pkg)) + ".git", "", 1, {}, true}); } return out; @@ -573,6 +571,7 @@ vector TaurBackend::search_pac(string query) { string(alpm_pkg_get_desc(pkg)), // desc 100, // system packages are very trustable vector(), // depends + alpm_db_get_pkg(alpm_get_localdb(config.handle), alpm_pkg_get_name(pkg)) != nullptr, string(alpm_db_get_name(alpm_pkg_get_db(pkg))), // db_name }; diff --git a/src/util.cpp b/src/util.cpp index 487eae5..c304850 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -266,7 +266,7 @@ bool taur_exec(vector cmd, bool exitOnFailure) { if (WIFEXITED(status) && WEXITSTATUS(status) == 0) return true; else { - log_printf(LOG_ERROR, "Failed to execute the command: "); + log_printf(LOG_ERROR, "Failed to execute command: "); print_vec(cmd); // fmt::join() doesn't work with vector if (exitOnFailure) exit(-1); @@ -312,7 +312,11 @@ void printPkgInfo(TaurPkg_t &pkg, string db_name, int index) { fmt::print(getColorFromDBName(db_name), "{}/", db_name); fmt::print(fmt::emphasis::bold, "{} ", pkg.name); fmt::print(BOLD_TEXT(config->getThemeValue("version", green)), "{} ", pkg.version); - fmt::println(fmt::fg(config->getThemeValue("popularity", cyan)), " Popularity: {} ({})", pkg.popularity, getTitleForPopularity(pkg.popularity)); + fmt::print(fmt::fg(config->getThemeValue("popularity", cyan)), " Popularity: {} ({}) ", pkg.popularity, getTitleForPopularity(pkg.popularity)); + if (pkg.installed) + fmt::println(fmt::fg(config->getThemeValue("gray", gray)), "[Installed]"); + else + fmt::println(""); fmt::println(" {}", pkg.desc); } @@ -343,8 +347,6 @@ optional> askUserForPkg(vector pkgs, TaurBackend& b fmt::print("Choose a package to download: "); std::getline(std::cin, input); - - input.end() = input.end()-1; // remove leading newline } while (!is_number(input, true)); vector indices = split(input, ' ');