Skip to content

Commit

Permalink
general: code improvement pt2
Browse files Browse the repository at this point in the history
const, consts everywhere

I'm making these changes because I use them on customfetch too and they are great
  • Loading branch information
Toni500github committed Sep 10, 2024
1 parent 024e901 commit 14a2350
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 128 deletions.
10 changes: 5 additions & 5 deletions include/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 11 additions & 11 deletions include/taur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaurPkg_t> getPkgFromJson(rapidjson::Document& doc, bool useGit);
std::vector<TaurPkg_t> search_pac(std::string_view query);
std::vector<TaurPkg_t> search(std::string_view query, bool useGit, bool aurOnly, bool checkExactMatch = true);
std::vector<TaurPkg_t> getPkgFromJson(const rapidjson::Document& doc, const bool useGit);
std::vector<TaurPkg_t> search_pac(const std::string_view query);
std::vector<TaurPkg_t> 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<TaurPkg_t> fetch_pkg(std::string_view pkg, bool returnGit);
std::vector<TaurPkg_t> fetch_pkgs(std::vector<std::string> 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<TaurPkg_t> 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<TaurPkg_t> get_all_local_pkgs(bool aurOnly);
std::optional<TaurPkg_t> fetch_pkg(const std::string_view pkg, const bool returnGit);
std::vector<TaurPkg_t> fetch_pkgs(std::vector<std::string> 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<TaurPkg_t> 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<TaurPkg_t> get_all_local_pkgs(const bool aurOnly);
};

inline std::string built_pkg, pkgs_to_install, pkgs_failed_to_build;
Expand Down
50 changes: 25 additions & 25 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*> 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<const char*> cmd, std::string& output, const bool exitOnFailure = true);
void interruptHandler(int);
bool taur_exec(std::vector<std::string> cmd, bool exitOnFailure = true);
bool taur_exec(std::vector<std::string> 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<alpm_pkg_t*> filterAURPkgs(std::vector<alpm_pkg_t*>& pkgs, alpm_list_t* syncdbs, bool inverse);
fmt::text_style getColorFromDBName(const std::string_view db_name);
std::vector<alpm_pkg_t*> filterAURPkgs(std::vector<alpm_pkg_t*>& pkgs, alpm_list_t* syncdbs, const bool inverse);
std::vector<std::string_view> filterAURPkgsNames(std::vector<std::string_view>& pkgs, alpm_list_t* syncdbs,
bool inverse);
std::string shell_exec(std::string_view cmd);
std::vector<std::string> 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<std::string> 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<std::string> const& args, bool exitOnFailure = true);
bool pacman_exec(std::string_view op, std::vector<std::string> const& args, bool exitOnFailure = true,
bool root = true);
bool makepkg_exec(std::vector<std::string> const& args, const bool exitOnFailure = true);
bool pacman_exec(const std::string_view op, std::vector<std::string> 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<std::vector<TaurPkg_t>> askUserForPkg(const std::vector<TaurPkg_t>& pkgs, TaurBackend& backend, bool useGit);
std::string_view binarySearch(const std::vector<std::string>& arr, std::string_view target);
std::optional<std::vector<TaurPkg_t>> askUserForPkg(const std::vector<TaurPkg_t>& pkgs, TaurBackend& backend, const bool useGit);
std::string_view binarySearch(const std::vector<std::string>& arr, const std::string_view target);
std::vector<std::string> load_aur_list();
bool update_aur_cache(bool recursiveCall = false);
bool update_aur_cache(const bool recursiveCall = false);

template <typename T>
struct is_fmt_convertible
{
private:
private:
template <typename U>
static auto test(int) -> decltype(fmt::to_string(std::declval<U>()), std::true_type{});

template <typename>
static auto test(...) -> std::false_type;

public:
public:
static constexpr bool value = decltype(test<T>(0))::value;
};

Expand Down Expand Up @@ -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 <typename... Args>
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;
Expand Down Expand Up @@ -309,7 +309,7 @@ bool askUserYorN(bool def, prompt_yn pr, Args&&... args)
* @returns the resulting list, empty if anything bad happens.
*/
template <typename T, typename = std::enable_if_t<is_fmt_convertible_v<T>>>
std::vector<T> askUserForList(const std::vector<T>& list, prompt_list pr, bool required = false)
std::vector<T> askUserForList(const std::vector<T>& 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)");
Expand Down Expand Up @@ -355,7 +355,7 @@ std::vector<T> askUserForList(const std::vector<T>& list, prompt_list pr, bool r
if (result_str == "a")
return list;

std::vector<std::string> input_indices = split(result_str, ' ');
const std::vector<std::string>& input_indices = split(result_str, ' ');

int added_elements = 0;
bool breakandcontinue = false;
Expand Down
10 changes: 5 additions & 5 deletions src/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -163,7 +163,7 @@ int parsearg_query(int opt)
return 0;
}

int parsearg_sync(int opt)
int parsearg_sync(const int opt)
{
switch (opt)
{
Expand Down Expand Up @@ -192,7 +192,7 @@ int parsearg_sync(int opt)
return 0;
}

int parsearg_remove(int opt)
int parsearg_remove(const int opt)
{
switch (opt)
{
Expand Down
6 changes: 3 additions & 3 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
40 changes: 22 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::unique_ptr<Config> config;
std::unique_ptr<TaurBackend> 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)
{
Expand Down Expand Up @@ -91,7 +91,8 @@ void test_colors()
std::string timestr = std::ctime(&current_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.",
Expand All @@ -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"));
Expand Down Expand Up @@ -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;
Expand All @@ -186,7 +188,7 @@ int installPkg(alpm_list_t* pkgNames)
{
for (size_t i = 0; i < pkgNamesVec.size(); i++)
{
std::vector<TaurPkg_t> pkgs = backend->search(pkgNamesVec[i], useGit, config->aurOnly, false);
const std::vector<TaurPkg_t>& pkgs = backend->search(pkgNamesVec[i], useGit, config->aurOnly, false);

if (pkgs.empty())
{
Expand All @@ -209,7 +211,7 @@ int installPkg(alpm_list_t* pkgNames)
// I swear there was a comment here..
const std::vector<std::string_view>& 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());
Expand All @@ -221,9 +223,9 @@ int installPkg(alpm_list_t* pkgNames)
if (!config->noconfirm && !AURPkgs.empty())
pkgsToReview = askUserForList<std::string_view>(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);
Expand All @@ -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());
Expand All @@ -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<std::string> cmd;
cmd.reserve(config->editor.size());
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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, " "));
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -483,7 +485,9 @@ bool queryPkgs(alpm_list_t* pkgNames)
pkgs_name.push_back(alpm_pkg_get_name(reinterpret_cast<alpm_pkg_t *>(result->data)));
pkgs_ver.push_back(alpm_pkg_get_version(reinterpret_cast<alpm_pkg_t *>(result->data)));
}
} else {
}
else
{
for (; pkgNames; pkgNames = pkgNames->next)
{
const char* strname = reinterpret_cast<const char *>(pkgNames->data);
Expand Down
Loading

0 comments on commit 14a2350

Please sign in to comment.