Skip to content

Commit

Permalink
New version (0.6.8)
Browse files Browse the repository at this point in the history
New version (0.6.8)
  • Loading branch information
BurntRanch authored Jul 1, 2024
2 parents 4f52be3 + 7ab073e commit 9ee5eb6
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 209 deletions.
1 change: 1 addition & 0 deletions include/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum {
OP_SYNC,
OP_REM,
OP_QUERY,
OP_UPGRADE,
OP_PACMAN, // when it's different from -S,R,Q we gonna use pacman
};

Expand Down
34 changes: 1 addition & 33 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ using std::filesystem::path;
// so we don't need to include util.hpp for getConfigValue()
string expandVar(string& str);

enum types {
STR,
BOOL
};

struct strOrBool {
types valueType;
string stringValue = "";
bool boolValue = false;
};

struct _color_t {
fmt::rgb red;
fmt::rgb green;
Expand Down Expand Up @@ -77,36 +66,19 @@ class Config {
// alpm transaction flags
int flags;

std::map<string, strOrBool> overrides;

Config();
Config(string_view configFile, string_view themeFile, string_view configDir);
~Config();

void init(string &configFile, string &themeFile, string_view configDir);
void initVars();
void initColors();

bool isInitialized();

void loadConfigFile(string_view filename);
void loadPacmanConfigFile(string filename);
void loadThemeFile(string_view filename);

// stupid c++ that wants template functions in header
template <typename T>
T getConfigValue(const string& value, T fallback) {
auto overridePos = overrides.find(value);

// user wants a bool (overridable), we found an override matching the name, and the override is a bool.
if constexpr (std::is_same<T, bool>())
if (overridePos != overrides.end() && overrides[value].valueType == BOOL)
return overrides[value].boolValue;

// user wants a str (overridable), we found an override matching the name, and the override is a str.
if constexpr (std::is_same<T, string>())
if (overridePos != overrides.end() && overrides[value].valueType == STR)
return overrides[value].stringValue;

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 string
return ret ? expandVar(ret.value()) : expandVar(fallback);
Expand All @@ -119,7 +91,6 @@ class Config {

private:
toml::table tbl, theme_tbl;
bool initialized = false;
};

extern std::unique_ptr<Config> config;
Expand Down Expand Up @@ -200,7 +171,4 @@ gray = "#5a5a5a"
#index = "#ff11cc"
)#";

inline string configfile;
inline string themefile;

#endif
7 changes: 6 additions & 1 deletion include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ enum prompt_yn {
PROMPT_YN_CONTINUE_WITHOUT_DIFF,
PROMPT_YN_EDIT_PKGBUILD,
PROMPT_YN_PROCEED_INSTALL,
PROMPT_YN_PROCEED_UPGRADE,
PROMPT_YN_PROCEED_TRANSACTION,
PROMPT_YN_CLEANBUILD,
};
Expand Down Expand Up @@ -90,7 +91,7 @@ string expandVar(string& str);
bool is_numerical(string_view s, bool allowSpace = false);
bool taur_read_exec(vector<const char *> cmd, string& output, bool exitOnFailure = true);
void interruptHandler(int);
bool taur_exec(vector<const char *> cmd, bool exitOnFailure = true);
bool taur_exec(vector<string> cmd, bool exitOnFailure = true);
void sanitizeStr(string& str);
bool is_package_from_syncdb(const char *name, alpm_list_t *syncdbs);
bool commitTransactionAndRelease(bool soft = false);
Expand Down Expand Up @@ -255,6 +256,10 @@ bool askUserYorN(bool def, prompt_yn pr, Args&&... args) {
log_printf(INFO, BOLD, _("Proceed with the installation? {}"), inputs_str);
NOCONFIRM(YES);
break;
case PROMPT_YN_PROCEED_UPGRADE:
log_printf(INFO, BOLD, _("Would you like to upgrade the above packages? {}"), inputs_str);
NOCONFIRM(YES);
break;
case PROMPT_YN_PROCEED_TRANSACTION:
log_printf(INFO, BOLD, _("Would you like to proceed with this transaction? {}"), inputs_str);
NOCONFIRM(YES);
Expand Down
19 changes: 9 additions & 10 deletions src/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ int parsearg_op(int opt, int dryrun) {
op.op = (op.op != OP_MAIN ? 0 : OP_PACMAN); break;
case 'U':
if(dryrun) break;
op.op = (op.op != OP_MAIN ? 0 : OP_PACMAN); break;
op.op = (op.op != OP_MAIN ? 0 : OP_UPGRADE);
op.requires_root = true;
break;
case 'V':
if(dryrun) break;
op.version = 1; break;
Expand All @@ -88,34 +90,31 @@ int parsearg_op(int opt, int dryrun) {
int parsearg_global(int opt) {
switch (opt) {
case OP_CACHEDIR:
config->overrides["general.cacheDir"] = {STR, strndup(optarg, PATH_MAX)};
config->cacheDir = strndup(optarg, PATH_MAX);
break;
case OP_COLORS:
fmt::disable_colors = !((bool)std::atoi(optarg));
config->overrides["general.colors"] = {BOOL, "", (bool)std::atoi(optarg)};
config->colors = (bool)std::atoi(optarg);
break;
case OP_DEBUG:
config->overrides["general.debug"] = {BOOL, "", true};
config->debug = true;
break;
case OP_AURONLY:
case 'a':
config->overrides["general.aurOnly"] = {BOOL, "", true};
config->aurOnly = true;
break;
case OP_SUDO:
config->overrides["general.sudo"] = {STR, strndup(optarg, PATH_MAX)};
config->sudo = strndup(optarg, PATH_MAX);
break;
case OP_NOCONFIRM:
config->noconfirm = true;
break;
case OP_USEGIT:
case 'g':
config->overrides["general.useGit"] = {BOOL, "", true};
config->useGit = true;
break;
case OP_CONFIG:
configfile = strndup(optarg, PATH_MAX);
break;
case OP_THEME:
themefile = strndup(optarg, PATH_MAX);
break;
default:
return 1;
Expand Down
23 changes: 6 additions & 17 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using std::ofstream;
using std::ifstream;

Config::Config() {}

Config::~Config() {
if (this->handle) {
alpm_trans_release(this->handle);
Expand All @@ -21,10 +19,7 @@ Config::~Config() {
}

// initialize Config, can only be ran once for each Config instance.
void Config::init(string &configFile, string &themeFile, string_view configDir) {
if (this->initialized)
return;

Config::Config(string_view configFile, string_view themeFile, string_view configDir) {
bool newUser = false;

if (!std::filesystem::exists(configDir)) {
Expand All @@ -36,24 +31,23 @@ void Config::init(string &configFile, string &themeFile, string_view configDir)
if (!std::filesystem::exists(configFile)) {
log_println(WARN, _("{} not found, generating new one"), configFile);
// https://github.com/hyprwm/Hyprland/blob/main/src/config/ConfigManager.cpp#L681
ofstream f(configFile, std::ios::trunc);
ofstream f(configFile.data(), std::ios::trunc);
f << AUTOCONFIG;
f.close();
}
if (!std::filesystem::exists(themeFile)) {
log_println(WARN, _("{} not found, generating new one"), themeFile);
ofstream f(themeFile, std::ios::trunc);
ofstream f(themeFile.data(), std::ios::trunc);
f << AUTOTHEME;
f.close();
}

this->loadConfigFile(configFile);
this->loadThemeFile(themeFile);

this->initialized = true;
if (!std::filesystem::exists(config->cacheDir)) {
log_println(WARN, _("TabAUR cache folder was not found, Creating folders at {}!"), config->cacheDir.string());
std::filesystem::create_directories(config->cacheDir);
if (!std::filesystem::exists(this->cacheDir)) {
log_println(WARN, _("TabAUR cache folder was not found, Creating folders at {}!"), this->cacheDir.string());
std::filesystem::create_directories(this->cacheDir);
}

if (newUser)
Expand All @@ -66,11 +60,6 @@ void Config::init(string &configFile, string &themeFile, string_view configDir)
"Thank you!\n"));
}

// get initialized variable
bool Config::isInitialized() {
return this->initialized;
}

/*
* initialize all the "config.toml" variables
* and sanitize them (never trust user's input)
Expand Down
Loading

0 comments on commit 9ee5eb6

Please sign in to comment.