core: updated the if statement a bit

This commit is contained in:
Kaley, Fischer 2024-07-29 14:37:28 +02:00
parent 13a5cc85d3
commit 301d3b0a2d

View file

@ -1,17 +1,15 @@
#include <ios>
#include <iostream>
#include <filesystem>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <limits> #include <cpptoml.h>
#include <string>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <algorithm> #include <filesystem>
#include <ios>
#include <iostream>
#include <limits>
#include <stdexcept> #include <stdexcept>
#include <cpptoml.h> #include <string>
void clearBuffer() { void clearBuffer() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@ -42,14 +40,9 @@ struct Action {
std::string description; std::string description;
std::string command; std::string command;
Action( Action(const std::string &nms = "", const std::string &desc = "",
const std::string& nms = "",
const std::string& desc = "",
const std::string &cmd = "") const std::string &cmd = "")
: : names(nms), description(desc), command(cmd) {}
names(nms),
description(desc),
command(cmd) {}
}; };
void from_toml(const cpptoml::table &t, Action &a) { void from_toml(const cpptoml::table &t, Action &a) {
@ -62,27 +55,45 @@ void from_toml(const cpptoml::table& t, Action& a) {
} }
} }
int main(int argc, char* argv[]) { int main(int argc, char **argv, char **envp) {
const char *userHome = getenv("HOME"); const char *userHome = getenv("HOME");
std::string rapidMenuPath = std::string(userHome) + "/.config/RapidMenu"; std::string rapidMenuPath = std::string(userHome) + "/.config/RapidMenu";
std::string rapidcommand = "mkdir -p " + rapidMenuPath; std::string rapidcommand = "mkdir -p " + rapidMenuPath;
if (std::filesystem::exists(rapidMenuPath) && std::filesystem::is_directory(rapidMenuPath)) { std::vector<std::string> ARGS{argv, argv + argc};
for (int i = 0; i < argc; ++i) {
ARGS[i] = std::string{argv[i]};
}
if (ARGS.size() < 2) {
std::cout << USAGE;
return 1;
}
if (std::filesystem::exists(rapidMenuPath) &&
std::filesystem::is_directory(rapidMenuPath)) {
} else { } else {
system(rapidcommand.c_str()); system(rapidcommand.c_str());
std::cerr << "Setting up the config directory: " << rapidMenuPath; std::cerr << "Setting up the config directory: " << rapidMenuPath;
return 1; return 1;
} }
std::vector<std::string> command;
bool notify = false, verbose = false, force = false, noShallow = false;
if (argc > 1 && strcmp(argv[1], "-c") == 0) { for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg.find('-') == 0) {
if (ARGS[i] == "--help" || ARGS[i] == "-h") {
std::cout << USAGE;
return 1;
} else if (ARGS[i] == "-c") {
if (argc < 3 || argv[2][0] == '-') { if (argc < 3 || argv[2][0] == '-') {
std::cerr << USAGE.c_str(); std::cerr << USAGE.c_str();
return 1; return 1;
} }
const char *configFile = nullptr; const char *configFile = nullptr;
configFile = argv[2]; configFile = argv[2];
@ -118,11 +129,18 @@ int main(int argc, char* argv[]) {
namesList = namesStream.str(); namesList = namesStream.str();
std::string rname = config->get_table("runner")->get_as<std::string>("rname").value_or("dashboard:"); std::string rname = config->get_table("runner")
std::string rtheme = config->get_table("runner")->get_as<std::string>("rtheme").value_or(""); ->get_as<std::string>("rname")
std::string rcommand = config->get_table("runner")->get_as<std::string>("rcommand").value_or("rofi -dmenu -p"); .value_or("dashboard:");
std::string rtheme = config->get_table("runner")
->get_as<std::string>("rtheme")
.value_or("");
std::string rcommand = config->get_table("runner")
->get_as<std::string>("rcommand")
.value_or("rofi -dmenu -p");
std::string rofiCommand = "printf '" + namesList + "' | " + rcommand + " '" + rname + " ' " + rtheme; std::string rofiCommand = "printf '" + namesList + "' | " + rcommand +
" '" + rname + " ' " + rtheme;
FILE *rofiProcess = popen(rofiCommand.c_str(), "r"); FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
char buffer[256]; char buffer[256];
@ -132,11 +150,14 @@ int main(int argc, char* argv[]) {
userChoice += buffer; userChoice += buffer;
} }
userChoice.erase(remove_if(userChoice.begin(), userChoice.end(), [](char c) { return c == '\n'; }), userChoice.end()); userChoice.erase(remove_if(userChoice.begin(), userChoice.end(),
[](char c) { return c == '\n'; }),
userChoice.end());
for (const auto &tableItem : *config) { for (const auto &tableItem : *config) {
try { try {
const auto &table = tableItem.second->as_table(); const auto &table = tableItem.second->as_table();
if (table->get_as<std::string>("names").value_or("") == userChoice) { if (table->get_as<std::string>("names").value_or("") ==
userChoice) {
Action a; Action a;
from_toml(*table, a); from_toml(*table, a);
@ -157,14 +178,13 @@ int main(int argc, char* argv[]) {
std::cerr << "Incorrect config file: "; std::cerr << "Incorrect config file: ";
return 1; return 1;
} }
break;
// executable } else if (ARGS[i] == "-b") {
} else if (argc > 1 && strcmp(argv[1], "-b") == 0) {
if (argc < 3 || argv[2][0] == '-') { if (argc < 3 || argv[2][0] == '-') {
std::cerr << USAGE.c_str(); std::cerr << USAGE.c_str();
return 1; return 1;
} }
// executable
const char *bconfig = nullptr; const char *bconfig = nullptr;
const char *bexe = nullptr; const char *bexe = nullptr;
const char *userHome = getenv("HOME"); const char *userHome = getenv("HOME");
@ -175,7 +195,8 @@ int main(int argc, char* argv[]) {
std::string bindir = std::string(userHome) + "/.local/bin"; std::string bindir = std::string(userHome) + "/.local/bin";
std::string createbindir = "mkdir -p " + bindir; std::string createbindir = "mkdir -p " + bindir;
if (std::filesystem::exists(bindir) && std::filesystem::is_directory(bindir)) { if (std::filesystem::exists(bindir) &&
std::filesystem::is_directory(bindir)) {
} else { } else {
system(createbindir.c_str()); system(createbindir.c_str());
std::cerr << "Setting up bin dir."; std::cerr << "Setting up bin dir.";
@ -184,11 +205,13 @@ int main(int argc, char* argv[]) {
// config // config
std::string bconfigfile = bconfigin; std::string bconfigfile = bconfigin;
if (std::filesystem::exists(bconfigfile) && std::filesystem::is_regular_file(bconfigfile)) { if (std::filesystem::exists(bconfigfile) &&
std::filesystem::is_regular_file(bconfigfile)) {
bconfig = argv[2]; bconfig = argv[2];
} else { } else {
while (!(std::filesystem::exists(bconfigfile) && std::filesystem::is_regular_file(bconfigfile))) { while (!(std::filesystem::exists(bconfigfile) &&
std::filesystem::is_regular_file(bconfigfile))) {
std::cout << "Invalid config file: " << bconfigfile << ' '; std::cout << "Invalid config file: " << bconfigfile << ' ';
std::cout << "Please enter a valid config: "; std::cout << "Please enter a valid config: ";
std::cin >> bconfigfile; std::cin >> bconfigfile;
@ -196,7 +219,6 @@ int main(int argc, char* argv[]) {
clearBuffer(); // clear extra input if entered clearBuffer(); // clear extra input if entered
bconfig = bconfigfile.c_str(); bconfig = bconfigfile.c_str();
} }
} }
// executable // executable
@ -206,8 +228,10 @@ int main(int argc, char* argv[]) {
clearBuffer(); clearBuffer();
std::string bexefile = std::string(userHome) + "/.local/bin/" + bexeout; std::string bexefile = std::string(userHome) + "/.local/bin/" + bexeout;
if (std::filesystem::exists(bexefile) && std::filesystem::is_regular_file(bexefile)) { if (std::filesystem::exists(bexefile) &&
while (std::filesystem::exists(bexefile) && std::filesystem::is_regular_file(bexefile)) { std::filesystem::is_regular_file(bexefile)) {
while (std::filesystem::exists(bexefile) &&
std::filesystem::is_regular_file(bexefile)) {
std::cout << "do you want to overwrite: " << bexeout << "? (y/n) "; std::cout << "do you want to overwrite: " << bexeout << "? (y/n) ";
std::cin >> byn; std::cin >> byn;
checkIfExtractionFailed(); checkIfExtractionFailed();
@ -232,12 +256,20 @@ int main(int argc, char* argv[]) {
system(("touch " + bexefile + std::string(bexe)).c_str()); system(("touch " + bexefile + std::string(bexe)).c_str());
system(("chmod +x " + bexefile + std::string(bexe)).c_str()); system(("chmod +x " + bexefile + std::string(bexe)).c_str());
system(("echo '#!/usr/bin/env bash' > " + bexefile + std::string(bexe)).c_str()); system(("echo '#!/usr/bin/env bash' > " + bexefile + std::string(bexe))
system(("echo 'RapidMenu -c " + std::string(bconfig) + "' >> " + std::string(bexe)).c_str()); .c_str());
system(("echo 'RapidMenu -c " + std::string(bconfig) + "' >> " +
std::string(bexe))
.c_str());
break;
} else { } else {
std::cerr << USAGE.c_str(); std::cerr << USAGE;
return 1; return 1;
} }
return 0; } else {
std::cerr << USAGE;
return 1;
}
}
} }