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 <cctype>
#include <cpptoml.h>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <algorithm>
#include <stdexcept>
#include <string>
#include <cpptoml.h>
const std::string USAGE = R"#(usage: RapidMenu [flags] [<command> [args]]
LISTING COMMANDS:
@ -23,9 +24,14 @@ struct Action {
std::string description;
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 = "")
: names(nms), description(desc), command(cmd) {}
:
names(nms),
description(desc),
command(cmd) {}
};
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[]) {
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 rapidcommand = "mkdir -p " + rapidMenuPath;
if (std::filesystem::exists(rapidMenuPath) &&
std::filesystem::is_directory(rapidMenuPath)) {
if (std::filesystem::exists(rapidMenuPath) && std::filesystem::is_directory(rapidMenuPath)) {
} else {
system(rapidcommand.c_str());
std::cerr << "Setting up the config directory: " << rapidMenuPath;
return 1;
}
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
if (argc < 3 || argv[2][0] == '-') {
std::cerr << USAGE.c_str();
@ -95,19 +100,12 @@ int main(int argc, char *argv[]) {
namesList = namesStream.str();
std::string rname =
config->get_table("runner")->get_as<std::string>("rname").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 rname = config->get_table("runner")->get_as<std::string>("rname").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::FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
std::string rofiCommand = "printf '" + namesList + "' | " + rcommand + " '" + rname + " ' " + rtheme;
FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
char buffer[256];
std::string userChoice;
@ -116,9 +114,7 @@ int main(int argc, char *argv[]) {
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) {
try {
const auto& table = tableItem.second->as_table();
@ -153,7 +149,7 @@ int main(int argc, char *argv[]) {
const char* bconfig = nullptr;
const char* bexe = nullptr;
const std::string userHome = std::getenv("HOME");
const char* userHome = getenv("HOME");
std::string bexeout;
std::string byn;
@ -161,8 +157,7 @@ int main(int argc, char *argv[]) {
std::string bindir = std::string(userHome) + "/.local/bin";
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 {
system(createbindir.c_str());
std::cerr << "Setting up bin dir.";
@ -171,19 +166,18 @@ int main(int argc, char *argv[]) {
// config
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];
} 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 << "Please enter a valid config: ";
std::cin >> bconfigfile;
bconfig = bconfigfile.c_str();
}
}
// executable
@ -191,10 +185,8 @@ int main(int argc, char *argv[]) {
std::cin >> bexeout;
std::string bexefile = std::string(userHome) + "/.local/bin/" + bexeout;
if (std::filesystem::exists(bexefile) &&
std::filesystem::is_regular_file(bexefile)) {
while (std::filesystem::exists(bexefile) &&
std::filesystem::is_regular_file(bexefile)) {
if (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::cin >> byn;
@ -216,11 +208,8 @@ int main(int argc, char *argv[]) {
system(("touch " + 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 'RapidMenu -c " + std::string(bconfig) + "' >> " +
std::string(bexe))
.c_str());
system(("echo '#!/usr/bin/env bash' > " + bexefile + std::string(bexe)).c_str());
system(("echo 'RapidMenu -c " + std::string(bconfig) + "' >> " + std::string(bexe)).c_str());
}else{
std::cerr << USAGE.c_str();