fix: error handling
This commit is contained in:
parent
0fb9bd6e9e
commit
b550260cc9
1 changed files with 41 additions and 24 deletions
65
src/main.cpp
65
src/main.cpp
|
@ -12,13 +12,29 @@
|
|||
using namespace std;
|
||||
using namespace cpptoml;
|
||||
|
||||
const string USAGE = R"#(usage: RapidMenu [flags] [<command> [args]]
|
||||
LISTING COMMANDS:
|
||||
-c: To specify which config to use.
|
||||
-b: Make a binary out of a config.
|
||||
)#";
|
||||
|
||||
const string invalidvalue = R"#(Invalid value in config: )#";
|
||||
const string egc = R"#(Invalid command in config: )#";
|
||||
|
||||
|
||||
struct Action {
|
||||
string names;
|
||||
string description;
|
||||
string command;
|
||||
|
||||
Action(const string& nms = "", const string& desc = "", const string& cmd = "")
|
||||
: names(nms), description(desc), command(cmd) {}
|
||||
Action(
|
||||
const string& nms = "",
|
||||
const string& desc = "",
|
||||
const string& cmd = "")
|
||||
|
||||
: names(nms),
|
||||
description(desc),
|
||||
command(cmd) {}
|
||||
};
|
||||
|
||||
void from_toml(const table& t, Action& a) {
|
||||
|
@ -27,26 +43,33 @@ void from_toml(const table& t, Action& a) {
|
|||
a.description = *t.get_as<string>("description");
|
||||
a.command = *t.get_as<string>("command");
|
||||
} catch (const parse_exception& e) {
|
||||
throw invalid_argument("Error getting value from config:");
|
||||
throw invalid_argument(invalidvalue.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
const char* configFile = nullptr; // Initialize to nullptr
|
||||
|
||||
if (argc < 3 || argv[2][0] == '-') {
|
||||
cerr << "Usage: -c <config_file>" << endl;
|
||||
return 1;
|
||||
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
|
||||
if (argc < 3 || argv[2][0] == '-') {
|
||||
std::cerr << USAGE.c_str() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
configFile = argv[2];
|
||||
} else if (argc > 1 && strcmp(argv[1], "-d") == 0) {
|
||||
std::cerr << USAGE.c_str() << std::endl;
|
||||
return 0; // Assuming you want to exit after printing the message
|
||||
}else{
|
||||
std::cerr << USAGE.c_str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const string configFile = argv[2];
|
||||
if (configFile) {
|
||||
}
|
||||
|
||||
const char* userHome = getenv("HOME");
|
||||
|
||||
if (userHome == nullptr) {
|
||||
cerr << "Error: HOME environment variable not set." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
string rapidMenuPath = string(userHome) + "/.config/RapidMenu";
|
||||
|
||||
if (filesystem::exists(rapidMenuPath) && filesystem::is_directory(rapidMenuPath)) {
|
||||
|
@ -59,18 +82,16 @@ int main(int argc, char* argv[]) {
|
|||
try {
|
||||
auto config = parse_file(configFile);
|
||||
|
||||
// Create the Rofi command
|
||||
setenv("LC_CTYPE", "", 1); // Unset LC_CTYPE
|
||||
setenv("LC_CTYPE", "", 1);
|
||||
string namesList;
|
||||
|
||||
for (const auto& tableItem : *config) {
|
||||
try {
|
||||
// Use the `from_toml` function to convert TOML to Action
|
||||
Action a;
|
||||
from_toml(*tableItem.second->as_table(), a);
|
||||
namesList += a.names + "\n";
|
||||
} catch (const invalid_argument& e) {
|
||||
cerr << "Error getting value from config: " << e.what() << endl;
|
||||
cerr << invalidvalue.c_str() << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -78,41 +99,37 @@ int main(int argc, char* argv[]) {
|
|||
string rname = config->get_table("runner")->get_as<string>("rname").value_or("");
|
||||
string rtheme = config->get_table("runner")->get_as<string>("rtheme").value_or("");
|
||||
string rcommand = config->get_table("runner")->get_as<string>("rcommand").value_or("");
|
||||
//string rofiCommand = "printf '" + namesList + "' | rofi -dmenu -p 'Dashboard: ' -show-icons -theme ~/.config/rofi/themes/rounded-purple-dark.rasi";
|
||||
|
||||
string rofiCommand = "printf '" + namesList + "' | " + rcommand + " '" + rname + " ' " + rtheme;
|
||||
// Open a pipe to the Rofi process
|
||||
FILE *rofiProcess = popen(rofiCommand.c_str(), "r");
|
||||
|
||||
char buffer[256];
|
||||
string userChoice;
|
||||
|
||||
// Read user input from Rofi using getline
|
||||
while (fgets(buffer, sizeof(buffer), rofiProcess)) {
|
||||
userChoice += buffer;
|
||||
}
|
||||
|
||||
// Remove newline characters from userChoice
|
||||
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) {
|
||||
// Use the `from_toml` function to convert TOML to Action
|
||||
|
||||
Action a;
|
||||
from_toml(*table, a);
|
||||
int bashResult = system(a.command.c_str());
|
||||
cout << a.description << endl;
|
||||
|
||||
// Check the return value if needed
|
||||
if (bashResult != 0) {
|
||||
cerr << "Error executing command: " << a.command << endl;
|
||||
cerr << egc.c_str() << a.command << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
} catch (const invalid_argument& e) {
|
||||
cerr << "Error getting value from config: " << e.what() << endl;
|
||||
cerr << invalidvalue.c_str() << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue