diff --git a/include/args.hpp b/include/args.hpp index 59f13bb..ef05211 100644 --- a/include/args.hpp +++ b/include/args.hpp @@ -80,10 +80,10 @@ struct Operation_t inline struct Operation_t op; extern alpm_list_smart_deleter taur_targets; -int parsearg_op(int opt, int dryrun); +int parsearg_op(const int opt, const int dryrun); void invalid_opt(); -int parsearg_global(int opt); -int parsearg_query(int opt); -int parsearg_sync(int opt); -int parsearg_remove(int opt); +int parsearg_global(const int opt); +int parsearg_query(const int opt); +int parsearg_sync(const int opt); +int parsearg_remove(const int opt); #endif diff --git a/include/taur.hpp b/include/taur.hpp index 5dcd776..69ffcec 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -51,20 +51,20 @@ class TaurBackend TaurBackend(Config& cfg); // 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. - 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); + std::vector getPkgFromJson(const rapidjson::Document& doc, const bool useGit); + std::vector search_pac(const std::string_view query); + std::vector search(const std::string_view query, const bool useGit, const bool aurOnly, const bool checkExactMatch = true); 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 build_pkg(std::string_view pkg_name, std::string_view extracted_path, bool alreadyprepared); - bool update_all_aur_pkgs(path& cacheDir, bool useGit); - std::vector get_all_local_pkgs(bool aurOnly); + std::optional fetch_pkg(const std::string_view pkg, const bool returnGit); + std::vector fetch_pkgs(std::vector const& pkgs, const bool returnGit); + bool remove_pkgs(const alpm_list_smart_pointer& pkgs); + bool remove_pkg(alpm_pkg_t* pkgs, const bool ownTransaction = true); + bool handle_aur_depends(const TaurPkg_t& pkg, const path& out_path, std::vector const& localPkgs, const bool useGit); + bool build_pkg(const std::string_view pkg_name, const std::string_view extracted_path, const bool alreadyprepared); + bool update_all_aur_pkgs(const path& cacheDir, const bool useGit); + std::vector get_all_local_pkgs(const bool aurOnly); }; inline std::string built_pkg, pkgs_to_install, pkgs_failed_to_build; diff --git a/include/util.hpp b/include/util.hpp index 845dc23..acd9711 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -89,55 +89,55 @@ enum log_level NONE // display no prefix for this. }; -bool hasEnding(std::string_view fullString, std::string_view ending); -bool hasStart(std::string_view fullString, std::string_view start); +bool hasEnding(const std::string_view fullString, const std::string_view ending); +bool hasStart(const std::string_view fullString, const std::string_view start); 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); +bool is_numerical(const std::string_view s, const bool allowSpace = false); +bool taur_read_exec(std::vector cmd, std::string& output, const bool exitOnFailure = true); void interruptHandler(int); -bool taur_exec(std::vector cmd, bool exitOnFailure = true); +bool taur_exec(std::vector cmd, const bool exitOnFailure = true); void sanitizeStr(std::string& str); bool is_package_from_syncdb(const char* name, alpm_list_t* syncdbs); -bool commitTransactionAndRelease(bool soft = false); +bool commitTransactionAndRelease(const bool soft = false); 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); +std::string makepkg_list(const std::string_view pkg_name, const 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); +fmt::text_style getColorFromDBName(const std::string_view db_name); +std::vector filterAURPkgs(std::vector& pkgs, alpm_list_t* syncdbs, const 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); -fmt::rgb hexStringToColor(std::string_view hexstr); + const bool inverse); +std::string shell_exec(const std::string_view cmd); +std::vector split(const std::string_view text, const char delim); +fmt::rgb hexStringToColor(const std::string_view hexstr); void ctrl_d_handler(const std::istream& cin); -std::string getTitleFromVotes(float votes); +std::string getTitleFromVotes(const float votes); std::string getHomeCacheDir(); std::string getHomeConfigDir(); std::string getConfigDir(); std::string getCacheDir(); -bool makepkg_exec(std::vector const& args, bool exitOnFailure = true); -bool pacman_exec(std::string_view op, std::vector const& args, bool exitOnFailure = true, - bool root = true); +bool makepkg_exec(std::vector const& args, const bool exitOnFailure = true); +bool pacman_exec(const std::string_view op, std::vector const& args, const bool exitOnFailure = true, + const bool root = true); bool util_db_search(alpm_db_t* db, alpm_list_t* needles, alpm_list_t** ret); -std::optional> askUserForPkg(const std::vector& pkgs, TaurBackend& backend, bool useGit); -std::string_view binarySearch(const std::vector& arr, std::string_view target); +std::optional> askUserForPkg(const std::vector& pkgs, TaurBackend& backend, const bool useGit); +std::string_view binarySearch(const std::vector& arr, const std::string_view target); std::vector load_aur_list(); -bool update_aur_cache(bool recursiveCall = false); +bool update_aur_cache(const bool recursiveCall = false); template struct is_fmt_convertible { - private: +private: template static auto test(int) -> decltype(fmt::to_string(std::declval()), std::true_type{}); template static auto test(...) -> std::false_type; - public: +public: static constexpr bool value = decltype(test(0))::value; }; @@ -239,7 +239,7 @@ void log_printf(log_level log, const fmt::text_style ts, std::string_view fmt, A * @returns the result, y = true, f = false, only returns def if the result is def */ template -bool askUserYorN(bool def, prompt_yn pr, Args&&... args) +bool askUserYorN(const bool def, const prompt_yn pr, const Args&&... args) { const std::string& inputs_str = fmt::format("[{}]: ", (def ? "Y/n" : "y/N")); std::string result; @@ -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(const std::vector& list, prompt_list pr, bool required = false) +std::vector askUserForList(const std::vector& list, const prompt_list pr, const 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)"); @@ -355,7 +355,7 @@ std::vector askUserForList(const std::vector& list, prompt_list pr, bool r if (result_str == "a") return list; - std::vector input_indices = split(result_str, ' '); + const std::vector& input_indices = split(result_str, ' '); int added_elements = 0; bool breakandcontinue = false; diff --git a/src/args.cpp b/src/args.cpp index 56d20e2..765e046 100644 --- a/src/args.cpp +++ b/src/args.cpp @@ -30,7 +30,7 @@ alpm_list_smart_deleter taur_targets(nullptr, free_list_and_internals); -void invalid_opt(int used, std::string_view opt1, std::string_view opt2) +void invalid_opt(const int used, const std::string_view opt1, const std::string_view opt2) { if (used) log_println(ERROR, _("invalid option: '{}' and '{}' may not be used together"), opt1, opt2); @@ -41,7 +41,7 @@ void invalid_opt(int used, std::string_view opt1, std::string_view opt2) * @param dryrun If nonzero, application state is NOT changed * @return 0 if opt was handled, 1 if it was not handled */ -int parsearg_op(int opt, int dryrun) +int parsearg_op(const int opt, const int dryrun) { switch (opt) { @@ -93,7 +93,7 @@ int parsearg_op(int opt, int dryrun) * @param opt Keycode returned by getopt_long * @return 0 on success, 1 on unkown option, 2 on invalid argument */ -int parsearg_global(int opt) +int parsearg_global(const int opt) { switch (opt) { @@ -163,7 +163,7 @@ int parsearg_query(int opt) return 0; } -int parsearg_sync(int opt) +int parsearg_sync(const int opt) { switch (opt) { @@ -192,7 +192,7 @@ int parsearg_sync(int opt) return 0; } -int parsearg_remove(int opt) +int parsearg_remove(const int opt) { switch (opt) { diff --git a/src/config.cpp b/src/config.cpp index d34ab27..57fadfb 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -99,7 +99,7 @@ void Config::initVars() this->editor.push_back(str); } - char* no_color = getenv("NO_COLOR"); + const char* no_color = getenv("NO_COLOR"); if (no_color != NULL && no_color[0] != '\0') { fmt::disable_colors = true; @@ -112,7 +112,7 @@ void Config::initVars() * using the variables under the [pacman] table in "config.toml" * @param the directory of the config file */ -void Config::loadConfigFile(std::string_view filename) +void Config::loadConfigFile(const std::string_view filename) { try { @@ -139,7 +139,7 @@ void Config::loadConfigFile(std::string_view filename) /** parse the theme file (aka "theme.toml") * @param filename The directory of the theme file */ -void Config::loadThemeFile(std::string_view filename) +void Config::loadThemeFile(const std::string_view filename) { try { diff --git a/src/main.cpp b/src/main.cpp index 303b710..5050d74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,7 +34,7 @@ std::unique_ptr config; std::unique_ptr backend; // this may be hard to read, but better than calling fmt::println multiple times -void usage(int op) +void usage(const int op) { if (op == OP_MAIN) { @@ -91,7 +91,8 @@ void test_colors() std::string timestr = std::ctime(¤t_time); timestr.pop_back(); - TaurPkg_t pkg = { + const TaurPkg_t pkg = + { .name = "TabAUR", .version = VERSION, .desc = "A customizable and lightweight AUR helper, designed to be simple but powerful.", @@ -105,6 +106,7 @@ void test_colors() if (fmt::disable_colors) fmt::println("Colors are disabled"); + log_println(DEBUG, _("Debug color: {}"), fmt::format(BOLD_COLOR(color.magenta), "(bold) magenta")); log_println(INFO, _("Info color: {}"), fmt::format(BOLD_COLOR(color.cyan), "(bold) cyan")); log_println(WARN, _("Warning color: {}"), fmt::format(BOLD_COLOR(color.yellow), "(bold) yellow")); @@ -159,8 +161,8 @@ int installPkg(alpm_list_t* pkgNames) if (!pkgNames && !op.op_s_upgrade) return false; - bool useGit = config->useGit; - path cacheDir = config->cacheDir; + const bool useGit = config->useGit; + const path& cacheDir = config->cacheDir; bool returnStatus = true; bool stat; @@ -186,7 +188,7 @@ int installPkg(alpm_list_t* pkgNames) { for (size_t i = 0; i < pkgNamesVec.size(); i++) { - std::vector pkgs = backend->search(pkgNamesVec[i], useGit, config->aurOnly, false); + const std::vector& pkgs = backend->search(pkgNamesVec[i], useGit, config->aurOnly, false); if (pkgs.empty()) { @@ -209,7 +211,7 @@ int installPkg(alpm_list_t* pkgNames) // I swear there was a comment here.. const std::vector& AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true); - for (const auto& pkg : pkgNamesVec) + for (const std::string_view pkg : pkgNamesVec) { if (std::find(AURPkgs.begin(), AURPkgs.end(), pkg) == AURPkgs.end()) pacmanPkgs.push_back(pkg.data()); @@ -221,9 +223,9 @@ int installPkg(alpm_list_t* pkgNames) if (!config->noconfirm && !AURPkgs.empty()) pkgsToReview = askUserForList(AURPkgs, PROMPT_LIST_REVIEWS); - for (auto& pkg_name : AURPkgs) + for (const std::string_view pkg_name : AURPkgs) { - const path& pkgDir = path(cacheDir) / pkg_name; + const path& pkgDir = cacheDir / pkg_name; stat = useGit ? backend->download_git(AUR_URL_GIT(pkg_name), pkgDir) : backend->download_tar(AUR_URL_TAR(pkg_name), pkgDir); @@ -237,7 +239,7 @@ int installPkg(alpm_list_t* pkgNames) for (const std::string_view pkg : pkgsToCleanBuild) { - const path& pkgDir = path(cacheDir) / pkg; + const path& pkgDir = cacheDir / pkg; if (!useGit) { log_println(INFO, _("Removing {}"), pkgDir.c_str()); @@ -255,7 +257,7 @@ int installPkg(alpm_list_t* pkgNames) // instead of creating another config variable for (const std::string_view pkg : pkgsToReview) { - const path& pkgDir = path(cacheDir) / pkg; + const path& pkgDir = cacheDir / pkg; std::vector cmd; cmd.reserve(config->editor.size()); @@ -312,7 +314,7 @@ int installPkg(alpm_list_t* pkgNames) for (const TaurPkg_t& pkg : selectedPkgs) { - path pkgDir = path(cacheDir) / pkg.name; + path pkgDir = cacheDir / pkg.name; stat = backend->handle_aur_depends(pkg, cacheDir, backend->get_all_local_pkgs(true), useGit); @@ -341,7 +343,7 @@ int installPkg(alpm_list_t* pkgNames) if (!pkgs_to_install.empty()) { log_println(DEBUG, _("Installing {}"), fmt::join(pkgNamesVec, " ")); - pkgs_to_install.erase(pkgs_to_install.length() - 1); + pkgs_to_install.pop_back(); if (!pacman_exec("-U", split(pkgs_to_install, ' '), false)) { log_println(ERROR, _("Failed to install {}"), fmt::join(pkgNamesVec, " ")); @@ -352,8 +354,8 @@ int installPkg(alpm_list_t* pkgNames) if (!pkgs_failed_to_build.empty()) { pkgs_failed_to_build.erase(pkgs_failed_to_build.end() - 1); - log_println(WARN, fg(color.red), _("Failed to upgrade: {}"), pkgs_failed_to_build); - log_println(INFO, fg(color.cyan), _("Tip: try to run taur with \"-S {}\" (e.g \"taur -S {}\")"), + log_println(WARN, fg(color.red), _("Failed to upgrade: {}"), pkgs_failed_to_build); + log_println(INFO, fg(color.cyan), _("Tip: try to run taur with \"-S {}\" and cleanbuild every failed packages"), pkgs_failed_to_build, pkgs_failed_to_build); } return returnStatus; @@ -389,7 +391,7 @@ bool removePkg(alpm_list_t* pkgNames) alpm_list_smart_pointer ret(alpm_list_join(exactMatches, searchResults), alpm_list_free); - size_t ret_size = alpm_list_count(ret.get()); + const size_t ret_size = alpm_list_count(ret.get()); if (ret_size == 0) { @@ -422,7 +424,7 @@ bool removePkg(alpm_list_t* pkgNames) if (pkg >= pkgs.end()) continue; - size_t pkgIndex = std::distance(pkgs.begin(), pkg); + const size_t pkgIndex = std::distance(pkgs.begin(), pkg); alpm_list_nth(ret.get(), pkgIndex); @@ -445,7 +447,7 @@ bool removePkg(alpm_list_t* pkgNames) } // take control of the list and pass it to the smart pointer - alpm_list_smart_pointer finalList = make_list_smart_pointer(finalPackageListStart); + const alpm_list_smart_pointer& finalList = make_list_smart_pointer(finalPackageListStart); return backend->remove_pkgs(finalList); } @@ -483,7 +485,9 @@ bool queryPkgs(alpm_list_t* pkgNames) pkgs_name.push_back(alpm_pkg_get_name(reinterpret_cast(result->data))); pkgs_ver.push_back(alpm_pkg_get_version(reinterpret_cast(result->data))); } - } else { + } + else + { for (; pkgNames; pkgNames = pkgNames->next) { const char* strname = reinterpret_cast(pkgNames->data); diff --git a/src/taur.cpp b/src/taur.cpp index c190f11..790082a 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -53,7 +53,7 @@ bool TaurBackend::download_tar(const std::string_view url, const path& out_path) * @param out_path the path to extract the folder, for git folders, this will be where the repo is cloned. * @returns bool, true = success, false = failure. */ -bool TaurBackend::download_pkg(std::string_view url, path out_path) +bool TaurBackend::download_pkg(const std::string_view url, const path out_path) { if (hasEnding(url, ".git")) return this->download_git(url, out_path); @@ -63,7 +63,7 @@ bool TaurBackend::download_pkg(std::string_view url, path out_path) return false; } -std::string getUrl(const rapidjson::Value& pkgJson, bool returnGit = false) +static std::string getUrl(const rapidjson::Value& pkgJson, const bool returnGit = false) { if (returnGit) return fmt::format("https://aur.archlinux.org/{}.git", pkgJson["Name"].GetString()); @@ -72,7 +72,7 @@ std::string getUrl(const rapidjson::Value& pkgJson, bool returnGit = false) return fmt::format("https://aur.archlinux.org{}", pkgJson["URLPath"].GetString()); } -TaurPkg_t parsePkg(rapidjson::Value& pkgJson, bool returnGit = false) +static TaurPkg_t parsePkg(const rapidjson::Value& pkgJson, const bool returnGit = false) { std::vector makedepends, depends, totaldepends, licenses; @@ -140,7 +140,7 @@ TaurPkg_t parsePkg(rapidjson::Value& pkgJson, bool returnGit = false) return out; } -std::optional TaurBackend::fetch_pkg(std::string_view pkg, bool returnGit) +std::optional TaurBackend::fetch_pkg(const std::string_view pkg, const bool returnGit) { const std::string& urlStr = "https://aur.archlinux.org/rpc/v5/info/" + cpr::util::urlEncode(pkg.data()); @@ -158,12 +158,13 @@ std::optional TaurBackend::fetch_pkg(std::string_view pkg, bool retur return {}; } -std::vector TaurBackend::fetch_pkgs(std::vector const& pkgs, bool returnGit) +std::vector TaurBackend::fetch_pkgs(std::vector const& pkgs, const bool returnGit) { if (pkgs.empty()) return {}; std::string urlStr = "https://aur.archlinux.org/rpc/v5/info?arg%5B%5D=" + pkgs[0]; + urlStr.reserve(pkgs.size()); for (size_t i = 1; i < pkgs.size(); i++) urlStr += ("&arg%5B%5D=" + pkgs[i]); @@ -192,7 +193,7 @@ std::vector TaurBackend::fetch_pkgs(std::vector const& p * if not, it will not initialize it and not release it. * @return success. */ -bool TaurBackend::remove_pkg(alpm_pkg_t* pkg, bool ownTransaction) +bool TaurBackend::remove_pkg(alpm_pkg_t* pkg, const bool ownTransaction) { if (!pkg) return false; @@ -222,7 +223,7 @@ bool TaurBackend::remove_pkg(alpm_pkg_t* pkg, bool ownTransaction) * @param pkgs alpm list of alpm_pkg_t pointers to remove. * @return success. */ -bool TaurBackend::remove_pkgs(alpm_list_smart_pointer& pkgs) +bool TaurBackend::remove_pkgs(const alpm_list_smart_pointer& pkgs) { if (!pkgs) return false; @@ -266,7 +267,7 @@ bool TaurBackend::remove_pkgs(alpm_list_smart_pointer& pkgs) return true; } -bool TaurBackend::build_pkg(std::string_view pkg_name, std::string_view extracted_path, bool alreadyprepared) +bool TaurBackend::build_pkg(const std::string_view pkg_name, const std::string_view extracted_path, const bool alreadyprepared) { std::filesystem::current_path(extracted_path); @@ -298,7 +299,7 @@ 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, const path& out_path, std::vector const& localPkgs, const bool useGit) { log_println(DEBUG, "pkg.name = {}", pkg.name); log_println(DEBUG, "pkg.totaldepends = {}", pkg.totaldepends); @@ -348,11 +349,11 @@ bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path& out_path, std:: if (sub_pkg_depend.empty()) continue; - std::optional oSubDepend = this->fetch_pkg(sub_pkg_depend, useGit); + const std::optional& oSubDepend = this->fetch_pkg(sub_pkg_depend, useGit); if (!oDepend) continue; - TaurPkg_t subDepend = oSubDepend.value(); + const TaurPkg_t& subDepend = oSubDepend.value(); alreadyExists = false; for (size_t j = 0; (j < localPkgs.size() && !alreadyExists); j++) @@ -444,7 +445,7 @@ bool TaurBackend::handle_aur_depends(const TaurPkg_t& pkg, path& out_path, std:: return true; } -bool TaurBackend::update_all_aur_pkgs(path& cacheDir, bool useGit) +bool TaurBackend::update_all_aur_pkgs(const path& cacheDir, const bool useGit) { const std::vector& localPkgs = this->get_all_local_pkgs(true); @@ -529,14 +530,14 @@ bool TaurBackend::update_all_aur_pkgs(path& cacheDir, bool useGit) // onlinePkgs[i].name); continue; // } - bool isGitPackage = hasEnding(potentialUpgradeTargetTo.name, "-git"); + const bool isGitPackage = hasEnding(potentialUpgradeTargetTo.name, "-git"); // if (!isGitPackage && localPkgs[pkgIndex].version == onlinePkgs[i].version) { // log_println(DEBUG, "pkg {} has no update, local: {}, online: {}, skipping!", localPkgs[pkgIndex].name, // localPkgs[pkgIndex].version, onlinePkgs[i].version); continue; // } - path pkgDir = cacheDir / potentialUpgradeTargetTo.name; + const path& pkgDir = cacheDir / potentialUpgradeTargetTo.name; log_println(INFO, _("Downloading {}."), potentialUpgradeTargetTo.name); @@ -655,13 +656,11 @@ bool TaurBackend::update_all_aur_pkgs(path& cacheDir, bool useGit) // all AUR local packages // returns a barebones TaurPkg_t structure, with no description/depends list -std::vector TaurBackend::get_all_local_pkgs(bool aurOnly) +std::vector TaurBackend::get_all_local_pkgs(const bool aurOnly) { std::vector pkgs; - alpm_list_t *pkg, *syncdbs; - - syncdbs = config.repos; + alpm_list_t *pkg, *syncdbs = config.repos; for (pkg = alpm_db_get_pkgcache(alpm_get_localdb(config.handle)); pkg; pkg = pkg->next) pkgs.push_back((alpm_pkg_t*)(pkg->data)); @@ -685,7 +684,7 @@ std::vector TaurBackend::get_all_local_pkgs(bool aurOnly) // 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. -std::vector TaurBackend::getPkgFromJson(rapidjson::Document& doc, bool useGit) +std::vector TaurBackend::getPkgFromJson(const rapidjson::Document& doc, const bool useGit) { int resultcount = doc["resultcount"].GetInt(); @@ -697,7 +696,7 @@ std::vector TaurBackend::getPkgFromJson(rapidjson::Document& doc, boo return out; } -std::vector TaurBackend::search_pac(std::string_view query) +std::vector TaurBackend::search_pac(const std::string_view query) { // we search for the package name and print only the name, not the description alpm_list_t* syncdbs = config.repos; @@ -740,15 +739,16 @@ std::vector TaurBackend::search_pac(std::string_view query) // Returns an optional that is empty if an error occurs // status will be set to -1 in the case of an error as well. -std::vector TaurBackend::search(std::string_view query, bool useGit, bool aurOnly, bool checkExactMatch) +std::vector TaurBackend::search(const std::string_view query, const bool useGit, const bool aurOnly, const bool checkExactMatch) { if (query.empty()) return {}; + // link to AUR API. Took search pattern from yay 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()); - std::string_view raw_text_response = cpr::Get(url).text; + const std::string_view raw_text_response = cpr::Get(url).text; rapidjson::Document json_response; json_response.Parse(raw_text_response.data()); diff --git a/src/util.cpp b/src/util.cpp index cde8060..3bf3065 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -122,14 +122,14 @@ static void optdeplist_display(alpm_pkg_t* pkg, unsigned short cols = getcols()) } // https://stackoverflow.com/questions/874134/find-out-if-std::string-ends-with-another-std::string-in-c#874160 -bool hasEnding(std::string_view fullString, std::string_view ending) +bool hasEnding(const std::string_view fullString, const std::string_view ending) { if (ending.length() > fullString.length()) return false; return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending)); } -bool hasStart(std::string_view fullString, std::string_view start) +bool hasStart(const std::string_view fullString, const std::string_view start) { if (start.length() > fullString.length()) return false; @@ -137,7 +137,7 @@ bool hasStart(std::string_view fullString, std::string_view start) } // clang-format off -bool isInvalid(char c) +bool isInvalid(const unsigned char c) { return !isprint(c); } void sanitizeStr(std::string& str) @@ -166,7 +166,7 @@ bool is_package_from_syncdb(const char* name, alpm_list_t* syncdbs) } // soft means it won't return false (or even try) if the list is empty -bool commitTransactionAndRelease(bool soft) +bool commitTransactionAndRelease(const bool soft) { alpm_handle_t* handle = config->handle; @@ -203,7 +203,7 @@ bool commitTransactionAndRelease(bool soft) bool prepareStatus = alpm_trans_prepare(handle, &data) == 0; if (!prepareStatus) { - alpm_errno_t err = alpm_errno(handle); + const alpm_errno_t err = alpm_errno(handle); log_println(ERROR, _("Failed to prepare transaction ({})."), alpm_strerror(err)); /* TODO: we can use the errno and (data) to list off more precise information about the error. */ alpm_pkg_t* pkg = nullptr; @@ -212,7 +212,7 @@ bool commitTransactionAndRelease(bool soft) case ALPM_ERR_PKG_INVALID_ARCH: for (alpm_list_t* i = data; i; i = i->next) { - std::string_view pkgName = (char*)(i->data); + const std::string_view pkgName = (char*)(i->data); log_println(ERROR, "This package ({}) is built on an invalid architecture.", pkgName); free(i->data); } @@ -267,14 +267,16 @@ bool commitTransactionAndRelease(bool soft) alpm_conflict_free(conflict); } break; - default: break; + + default: + break; } } - bool commitStatus = alpm_trans_commit(handle, &data) == 0; + const bool commitStatus = alpm_trans_commit(handle, &data) == 0; if (!commitStatus) { - alpm_errno_t err = alpm_errno(handle); + const alpm_errno_t err = alpm_errno(handle); log_println(ERROR, _("Failed to commit transaction ({})."), alpm_strerror(err)); /* similarly. */ switch (err) @@ -306,7 +308,7 @@ bool commitTransactionAndRelease(bool soft) case ALPM_ERR_PKG_INVALID_SIG: for (alpm_list_t* i = data; i; i = i->next) { - std::string_view name = (char*)i->data; + const std::string_view name = reinterpret_cast(i->data); log_println(ERROR, "Package {} is corrupt or invalid!", name); free(i->data); } @@ -315,7 +317,7 @@ bool commitTransactionAndRelease(bool soft) } } - bool releaseStatus = alpm_trans_release(handle) == 0; + const bool releaseStatus = alpm_trans_release(handle) == 0; if (!releaseStatus) log_println(ERROR, _("Failed to release transaction ({})."), alpm_strerror(alpm_errno(handle))); @@ -376,18 +378,18 @@ fmt::rgb hexStringToColor(std::string_view hexstr) int intValue; ss >> intValue; - int red = (intValue >> 16) & 0xFF; - int green = (intValue >> 8) & 0xFF; - int blue = intValue & 0xFF; + const int red = (intValue >> 16) & 0xFF; + const int green = (intValue >> 8) & 0xFF; + const int blue = intValue & 0xFF; return fmt::rgb(red, green, blue); } // http://stackoverflow.com/questions/478898/ddg#478960 -std::string shell_exec(std::string_view cmd) +std::string shell_exec(const std::string_view cmd) { - std::array buffer; - std::string result; + std::array buffer; + std::string result; std::unique_ptr pipe(popen(cmd.data(), "r"), pclose); if (!pipe) @@ -397,13 +399,13 @@ std::string shell_exec(std::string_view cmd) result += buffer.data(); // why there is a '\n' at the end?? - if (!result.empty() && result[result.length() - 1] == '\n') - result.erase(result.length() - 1); + if (!result.empty() && result.back() == '\n') + result.pop_back(); return result; } // https://stackoverflow.com/questions/4654636/how-to-determine-if-a-std::string-is-a-number-with-c#4654718 -bool is_numerical(std::string_view s, bool allowSpace) +bool is_numerical(const std::string_view s, const bool allowSpace) { if (allowSpace) return !s.empty() && std::find_if(s.begin(), s.end(), @@ -413,7 +415,7 @@ bool is_numerical(std::string_view s, bool allowSpace) std::find_if(s.begin(), s.end(), [](unsigned char c) { return (!std::isdigit(c)); }) == s.end(); } -bool taur_read_exec(std::vector cmd, std::string& output, bool exitOnFailure) +bool taur_read_exec(std::vector cmd, std::string& output, const bool exitOnFailure) { int pipeout[2]; @@ -479,11 +481,11 @@ bool taur_read_exec(std::vector cmd, std::string& output, bool exit * @param exitOnFailure Whether to call exit(1) on command failure. * @return true if the command successed, else false */ -bool taur_exec(std::vector cmd_str, bool exitOnFailure) +bool taur_exec(const std::vector cmd_str, const bool exitOnFailure) { std::vector cmd; - for (std::string& str : cmd_str) - cmd.push_back(str.c_str()); + for (const std::string_view str : cmd_str) + cmd.push_back(str.data()); int pid = fork(); @@ -524,9 +526,9 @@ bool taur_exec(std::vector cmd_str, bool exitOnFailure) * @param exitOnFailure Whether to call exit(1) on command failure. * @return true if the command successed, else false */ -bool makepkg_exec(std::vector const& args, bool exitOnFailure) +bool makepkg_exec(std::vector const& args, const bool exitOnFailure) { - std::vector cmd = { config->makepkgBin }; + std::vector cmd{ config->makepkgBin }; if (config->noconfirm) cmd.push_back("--noconfirm"); @@ -551,7 +553,7 @@ bool makepkg_exec(std::vector const& args, bool exitOnFailure) * @param root If pacman should be executed as root (Default true) * @return true if the command successed, else false */ -bool pacman_exec(std::string_view op, std::vector const& args, bool exitOnFailure, bool root) +bool pacman_exec(const std::string_view op, std::vector const& args, const bool exitOnFailure, const bool root) { std::vector cmd; @@ -594,7 +596,7 @@ void free_list_and_internals(alpm_list_t* list) * @param db_name The database name * @return database's color in bold */ -fmt::text_style getColorFromDBName(std::string_view db_name) +fmt::text_style getColorFromDBName(const std::string_view db_name) { switch (fnv1a16::hash(db_name)) { @@ -638,6 +640,7 @@ void printPkgInfo(const TaurPkg_t& pkg, const std::string_view db_name) fmt::println(BOLD_COLOR(color.installed), "[Installed]"); else fmt::print("\n"); + fmt::println(" {}", pkg.desc); } @@ -680,7 +683,7 @@ void getFileValue(u_short& iterIndex, const std::string& line, std::string& str, } // faster than `makepkg --packagelist` -std::string makepkg_list(std::string_view pkg_name, std::string_view path) +std::string makepkg_list(const std::string_view pkg_name, const std::string_view path) { std::ifstream pkgbuild_f(fmt::format("{}/PKGBUILD", path), std::ios::in); @@ -794,7 +797,7 @@ bool util_db_search(alpm_db_t* db, alpm_list_t* needles, alpm_list_t** ret) std::vector load_aur_list() { - path file_path = config->cacheDir / "packages.aur"; + const path& file_path = config->cacheDir / "packages.aur"; std::ifstream infile(file_path); if (!infile.good()) die(_("Failed to open {}"), file_path.c_str()); @@ -808,9 +811,9 @@ std::vector load_aur_list() return aur_list; } -bool download_aur_cache(path file_path) +bool download_aur_cache(const path& file_path) { - cpr::Response r = cpr::Get(cpr::Url{ AUR_URL "/packages.gz" }); + const cpr::Response& r = cpr::Get(cpr::Url{ AUR_URL "/packages.gz" }); if (r.status_code == 200) { @@ -835,9 +838,9 @@ bool download_aur_cache(path file_path) // This function will automatically try again after downloading the file, if not already present. // Do not call this with true unless you do not want this behavior. // It is, by default, false. -bool update_aur_cache(bool recursiveCall) +bool update_aur_cache(const bool recursiveCall) { - path file_path = config->cacheDir / "packages.aur"; + const path& file_path = config->cacheDir / "packages.aur"; struct stat file_stat; if (stat(file_path.c_str(), &file_stat) != 0) @@ -874,7 +877,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(const std::vector& pkgs, TaurBackend& backend, bool useGit) +std::optional> askUserForPkg(const std::vector& pkgs, TaurBackend& backend, const bool useGit) { if (pkgs.size() == 1) { @@ -991,7 +994,7 @@ std::vector filterAURPkgsNames(std::vector& return out; } -std::string getTitleFromVotes(float votes) +std::string getTitleFromVotes(const float votes) { if (votes < 2) return _("Untrustable"); @@ -1036,7 +1039,7 @@ std::string getHomeCacheDir() */ std::string getHomeConfigDir() { - char* dir = getenv("XDG_CONFIG_HOME"); + const char* dir = getenv("XDG_CONFIG_HOME"); if (dir != NULL && dir[0] != '\0' && std::filesystem::exists(dir)) { std::string str_dir(dir); @@ -1044,7 +1047,7 @@ std::string getHomeConfigDir() } else { - char* home = getenv("HOME"); + const char* home = getenv("HOME"); if (home == nullptr) die(_("Failed to find $HOME, set it to your home directory!")); @@ -1070,7 +1073,7 @@ std::string getConfigDir() std::string getCacheDir() { return getHomeCacheDir() + "/TabAUR"; } -std::vector split(std::string_view text, char delim) +std::vector split(const std::string_view text, const char delim) { std::string line; std::vector vec;