From 6775114ea657e93b461fb1beadee2002e3360102 Mon Sep 17 00:00:00 2001 From: Kaley Fischer Date: Thu, 11 Jan 2024 17:04:31 +0100 Subject: [PATCH] All of the files --- .gitignore | 1 + CMakeLists.txt | 15 ++++++ Makefile | 25 ++++++++++ assets/dashboard.conf | 24 +++++++++ src/main.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 175 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Makefile create mode 100644 assets/dashboard.conf create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ddb6965 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.19) +project(RapidMenu) + +set(CMAKE_CXX_STANDARD 23) + +# Add the path to the source files +file(GLOB_RECURSE SOURCE_FILES "src/*.cpp") + +# Add the executable target +add_executable(RapidMenu ${SOURCE_FILES}) + +# Find and link against the tomlplusplus library +find_package(tomlplusplus REQUIRED) +target_link_libraries(RapidMenu PRIVATE tomlplusplus::tomlplusplus) + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bc5ec23 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +NAME = RapidMenu +PREFIX = /usr + +all: + $(MAKE) clear + $(MAKE) release + +release: + cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DLEGACY_RENDERER:BOOL=true -S . -B ./build -G Ninja + cmake --build ./build + chmod -R 777 ./build + +clear: + rm -rf build + +install.core: + @if [ ! -f ./build/RapidMenu ]; then echo -en "You need to run $(MAKE) all first.\n" && exit 1; fi + @echo -en "!NOTE: Please note make install does not compile RapidMenu and only installs the already built files." + mkdir -p ${PREFIX}/bin + cp -f ./build/RapidMenu ${PREFIX}/bin + chmod 755 ${PREFIX}/bin/RapidMenu + +install: install.core + +uninstall: diff --git a/assets/dashboard.conf b/assets/dashboard.conf new file mode 100644 index 0000000..ebb9160 --- /dev/null +++ b/assets/dashboard.conf @@ -0,0 +1,24 @@ +[runner] +rname = "Dashboard:" +rcommand = "rofi -dmenu -p" +rtheme = "-show-icons -theme ~/.config/rofi/themes/rounded-purple-dark.rasi" + +[Disk] +names = "Disk" +description = "You chose Disk." +command = "RapidMenu -c yourdiskconf.conf" + +[Games] +names = "Games" +description = "You chose Games." +command = "RapidMenu -c yougameconf.conf" + +[PipeWire] +names = "PipeWire" +description = "You chose PipeWire" +command = "systemctl --user restart pipestart" + +[Browser] +names = "Browser" +description = "You chose floorp." +command = "floorp" diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..94d565d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace cpptoml; + +struct Action { + string names; + string description; + string command; + + Action(const string& nms = "", const string& desc = "", const string& cmd = "") + : names(nms), description(desc), command(cmd) {} +}; + +void from_toml(const table& t, Action& a) { + try { + a.names = *t.get_as("names"); + a.description = *t.get_as("description"); + a.command = *t.get_as("command"); + } catch (const parse_exception& e) { + throw std::invalid_argument("Error getting value from config:"); + } +} + +int main(int argc, char* argv[]) { + if (argc < 3 || argv[2][0] == '-') { + cerr << "Usage: -c " << endl; + return 1; + } + + const string configFile2 = argv[2]; + + try { + auto config = parse_file(configFile2); + + // Create the Rofi command + setenv("LC_CTYPE", "", 1); // Unset LC_CTYPE + 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 std::invalid_argument& e) { + cerr << "Error getting value from config: " << e.what() << endl; + return 1; + } + } + + string rname = config->get_table("runner")->get_as("rname").value_or(""); + string rtheme = config->get_table("runner")->get_as("rtheme").value_or(""); + string rcommand = config->get_table("runner")->get_as("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("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; + } + + return 0; + } + } catch (const std::invalid_argument& e) { + cerr << "Error getting value from config: " << 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; + } + + return 0; +}