From 024e9013d660d8d3c1195b33d8b00e723e6eb750 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Tue, 10 Sep 2024 18:26:39 +0200 Subject: [PATCH] general: code improvement reference, references everywhere & --- include/config.hpp | 12 +++---- include/taur.hpp | 10 +++--- include/util.hpp | 14 ++++---- src/config.cpp | 32 +++++++++--------- src/main.cpp | 48 ++++++++++++++------------- src/taur.cpp | 81 +++++++++++++++++++++++----------------------- src/util.cpp | 43 ++++++++++++++---------- 7 files changed, 126 insertions(+), 114 deletions(-) diff --git a/include/config.hpp b/include/config.hpp index 0ccfd33..b0a0286 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -18,7 +18,7 @@ using std::filesystem::path; // so we don't need to include util.hpp for getConfigValue() -std::string expandVar(std::string& str); +std::string expandVar(std::string str); struct _color_t { @@ -66,19 +66,19 @@ class Config // alpm transaction flags int flags; - Config(std::string_view configFile, std::string_view themeFile, std::string_view configDir); + Config(const std::string_view configFile, const std::string_view themeFile, const std::string_view configDir); ~Config(); void initVars(); void initColors(); - void loadConfigFile(std::string_view filename); - void loadPacmanConfigFile(std::string filename); - void loadThemeFile(std::string_view filename); + void loadConfigFile(const std::string_view filename); + void loadPacmanConfigFile(const std::string_view filename); + void loadThemeFile(const std::string_view filename); // stupid c++ that wants template functions in header template - T getConfigValue(const std::string& value, T fallback) + T getConfigValue(const std::string& value, T&& fallback) { std::optional ret = this->tbl.at_path(value).value(); if constexpr (toml::is_string) // if we want to get a value that's a std::string diff --git a/include/taur.hpp b/include/taur.hpp index ec2b4a1..5dcd776 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -54,16 +54,16 @@ class TaurBackend std::vector getPkgFromJson(rapidjson::Document& doc, bool useGit); std::vector search_pac(std::string_view query); std::vector search(std::string_view query, bool useGit, bool aurOnly, bool checkExactMatch = true); - bool download_tar(std::string_view url, path out_path); - bool download_git(std::string_view url, path out_path); - bool download_pkg(std::string_view url, path out_path); + bool download_tar(const std::string_view url, const path& out_path); + bool download_git(const std::string_view url, const path& out_path); + bool download_pkg(const std::string_view url, const path out_path); std::optional fetch_pkg(std::string_view pkg, bool returnGit); std::vector fetch_pkgs(std::vector const& pkgs, bool returnGit); bool remove_pkgs(alpm_list_smart_pointer& pkgs); bool remove_pkg(alpm_pkg_t* pkgs, bool ownTransaction = true); - bool handle_aur_depends(const TaurPkg_t& pkg, path out_path, std::vector const& localPkgs, bool useGit); + bool handle_aur_depends(const TaurPkg_t& pkg, path& out_path, std::vector const& localPkgs, bool useGit); bool build_pkg(std::string_view pkg_name, std::string_view extracted_path, bool alreadyprepared); - bool update_all_aur_pkgs(path cacheDir, bool useGit); + bool update_all_aur_pkgs(path& cacheDir, bool useGit); std::vector get_all_local_pkgs(bool aurOnly); }; diff --git a/include/util.hpp b/include/util.hpp index 6d8cf79..845dc23 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -91,7 +91,7 @@ enum log_level bool hasEnding(std::string_view fullString, std::string_view ending); bool hasStart(std::string_view fullString, std::string_view start); -std::string expandVar(std::string& str); +std::string expandVar(std::string str); bool is_numerical(std::string_view s, bool allowSpace = false); bool taur_read_exec(std::vector cmd, std::string& output, bool exitOnFailure = true); void interruptHandler(int); @@ -99,14 +99,14 @@ bool taur_exec(std::vector cmd, bool exitOnFail void sanitizeStr(std::string& str); bool is_package_from_syncdb(const char* name, alpm_list_t* syncdbs); bool commitTransactionAndRelease(bool soft = false); -void printPkgInfo(TaurPkg_t& pkg, std::string_view db_name); +void printPkgInfo(const TaurPkg_t& pkg, const std::string_view db_name); void printLocalFullPkgInfo(alpm_pkg_t* pkg); std::string makepkg_list(std::string_view pkg_name, std::string_view path); void getFileValue(u_short& iterIndex, const std::string& line, std::string& str, const size_t& amount); void free_list_and_internals(alpm_list_t* list); fmt::text_style getColorFromDBName(std::string_view db_name); -std::vector filterAURPkgs(std::vector pkgs, alpm_list_t* syncdbs, bool inverse); -std::vector filterAURPkgsNames(std::vector pkgs, alpm_list_t* syncdbs, +std::vector filterAURPkgs(std::vector& pkgs, alpm_list_t* syncdbs, bool inverse); +std::vector filterAURPkgsNames(std::vector& pkgs, alpm_list_t* syncdbs, bool inverse); std::string shell_exec(std::string_view cmd); std::vector split(std::string_view text, char delim); @@ -122,7 +122,7 @@ bool pacman_exec(std::string_view op, std::vector> askUserForPkg(std::vector pkgs, TaurBackend& backend, bool useGit); +std::optional> askUserForPkg(const std::vector& pkgs, TaurBackend& backend, bool useGit); std::string_view binarySearch(const std::vector& arr, std::string_view target); std::vector load_aur_list(); bool update_aur_cache(bool recursiveCall = false); @@ -241,7 +241,7 @@ void log_printf(log_level log, const fmt::text_style ts, std::string_view fmt, A template bool askUserYorN(bool def, prompt_yn pr, Args&&... args) { - std::string inputs_str = "[" + (std::string)(def ? "Y" : "y") + "/" + (std::string)(!def ? "N" : "n") + "] "; + const std::string& inputs_str = fmt::format("[{}]: ", (def ? "Y/n" : "y/N")); std::string result; switch (pr) @@ -309,7 +309,7 @@ bool askUserYorN(bool def, prompt_yn pr, Args&&... args) * @returns the resulting list, empty if anything bad happens. */ template >> -std::vector askUserForList(std::vector& list, prompt_list pr, bool required = false) +std::vector askUserForList(const std::vector& list, prompt_list pr, bool required = false) { std::string sep_str = _("Type the index of each package (eg: \"0 1 2\", \"0-2\", \"a\" for all, \"n\" or enter for none)"); diff --git a/src/config.cpp b/src/config.cpp index 3b3ccf3..d34ab27 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -20,7 +20,7 @@ Config::~Config() } // initialize Config, can only be ran once for each Config instance. -Config::Config(std::string_view configFile, std::string_view themeFile, std::string_view configDir) +Config::Config(const std::string_view configFile, const std::string_view themeFile, const std::string_view configDir) { bool newUser = false; @@ -133,7 +133,7 @@ void Config::loadConfigFile(std::string_view filename) if (!(this->handle)) die(_("Failed to get an alpm handle! Error: {}"), alpm_strerror(err)); - this->loadPacmanConfigFile(this->getConfigValue("pacman.ConfigFile", "/etc/pacman.conf")); + this->loadPacmanConfigFile(this->getConfigValue("pacman.ConfigFile", "/etc/pacman.conf")); } /** parse the theme file (aka "theme.toml") @@ -186,25 +186,25 @@ void Config::initColors() color.yellow = this->getThemeValue("yellow", "#ffff00"); color.magenta = this->getThemeValue("magenta", "#ff11cc"); color.gray = this->getThemeValue("gray", "#5a5a5a"); - color.aur = this->getThemeValue("aur", this->getThemeHexValue("blue", "#00aaff")); - color.extra = this->getThemeValue("extra", this->getThemeHexValue("green", "#00ff00")); - color.core = this->getThemeValue("core", this->getThemeHexValue("yellow", "#ffff00")); - color.multilib = this->getThemeValue("multilib", this->getThemeHexValue("cyan", "#00ffff")); + color.aur = this->getThemeValue("aur", this->getThemeHexValue("blue", "#00aaff")); + color.extra = this->getThemeValue("extra", this->getThemeHexValue("green", "#00ff00")); + color.core = this->getThemeValue("core", this->getThemeHexValue("yellow", "#ffff00")); + color.multilib = this->getThemeValue("multilib", this->getThemeHexValue("cyan", "#00ffff")); color.others = this->getThemeValue("others", this->getThemeHexValue("magenta", "#ff11cc")); - color.version = this->getThemeValue("version", this->getThemeHexValue("green", "#00ff00")); + color.version = this->getThemeValue("version", this->getThemeHexValue("green", "#00ff00")); color.last_modified = this->getThemeValue("last_modified", this->getThemeHexValue("magenta", "#ff11cc")); - color.outofdate = this->getThemeValue("outofdate", this->getThemeHexValue("red", "#ff2000")); - color.orphan = this->getThemeValue("orphan", this->getThemeHexValue("red", "#ff2000")); - color.popularity = this->getThemeValue("popularity", this->getThemeHexValue("cyan", "#00ffff")); - color.votes = this->getThemeValue("votes", this->getThemeHexValue("cyan", "#00ffff")); - color.installed = this->getThemeValue("installed", this->getThemeHexValue("gray", "#5a5a5a")); + color.outofdate = this->getThemeValue("outofdate", this->getThemeHexValue("red", "#ff2000")); + color.orphan = this->getThemeValue("orphan", this->getThemeHexValue("red", "#ff2000")); + color.popularity = this->getThemeValue("popularity", this->getThemeHexValue("cyan", "#00ffff")); + color.votes = this->getThemeValue("votes", this->getThemeHexValue("cyan", "#00ffff")); + color.installed = this->getThemeValue("installed", this->getThemeHexValue("gray", "#5a5a5a")); color.index = this->getThemeValue("index", this->getThemeHexValue("magenta", "#ff11cc")); } // clang-format on -bool addServers(alpm_db_t* db, const std::string& includeFilename, std::string_view repoName) +bool addServers(alpm_db_t* db, const std::string_view includeFilename, std::string_view repoName) { - std::ifstream includeFile(includeFilename); + std::ifstream includeFile(includeFilename.data()); if (!includeFile.is_open()) return false; @@ -233,9 +233,9 @@ bool addServers(alpm_db_t* db, const std::string& includeFilename, std::string_v return true; } -void Config::loadPacmanConfigFile(std::string filename) +void Config::loadPacmanConfigFile(const std::string_view filename) { - mINI::INIFile file(filename); + mINI::INIFile file(filename.data()); mINI::INIStructure ini; file.read(ini); diff --git a/src/main.cpp b/src/main.cpp index e204072..303b710 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,9 +87,9 @@ void usage(int op) void test_colors() { - std::time_t current_time = std::time(nullptr); + const std::time_t& current_time = std::time(nullptr); std::string timestr = std::ctime(¤t_time); - timestr.erase(timestr.length() - 1); + timestr.pop_back(); TaurPkg_t pkg = { .name = "TabAUR", @@ -207,7 +207,7 @@ int installPkg(alpm_list_t* pkgNames) log_println(ERROR, _("Failed to get information about {}"), (config->cacheDir / "packages.aur").string()); // I swear there was a comment here.. - std::vector AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true); + const std::vector& AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true); for (const auto& pkg : pkgNamesVec) { @@ -223,7 +223,7 @@ int installPkg(alpm_list_t* pkgNames) for (auto& pkg_name : AURPkgs) { - path pkgDir = path(cacheDir) / pkg_name; + const path& pkgDir = path(cacheDir) / pkg_name; stat = useGit ? backend->download_git(AUR_URL_GIT(pkg_name), pkgDir) : backend->download_tar(AUR_URL_TAR(pkg_name), pkgDir); @@ -235,9 +235,9 @@ int installPkg(alpm_list_t* pkgNames) } } - for (std::string_view& pkg : pkgsToCleanBuild) + for (const std::string_view pkg : pkgsToCleanBuild) { - path pkgDir = path(cacheDir) / pkg; + const path& pkgDir = path(cacheDir) / pkg; if (!useGit) { log_println(INFO, _("Removing {}"), pkgDir.c_str()); @@ -253,13 +253,15 @@ int installPkg(alpm_list_t* pkgNames) // cmd is just a workaround for making it possible // to pass flags to the editor, e.g nano --modernbindings // instead of creating another config variable - for (std::string_view& pkg : pkgsToReview) + for (const std::string_view pkg : pkgsToReview) { - path pkgDir = path(cacheDir) / pkg; + const path& pkgDir = path(cacheDir) / pkg; std::vector cmd; + cmd.reserve(config->editor.size()); for (auto& str : config->editor) cmd.push_back(str); + cmd.push_back((pkgDir / "PKGBUILD").string()); taur_exec(cmd); @@ -296,9 +298,9 @@ int installPkg(alpm_list_t* pkgNames) for (size_t i = 0; i < AURPkgs.size(); i++) { - std::vector pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true); + const std::vector& pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true); - std::optional> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit); + const std::optional>& oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit); if (!oSelectedPkgs) { @@ -306,9 +308,9 @@ int installPkg(alpm_list_t* pkgNames) continue; } - std::vector selectedPkgs = oSelectedPkgs.value(); + const std::vector& selectedPkgs = oSelectedPkgs.value(); - for (TaurPkg_t& pkg : selectedPkgs) + for (const TaurPkg_t& pkg : selectedPkgs) { path pkgDir = path(cacheDir) / pkg.name; @@ -325,7 +327,7 @@ int installPkg(alpm_list_t* pkgNames) if (!stat) { - log_println(ERROR, _("Building {} has failed."), pkg.name); + log_println(ERROR, _("Building '{}' has failed."), pkg.name); returnStatus = false; pkgs_failed_to_build += pkg.name + ' '; log_println(DEBUG, "pkgs_failed_to_build = {}", pkgs_failed_to_build); @@ -387,25 +389,26 @@ bool removePkg(alpm_list_t* pkgNames) alpm_list_smart_pointer ret(alpm_list_join(exactMatches, searchResults), alpm_list_free); - size_t ret_length = alpm_list_count(ret.get()); + size_t ret_size = alpm_list_count(ret.get()); - if (ret_length == 0) + if (ret_size == 0) { log_println(ERROR, _("No packages found!")); return false; } - if (ret_length == 1) + if (ret_size == 1) return backend->remove_pkg(reinterpret_cast(ret->data)); std::vector pkgs; + pkgs.reserve(ret_size); // fmt::println("Choose packages to remove, (Seperate by spaces, type * to remove all):"); - for (size_t i = 0; i < ret_length; i++) + for (size_t i = 0; i < ret_size; i++) pkgs.push_back(alpm_pkg_get_name(reinterpret_cast(alpm_list_nth(ret.get(), i)->data))); - std::vector includedPkgs = askUserForList(pkgs, PROMPT_LIST_REMOVE_PKGS, true); + const std::vector& includedPkgs = askUserForList(pkgs, PROMPT_LIST_REMOVE_PKGS, true); alpm_list_t* finalPackageList = nullptr; alpm_list_t* finalPackageListStart = nullptr; @@ -414,7 +417,7 @@ bool removePkg(alpm_list_t* pkgNames) { try { - auto pkg = std::find(pkgs.begin(), pkgs.end(), includedPkgs[i]); + const auto& pkg = std::find(pkgs.begin(), pkgs.end(), includedPkgs[i]); if (pkg >= pkgs.end()) continue; @@ -455,8 +458,8 @@ bool queryPkgs(alpm_list_t* pkgNames) // query out pkgs will be used for -Qs, for then using it on printPkgInfo() pkgs_name and pkgs_ver will be used for // bare operations (only -Q) std::vector pkgs_name, pkgs_ver; - std::vector pkgs; - alpm_db_t* localdb = alpm_get_localdb(config->handle); + std::vector pkgs; + alpm_db_t* localdb = alpm_get_localdb(config->handle); // just -Q, no options other than --quiet and global ones if (!pkgNames) @@ -481,11 +484,10 @@ bool queryPkgs(alpm_list_t* pkgNames) pkgs_ver.push_back(alpm_pkg_get_version(reinterpret_cast(result->data))); } } else { - alpm_pkg_t* pkg; for (; pkgNames; pkgNames = pkgNames->next) { const char* strname = reinterpret_cast(pkgNames->data); - pkg = alpm_db_get_pkg(localdb, strname); + alpm_pkg_t* pkg = alpm_db_get_pkg(localdb, strname); if (pkg == nullptr) pkg = alpm_find_satisfier(alpm_db_get_pkgcache(localdb), strname); diff --git a/src/taur.cpp b/src/taur.cpp index 1cfca75..c190f11 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -11,7 +11,7 @@ TaurBackend::TaurBackend(Config& cfg) : config(cfg) {} -bool TaurBackend::download_git(std::string_view url, path out_path) +bool TaurBackend::download_git(const std::string_view url, const path& out_path) { if (std::filesystem::exists(path(out_path) / ".git")) { @@ -25,15 +25,19 @@ bool TaurBackend::download_git(std::string_view url, path out_path) } } -bool TaurBackend::download_tar(std::string_view url, path out_path) +bool TaurBackend::download_tar(const std::string_view url, const path& out_path) { - std::ofstream out(out_path.replace_extension("tar.gz")); + const std::string& out_path_str = out_path.string(); + std::ofstream out(out_path_str.substr(0, out_path_str.length() - "tar.gz"_len)); if (!out.is_open()) return false; cpr::Session session; session.SetUrl(cpr::Url(url)); - cpr::Response response = session.Download(out); + const cpr::Response& r = session.Download(out); + + if (r.status_code != 200) + return false; out.close(); @@ -55,7 +59,7 @@ bool TaurBackend::download_pkg(std::string_view url, path out_path) return this->download_git(url, out_path); else if (hasEnding(url, ".tar.gz")) return this->download_tar(url, out_path); - + return false; } @@ -63,7 +67,7 @@ std::string getUrl(const rapidjson::Value& pkgJson, bool returnGit = false) { if (returnGit) return fmt::format("https://aur.archlinux.org/{}.git", pkgJson["Name"].GetString()); - + // URLPath starts with a / return fmt::format("https://aur.archlinux.org{}", pkgJson["URLPath"].GetString()); } @@ -138,10 +142,9 @@ TaurPkg_t parsePkg(rapidjson::Value& pkgJson, bool returnGit = false) std::optional TaurBackend::fetch_pkg(std::string_view pkg, bool returnGit) { - std::string urlStr = "https://aur.archlinux.org/rpc/v5/info/" + cpr::util::urlEncode(pkg.data()); + const std::string& urlStr = "https://aur.archlinux.org/rpc/v5/info/" + cpr::util::urlEncode(pkg.data()); - cpr::Url url = cpr::Url(urlStr); - cpr::Response resp = cpr::Get(url); + const cpr::Response& resp = cpr::Get(cpr::Url(urlStr)); if (resp.status_code != 200) return {}; @@ -158,7 +161,7 @@ std::optional TaurBackend::fetch_pkg(std::string_view pkg, bool retur std::vector TaurBackend::fetch_pkgs(std::vector const& pkgs, bool returnGit) { if (pkgs.empty()) - return std::vector(); + return {}; std::string urlStr = "https://aur.archlinux.org/rpc/v5/info?arg%5B%5D=" + pkgs[0]; @@ -167,11 +170,10 @@ std::vector TaurBackend::fetch_pkgs(std::vector const& p log_println(DEBUG, "info url = {}", urlStr); - cpr::Url url = cpr::Url(urlStr); - cpr::Response resp = cpr::Get(url); + const cpr::Response& resp = cpr::Get(cpr::Url(urlStr)); if (resp.status_code != 200) - return std::vector(); + return {}; rapidjson::Document json; json.Parse(resp.text.c_str()); @@ -296,11 +298,11 @@ bool TaurBackend::build_pkg(std::string_view pkg_name, std::string_view extracte } // I don't know but I feel this is shitty, atleast it works great -bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path out_path, std::vector const& localPkgs, bool useGit) +bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path& out_path, std::vector const& localPkgs, bool useGit) { log_println(DEBUG, "pkg.name = {}", pkg.name); log_println(DEBUG, "pkg.totaldepends = {}", pkg.totaldepends); - std::vector aur_list = load_aur_list(); + const std::vector& aur_list = load_aur_list(); for (size_t i = 0; i < pkg.totaldepends.size(); i++) { @@ -312,11 +314,11 @@ bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path out_path, std::v if (pkg_depend.empty()) continue; - std::optional oDepend = this->fetch_pkg(pkg_depend, useGit); + const std::optional& oDepend = this->fetch_pkg(pkg_depend, useGit); if (!oDepend) continue; - TaurPkg_t depend = oDepend.value(); + const TaurPkg_t& depend = oDepend.value(); log_println(DEBUG, "depend = {} -- depend.totaldepends = {}", depend.name, depend.totaldepends); @@ -368,7 +370,7 @@ bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path out_path, std::v continue; } - std::string filename = out_path / subDepend.aur_url.substr(subDepend.aur_url.rfind("/") + 1); + std::string filename = out_path / subDepend.aur_url.substr(subDepend.aur_url.rfind('/') + 1); if (useGit) filename = filename.substr(0, filename.rfind(".git")); @@ -410,7 +412,7 @@ bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path out_path, std::v log_println(INFO, _("Downloading dependency {}"), depend.name); - std::string filename = out_path / depend.aur_url.substr(depend.aur_url.rfind("/") + 1); + std::string filename = out_path / depend.aur_url.substr(depend.aur_url.rfind('/') + 1); if (useGit) filename = filename.substr(0, filename.rfind(".git")); @@ -442,9 +444,9 @@ bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path out_path, std::v return true; } -bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) +bool TaurBackend::update_all_aur_pkgs(path& cacheDir, bool useGit) { - std::vector localPkgs = this->get_all_local_pkgs(true); + const std::vector& localPkgs = this->get_all_local_pkgs(true); if (localPkgs.empty()) { @@ -459,7 +461,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) for (const TaurPkg_t& pkg : localPkgs) pkgNames.push_back(pkg.name); - std::vector onlinePkgs = this->fetch_pkgs(pkgNames, useGit); + const std::vector& onlinePkgs = this->fetch_pkgs(pkgNames, useGit); int updatedPkgs = 0; int attemptedDownloads = 0; @@ -475,10 +477,12 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) for (size_t i = 0; i < onlinePkgs.size(); i++) { - TaurPkg_t& pkg = onlinePkgs[i]; - auto pkgIteratorInLocalPkgs = std::find_if(localPkgs.begin(), localPkgs.end(), [&pkg](const TaurPkg_t& element) { return element.name == pkg.name; }); - size_t pkgIndexInLocalPkgs = std::distance(localPkgs.begin(), pkgIteratorInLocalPkgs); - TaurPkg_t& localPkg = localPkgs[pkgIndexInLocalPkgs]; + const TaurPkg_t& pkg = onlinePkgs[i]; + const auto& pkgIteratorInLocalPkgs = std::find_if(localPkgs.begin(), localPkgs.end(), + [&pkg](const TaurPkg_t& element) { return element.name == pkg.name; }); + + const size_t pkgIndexInLocalPkgs = std::distance(localPkgs.begin(), pkgIteratorInLocalPkgs); + const TaurPkg_t& localPkg = localPkgs[pkgIndexInLocalPkgs]; if (hasEnding(pkg.name, "-git")) { @@ -642,7 +646,7 @@ bool TaurBackend::update_all_aur_pkgs(path cacheDir, bool useGit) log_println(WARN, fg(color.red), _("Failed to upgrade: {}"), pkgs_failed_to_build); // log_println(WARN, _("Some packages failed to download/upgrade, Please redo this command and log the // issue.\nIf it is an issue with TabAUR, feel free to open an issue in GitHub.")); - log_println(INFO, fg(color.cyan), _("Tip: try to run taur with \"-S {}\" (e.g \"taur -S {}\")"), + log_println(INFO, fg(color.cyan), _("Tip: try to run taur with \"-S {}\" (e.g \"taur -S {}\") and cleanbuild"), pkgs_failed_to_build, pkgs_failed_to_build); } @@ -739,30 +743,27 @@ std::vector TaurBackend::search_pac(std::string_view query) std::vector TaurBackend::search(std::string_view query, bool useGit, bool aurOnly, bool checkExactMatch) { if (query.empty()) - return std::vector(); + return {}; // link to AUR API. Took search pattern from yay - cpr::Url url(("https://aur.archlinux.org/rpc?arg%5B%5D=" + cpr::util::urlEncode(query.data()) + - "&by=" + config.getConfigValue("searchBy", "name-desc") + "&type=search&v=5")); + const cpr::Url& url = fmt::format("https://aur.archlinux.org/rpc?arg%5B%5D={}&by={}&type=search&v=5", cpr::util::urlEncode(query.data()), config.getConfigValue("searchBy", "name-desc")); log_println(DEBUG, _("url search = {}"), url.str()); - cpr::Response r = cpr::Get(url); - std::string_view raw_text_response = r.text; + std::string_view raw_text_response = cpr::Get(url).text; rapidjson::Document json_response; json_response.Parse(raw_text_response.data()); - std::vector aurPkgs; - std::vector pacPkgs; + // clang-format off + const std::vector& aurPkgs = (json_response["resultcount"].GetInt() > 0) ? + this->getPkgFromJson(json_response, useGit) : std::vector(); + + const std::vector& pacPkgs = (!aurOnly) ? + this->search_pac(query) : std::vector(); - if (json_response["resultcount"].GetInt() > 0) - aurPkgs = this->getPkgFromJson(json_response, useGit); if (std::string_view(json_response["type"].GetString(), json_response["type"].GetStringLength()) == "error") log_println(ERROR, "AUR Search error: {}", json_response["error"].GetString()); - if (!aurOnly) - pacPkgs = this->search_pac(query); - - size_t allPkgsSize = aurPkgs.size() + pacPkgs.size(); + const size_t& allPkgsSize = aurPkgs.size() + pacPkgs.size(); std::vector combined; diff --git a/src/util.cpp b/src/util.cpp index 05dbd56..cde8060 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -19,7 +19,6 @@ #include #include -#include #pragma GCC diagnostic ignored "-Wignored-attributes" #include "config.hpp" @@ -142,7 +141,7 @@ bool isInvalid(char c) { return !isprint(c); } void sanitizeStr(std::string& str) -{ str.erase(std::remove_if(str.begin(), str.end(), isInvalid), str.end()); } +{ str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char c){ return isInvalid(c); } ), str.end()); } /** Print some text after hitting CTRL-C. * Used only in main() for signal() @@ -333,28 +332,38 @@ bool commitTransactionAndRelease(bool soft) * @param str The std::string * @return The modified std::string */ -std::string expandVar(std::string& str) +std::string expandVar(std::string ret) { const char* env; - if (str[0] == '~') + if (ret[0] == '~') { - env = getenv("HOME"); + env = std::getenv("HOME"); if (env == nullptr) - die(_("FATAL: $HOME enviroment variable is not set (how?)")); + die("FATAL: $HOME enviroment variable is not set (how?)"); - str.replace(0, 1, env); // replace ~ with the $HOME value + ret.replace(0, 1, env); // replace ~ with the $HOME value } - else if (str[0] == '$') + else if (ret[0] == '$') { - str.erase(0, 1); // erase from str[0] to str[1] - env = getenv(str.c_str()); + ret.erase(0, 1); + + std::string temp; + const size_t& pos = ret.find('/'); + if (pos != std::string::npos) + { + temp = ret.substr(pos); + ret.erase(pos); + } + + env = std::getenv(ret.c_str()); if (env == nullptr) - die(_("No such enviroment variable: {}"), str); + die("No such enviroment variable: {}", ret); - str = env; + ret = env; + ret += temp; } - return str; + return ret; } fmt::rgb hexStringToColor(std::string_view hexstr) @@ -599,7 +608,7 @@ fmt::text_style getColorFromDBName(std::string_view db_name) } // Takes a pkg to show on search. -void printPkgInfo(TaurPkg_t& pkg, std::string_view db_name) +void printPkgInfo(const TaurPkg_t& pkg, const std::string_view db_name) { fmt::print(getColorFromDBName(db_name), "{}/", db_name); fmt::print(BOLD, "{} ", pkg.name); @@ -865,7 +874,7 @@ bool update_aur_cache(bool recursiveCall) * @param useGit Whether the fetched pkg should use a .git url * @return Optional TaurPkg_t, will not return if interrupted. */ -std::optional> askUserForPkg(std::vector pkgs, TaurBackend& backend, bool useGit) +std::optional> askUserForPkg(const std::vector& pkgs, TaurBackend& backend, bool useGit) { if (pkgs.size() == 1) { @@ -930,7 +939,7 @@ void ctrl_d_handler(const std::istream& cin) * @param inverse a bool that, if true, will return only AUR packages instead of the other way around. * @return an optional unique_ptr to a result. */ -std::vector filterAURPkgs(std::vector pkgs, alpm_list_t* syncdbs, bool inverse) +std::vector filterAURPkgs(std::vector& pkgs, alpm_list_t* syncdbs, bool inverse) { std::vector out; out.reserve(pkgs.size()); @@ -959,7 +968,7 @@ std::vector filterAURPkgs(std::vector pkgs, alpm_list_ * @param inverse a bool that, if true, will return only AUR packages instead of the other way around. * @return an optional unique_ptr to a result. */ -std::vector filterAURPkgsNames(std::vector pkgs, alpm_list_t* syncdbs, bool inverse) +std::vector filterAURPkgsNames(std::vector& pkgs, alpm_list_t* syncdbs, bool inverse) { std::vector out; out.reserve(pkgs.size());