update: big update added the executable option!
This commit is contained in:
parent
ac0645956f
commit
6d01349719
1 changed files with 98 additions and 75 deletions
173
src/main.cpp
173
src/main.cpp
|
@ -11,12 +11,11 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cpptoml;
|
using namespace cpptoml;
|
||||||
using namespace filesystem;
|
|
||||||
|
|
||||||
const string USAGE = R"#(usage: RapidMenu [flags] [<command> [args]]
|
const string USAGE = R"#(usage: RapidMenu [flags] [<command> [args]]
|
||||||
LISTING COMMANDS:
|
LISTING COMMANDS:
|
||||||
-c: To specify which config to use.
|
-c: To specify which config to use.
|
||||||
-b: Make a binary out of a config.
|
-b: Make a executable out of a config.
|
||||||
)#";
|
)#";
|
||||||
|
|
||||||
const string invalidvalue = R"#(Invalid value in config: )#";
|
const string invalidvalue = R"#(Invalid value in config: )#";
|
||||||
|
@ -49,7 +48,11 @@ void from_toml(const table& t, Action& a) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
const char* configFile = nullptr;
|
const char* configFile = nullptr;
|
||||||
|
const char* configFile2 = nullptr;
|
||||||
|
|
||||||
|
const char* userHome = getenv("HOME");
|
||||||
|
string rapidMenuPath = string(userHome) + "/.config/RapidMenu";
|
||||||
|
|
||||||
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] == '-') {
|
||||||
|
@ -58,89 +61,109 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
configFile = argv[2];
|
configFile = argv[2];
|
||||||
|
|
||||||
} else if (argc > 1 && strcmp(argv[1], "-d") == 0) {
|
if (configFile) {
|
||||||
cerr << USAGE.c_str() << endl;
|
|
||||||
return 0;
|
|
||||||
}else{
|
|
||||||
cerr << USAGE.c_str() << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (configFile) {
|
|
||||||
cerr << USAGE.c_str() << endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* userHome = getenv("HOME");
|
|
||||||
|
|
||||||
string rapidMenuPath = string(userHome) + "/.config/RapidMenu";
|
|
||||||
|
|
||||||
if (exists(rapidMenuPath) && is_directory(rapidMenuPath)) {
|
|
||||||
} else {
|
|
||||||
system("mkdir -p /home/$USER/.config/RapidMenu");
|
|
||||||
cerr << "Setting up config." << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
auto config = parse_file(configFile);
|
|
||||||
|
|
||||||
setenv("LC_CTYPE", "", 1);
|
|
||||||
string namesList;
|
|
||||||
|
|
||||||
for (const auto& tableItem : *config) {
|
|
||||||
try {
|
|
||||||
Action a;
|
|
||||||
from_toml(*tableItem.second->as_table(), a);
|
|
||||||
namesList += a.names + "\n";
|
|
||||||
} catch (const invalid_argument& e) {
|
|
||||||
cerr << invalidvalue.c_str() << e.what() << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string rname = config->get_table("runner")->get_as<string>("rname").value_or("dashboard:");
|
|
||||||
string rtheme = config->get_table("runner")->get_as<string>("rtheme").value_or("");
|
|
||||||
string rcommand = config->get_table("runner")->get_as<string>("rcommand").value_or("rofi -dmenu -p");
|
|
||||||
|
|
||||||
string rofiCommand = "printf '" + namesList + "' | " + rcommand + " '" + rname + " ' " + rtheme;
|
if (filesystem::exists(rapidMenuPath) && filesystem::is_directory(rapidMenuPath)) {
|
||||||
FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
|
} else {
|
||||||
|
system("mkdir -p /home/$USER/.config/RapidMenu");
|
||||||
char buffer[256];
|
cerr << "Setting up config." << endl;
|
||||||
string userChoice;
|
return 1;
|
||||||
|
|
||||||
while (fgets(buffer, sizeof(buffer), rofiProcess)) {
|
|
||||||
userChoice += buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
userChoice.erase(remove_if(userChoice.begin(), userChoice.end(), [](char c) { return c == '\n'; }), userChoice.end());
|
try {
|
||||||
|
auto config = parse_file(configFile);
|
||||||
|
|
||||||
for (const auto& tableItem : *config) {
|
setenv("LC_CTYPE", "", 1);
|
||||||
try {
|
string namesList;
|
||||||
const auto& table = tableItem.second->as_table();
|
|
||||||
if (table->get_as<string>("names").value_or("") == userChoice) {
|
|
||||||
|
|
||||||
|
for (const auto& tableItem : *config) {
|
||||||
|
try {
|
||||||
Action a;
|
Action a;
|
||||||
from_toml(*table, a);
|
from_toml(*tableItem.second->as_table(), a);
|
||||||
int bashResult = system(a.command.c_str());
|
namesList += a.names + "\n";
|
||||||
cout << a.description << endl;
|
} catch (const invalid_argument& e) {
|
||||||
|
cerr << invalidvalue.c_str() << e.what() << endl;
|
||||||
if (bashResult != 0) {
|
return 1;
|
||||||
cerr << egc.c_str() << a.command << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
} catch (const invalid_argument& e) {
|
|
||||||
cerr << invalidvalue.c_str() << e.what() << endl;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
cout << "Invalid choice. Please enter a valid option." << endl;
|
|
||||||
|
|
||||||
} catch (const parse_exception& e) {
|
string rname = config->get_table("runner")->get_as<string>("rname").value_or("dashboard:");
|
||||||
cerr << "Incorrect config file: " << endl;
|
string rtheme = config->get_table("runner")->get_as<string>("rtheme").value_or("");
|
||||||
return 1;
|
string rcommand = config->get_table("runner")->get_as<string>("rcommand").value_or("rofi -dmenu -p");
|
||||||
|
|
||||||
|
string rofiCommand = "printf '" + namesList + "' | " + rcommand + " '" + rname + " ' " + rtheme;
|
||||||
|
FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
|
||||||
|
|
||||||
|
char buffer[256];
|
||||||
|
string userChoice;
|
||||||
|
|
||||||
|
while (fgets(buffer, sizeof(buffer), rofiProcess)) {
|
||||||
|
userChoice += buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
if (table->get_as<string>("names").value_or("") == userChoice) {
|
||||||
|
|
||||||
|
Action a;
|
||||||
|
from_toml(*table, a);
|
||||||
|
int bashResult = system(a.command.c_str());
|
||||||
|
cout << a.description << endl;
|
||||||
|
|
||||||
|
if (bashResult != 0) {
|
||||||
|
cerr << egc.c_str() << a.command << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} catch (const invalid_argument& e) {
|
||||||
|
cerr << invalidvalue.c_str() << e.what() << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << "Invalid choice. Please enter a valid option." << endl;
|
||||||
|
|
||||||
|
} catch (const parse_exception& e) {
|
||||||
|
cerr << "Incorrect config file: " << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (argc > 1 && strcmp(argv[1], "-b") == 0) {
|
||||||
|
if (argc < 3 || argv[2][0] == '-') {
|
||||||
|
cerr << USAGE.c_str() << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* bconfig = nullptr;
|
||||||
|
const char* bexe = nullptr;
|
||||||
|
|
||||||
|
string bexeout;
|
||||||
|
|
||||||
|
bconfig = argv[2];
|
||||||
|
|
||||||
|
cout << "What do you want to call your executable?: ";
|
||||||
|
cin >> bexeout;
|
||||||
|
|
||||||
|
if (bexeout.empty()) {
|
||||||
|
cerr << "Error: Executable name cannot be empty." << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* bexe = bexeout.c_str();
|
||||||
|
|
||||||
|
system(("touch /home/$USER/.local/bin/" + string(bexe)).c_str());
|
||||||
|
system(("chmod +x /home/$USER/.local/bin/" + string(bexe)).c_str());
|
||||||
|
system(("echo '#!/usr/bin/env bash' > /home/$USER/.local/bin/" + string(bexe)).c_str());
|
||||||
|
system(("echo 'RapidMenu -c " + string(bconfig) + "' >> /home/$USER/.local/bin/" + string(bexe)).c_str());
|
||||||
|
|
||||||
|
}else{
|
||||||
|
cerr << USAGE.c_str() << endl;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue