Skip to content

Commit

Permalink
general: code improvement
Browse files Browse the repository at this point in the history
reference, references everywhere &
  • Loading branch information
Toni500github committed Sep 10, 2024
1 parent 49b3475 commit 024e901
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 114 deletions.
12 changes: 6 additions & 6 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 <typename T>
T getConfigValue(const std::string& value, T fallback)
T getConfigValue(const std::string& value, T&& fallback)
{
std::optional<T> ret = this->tbl.at_path(value).value<T>();
if constexpr (toml::is_string<T>) // if we want to get a value that's a std::string
Expand Down
10 changes: 5 additions & 5 deletions include/taur.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ class TaurBackend
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);
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<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 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);
bool update_all_aur_pkgs(path& cacheDir, bool useGit);
std::vector<TaurPkg_t> get_all_local_pkgs(bool aurOnly);
};

Expand Down
14 changes: 7 additions & 7 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ 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<const char*> cmd, std::string& output, bool exitOnFailure = true);
void interruptHandler(int);
bool taur_exec(std::vector<std::string> cmd, 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);
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<alpm_pkg_t*> filterAURPkgs(std::vector<alpm_pkg_t*> pkgs, alpm_list_t* syncdbs, bool inverse);
std::vector<std::string_view> filterAURPkgsNames(std::vector<std::string_view> pkgs, alpm_list_t* syncdbs,
std::vector<alpm_pkg_t*> filterAURPkgs(std::vector<alpm_pkg_t*>& pkgs, alpm_list_t* syncdbs, 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);
Expand All @@ -122,7 +122,7 @@ bool pacman_exec(std::string_view op, std::vector<std::
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(std::vector<TaurPkg_t> pkgs, TaurBackend& backend, bool useGit);
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::vector<std::string> load_aur_list();
bool update_aur_cache(bool recursiveCall = false);
Expand Down Expand Up @@ -241,7 +241,7 @@ void log_printf(log_level log, const fmt::text_style ts, std::string_view fmt, A
template <typename... Args>
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)
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(std::vector<T>& list, prompt_list pr, bool required = false)
std::vector<T> askUserForList(const std::vector<T>& 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)");
Expand Down
32 changes: 16 additions & 16 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<std::string>("pacman.ConfigFile", "/etc/pacman.conf"));
}

/** parse the theme file (aka "theme.toml")
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 25 additions & 23 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_time);
timestr.erase(timestr.length() - 1);
timestr.pop_back();

TaurPkg_t pkg = {
.name = "TabAUR",
Expand Down Expand Up @@ -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<std::string_view> AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true);
const std::vector<std::string_view>& AURPkgs = filterAURPkgsNames(pkgNamesVec, alpm_get_syncdbs(config->handle), true);

for (const auto& pkg : pkgNamesVec)
{
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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<std::string> cmd;
cmd.reserve(config->editor.size());
for (auto& str : config->editor)
cmd.push_back(str);

cmd.push_back((pkgDir / "PKGBUILD").string());

taur_exec(cmd);
Expand Down Expand Up @@ -296,19 +298,19 @@ int installPkg(alpm_list_t* pkgNames)

for (size_t i = 0; i < AURPkgs.size(); i++)
{
std::vector<TaurPkg_t> pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true);
const std::vector<TaurPkg_t>& pkgs = backend->search(AURPkgs[i], useGit, config->aurOnly, true);

std::optional<std::vector<TaurPkg_t>> oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit);
const std::optional<std::vector<TaurPkg_t>>& oSelectedPkgs = askUserForPkg(pkgs, *backend, useGit);

if (!oSelectedPkgs)
{
returnStatus = false;
continue;
}

std::vector<TaurPkg_t> selectedPkgs = oSelectedPkgs.value();
const std::vector<TaurPkg_t>& selectedPkgs = oSelectedPkgs.value();

for (TaurPkg_t& pkg : selectedPkgs)
for (const TaurPkg_t& pkg : selectedPkgs)
{
path pkgDir = path(cacheDir) / pkg.name;

Expand All @@ -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);
Expand Down Expand Up @@ -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<alpm_pkg_t *>(ret->data));

std::vector<std::string_view> 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_pkg_t *>(alpm_list_nth(ret.get(), i)->data)));

std::vector<std::string_view> includedPkgs = askUserForList<std::string_view>(pkgs, PROMPT_LIST_REMOVE_PKGS, true);
const std::vector<std::string_view>& includedPkgs = askUserForList<std::string_view>(pkgs, PROMPT_LIST_REMOVE_PKGS, true);

alpm_list_t* finalPackageList = nullptr;
alpm_list_t* finalPackageListStart = nullptr;
Expand All @@ -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;
Expand Down Expand Up @@ -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<const char *> pkgs_name, pkgs_ver;
std::vector<alpm_pkg_t *> pkgs;
alpm_db_t* localdb = alpm_get_localdb(config->handle);
std::vector<alpm_pkg_t *> pkgs;
alpm_db_t* localdb = alpm_get_localdb(config->handle);

// just -Q, no options other than --quiet and global ones
if (!pkgNames)
Expand All @@ -481,11 +484,10 @@ bool queryPkgs(alpm_list_t* pkgNames)
pkgs_ver.push_back(alpm_pkg_get_version(reinterpret_cast<alpm_pkg_t *>(result->data)));
}
} else {
alpm_pkg_t* pkg;
for (; pkgNames; pkgNames = pkgNames->next)
{
const char* strname = reinterpret_cast<const char *>(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);
Expand Down
Loading

0 comments on commit 024e901

Please sign in to comment.