formatting: implemented some helpful tips from oldfashionedcow ty!

This commit is contained in:
Kaley, Fischer 2024-02-11 04:45:41 +01:00
parent 595be25284
commit 9da341ebb0

View file

@ -1,13 +1,14 @@
#include <iostream>
#include <filesystem>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cpptoml.h> #include <string>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem> #include <algorithm>
#include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <cpptoml.h>
const std::string USAGE = R"#(usage: RapidMenu [flags] [<command> [args]] const std::string USAGE = R"#(usage: RapidMenu [flags] [<command> [args]]
LISTING COMMANDS: LISTING COMMANDS:
@ -23,9 +24,14 @@ struct Action {
std::string description; std::string description;
std::string command; std::string command;
Action(const std::string &nms = "", const std::string &desc = "", Action(
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) {
@ -39,21 +45,20 @@ void from_toml(const cpptoml::table &t, Action &a) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);
const std::string userHome = std::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) && if (std::filesystem::exists(rapidMenuPath) && std::filesystem::is_directory(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;
} }
if (argc > 1 && strcmp(argv[1], "-c") == 0) { if (argc > 1 && strcmp(argv[1], "-c") == 0) {
if (argc < 3 || argv[2][0] == '-') { if (argc < 3 || argv[2][0] == '-') {
std::cerr << USAGE.c_str(); std::cerr << USAGE.c_str();
@ -95,19 +100,12 @@ int main(int argc, char *argv[]) {
namesList = namesStream.str(); namesList = namesStream.str();
std::string rname = std::string rname = config->get_table("runner")->get_as<std::string>("rname").value_or("dashboard:");
config->get_table("runner")->get_as<std::string>("rname").value_or( std::string rtheme = config->get_table("runner")->get_as<std::string>("rtheme").value_or("");
"dashboard:"); std::string rcommand = config->get_table("runner")->get_as<std::string>("rcommand").value_or("rofi -dmenu -p");
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 + std::string rofiCommand = "printf '" + namesList + "' | " + rcommand + " '" + rname + " ' " + rtheme;
" '" + rname + " ' " + rtheme; FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
std::FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
char buffer[256]; char buffer[256];
std::string userChoice; std::string userChoice;
@ -116,9 +114,7 @@ int main(int argc, char *argv[]) {
userChoice += buffer; userChoice += buffer;
} }
userChoice.erase(remove_if(userChoice.begin(), userChoice.end(), userChoice.erase(remove_if(userChoice.begin(), userChoice.end(), [](char c) { return c == '\n'; }), 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();
@ -153,7 +149,7 @@ int main(int argc, char *argv[]) {
const char* bconfig = nullptr; const char* bconfig = nullptr;
const char* bexe = nullptr; const char* bexe = nullptr;
const std::string userHome = std::getenv("HOME"); const char* userHome = getenv("HOME");
std::string bexeout; std::string bexeout;
std::string byn; std::string byn;
@ -161,8 +157,7 @@ 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) && if (std::filesystem::exists(bindir) && std::filesystem::is_directory(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.";
@ -171,19 +166,18 @@ int main(int argc, char *argv[]) {
// config // config
std::string bconfigfile = bconfigin; std::string bconfigfile = bconfigin;
if (std::filesystem::exists(bconfigfile) && if (std::filesystem::exists(bconfigfile) && std::filesystem::is_regular_file(bconfigfile)) {
std::filesystem::is_regular_file(bconfigfile)) {
bconfig = argv[2]; bconfig = argv[2];
} else { } else {
while (!(std::filesystem::exists(bconfigfile) && while (!(std::filesystem::exists(bconfigfile) && std::filesystem::is_regular_file(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;
bconfig = bconfigfile.c_str(); bconfig = bconfigfile.c_str();
} }
} }
// executable // executable
@ -191,10 +185,8 @@ int main(int argc, char *argv[]) {
std::cin >> bexeout; std::cin >> bexeout;
std::string bexefile = std::string(userHome) + "/.local/bin/" + bexeout; std::string bexefile = std::string(userHome) + "/.local/bin/" + bexeout;
if (std::filesystem::exists(bexefile) && if (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)) {
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;
@ -216,11 +208,8 @@ 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)) system(("echo '#!/usr/bin/env bash' > " + bexefile + std::string(bexe)).c_str());
.c_str()); system(("echo 'RapidMenu -c " + std::string(bconfig) + "' >> " + std::string(bexe)).c_str());
system(("echo 'RapidMenu -c " + std::string(bconfig) + "' >> " +
std::string(bexe))
.c_str());
}else{ }else{
std::cerr << USAGE.c_str(); std::cerr << USAGE.c_str();