inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
arg-handler.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include "./cmd-line-args.hpp"
12#include "dirsort/songmap.hpp"
13#include <filesystem>
14#include <iostream>
15#include <string_view>
16#include <unordered_map>
17
18// Constants
19constexpr const char* DBUS_SERVICE_NAME =
20 "org.mpris.MediaPlayer2.inLimbo";
21constexpr const char* VERSION_FILE_NAME = "VERSION";
22constexpr const char* REPOSITORY_URL = "https://github.com/nots1dd/inLimbo";
23
25 false;
26bool parseSongTree = false;
27
29{
30 bool printSongTree = false;
31 bool printArtistsAll = false;
32 bool printSongsByArtist = false;
33 bool printSongsGenreAll = false;
34 bool printSongInfo = false;
35 std::string artist;
36 std::string song;
37};
38
53
62{
63public:
70 static auto getColor(ConsoleColor color) -> std::string_view
71 {
72 static const std::unordered_map<ConsoleColor, std::string_view> colorCodes = {
73 {ConsoleColor::Reset, "\033[0m"}, {ConsoleColor::Green, "\033[32m"},
74 {ConsoleColor::Blue, "\033[34m"}, {ConsoleColor::Yellow, "\033[33m"},
75 {ConsoleColor::Red, "\033[31m"}, {ConsoleColor::Cyan, "\033[36m"},
76 {ConsoleColor::Magenta, "\033[35m"}};
77 return colorCodes.at(color);
78 }
79};
80
90{
91public:
99 struct Paths
100 {
101 std::string configPath;
102 std::string libBinPath;
103 std::string cacheDir;
104 };
105
107
119 static void handleArguments(const CommandLineArgs& cmdArgs, const std::string& programName,
120 const Paths& paths)
121 {
122 const std::unordered_map<std::string, std::function<void()>> argumentHandlers = {
123 {"--help", [&]() { handleHelp(cmdArgs, programName); }},
124 {"--version", [&]() { handleVersion(); }},
125 {"--clear-cache", [&]() { handleClearCache(paths.libBinPath); }},
126 {"--show-config-file", [&]() { handleShowConfig(paths.configPath); }},
127 {"--show-log-dir", [&]() { handleShowLogDir(paths.cacheDir); }},
128 {"--show-dbus-name", [&]() { handleShowDBusName(); }},
129 {"--update-cache-run", [&]() { handleUpdateCacheRun(paths.libBinPath); }},
130 {"--print-song-tree", [&]() { handlePrintSongTree(); }},
131 {"--print-artists-all", [&]() { handlePrintArtistsAll(); }},
132 {"--print-songs-by-genre-all", [&]() { handlePrintSongsByGenreAll(); }},
133 {"--print-songs-by-artist",
134 [&]() { handlePrintSongsByArtist(cmdArgs.get("--print-songs-by-artist")); }},
135 {"--print-song-info", [&]() { handleSongInfo(cmdArgs.get("--print-song-info")); }}};
136
137 for (const auto& [flag, handler] : argumentHandlers)
138 {
139 if (cmdArgs.hasFlag(flag))
140 {
141 handler();
142 if (!shouldRunApp)
143 exit(0);
144 }
145 }
146 }
147
148 static auto processSongTreeArguments(SongTree& song_tree) -> void
149 {
151 {
152 song_tree.display();
153 }
154
155 if (ArgumentHandler::song_tree_parse_state.printArtistsAll)
156 {
157 song_tree.printAllArtists();
158 }
159
160 if (ArgumentHandler::song_tree_parse_state.printSongsByArtist)
161 {
162 song_tree.getSongsByArtist(ArgumentHandler::song_tree_parse_state.artist);
163 }
164
165 if (ArgumentHandler::song_tree_parse_state.printSongsGenreAll)
166 {
167 song_tree.getSongsByGenreAndPrint();
168 }
170 {
171 song_tree.printSongInfo(ArgumentHandler::song_tree_parse_state.song);
172 }
173 }
174
175private:
183 static void colorPrint(ConsoleColor color, std::string_view label, std::string_view value = "")
184 {
185 std::cout << ColorManager::getColor(color) << label
186 << ColorManager::getColor(ConsoleColor::Reset) << value << "\n";
187 }
188
195 static void handleHelp(const CommandLineArgs& cmdArgs, const std::string& programName)
196 {
197 cmdArgs.printUsage(programName);
198 }
199
200 static auto readVersionFromFile() -> std::string
201 {
202 std::ifstream versionFile(VERSION_FILE_NAME); // Open the VERSION file
203 if (!versionFile)
204 {
205 std::cerr << "Error: Unable to open VERSION file!" << std::endl;
206 return "Unknown Version"; // Return default version if file can't be opened
207 }
208
209 std::stringstream buffer;
210 buffer << versionFile.rdbuf(); // Read the file content into a string stream
211 return buffer.str(); // Return the string content
212 }
213
217 static void handleVersion()
218 {
219 std::string version = readVersionFromFile();
220 colorPrint(ConsoleColor::Cyan, "inLimbo version: ", version);
221 }
222
228 static void handleShowConfig(const std::string& configPath)
229 {
230 colorPrint(ConsoleColor::Green, "Config file location: ", configPath);
231 }
232
238 static void handleShowLogDir(const std::string& cacheDir)
239 {
240 colorPrint(ConsoleColor::Blue, "Log directory: ", cacheDir);
241 }
242
246 static void handleShowDBusName()
247 {
248 colorPrint(ConsoleColor::Magenta, "DBus name: ", DBUS_SERVICE_NAME);
249 }
250
259 static void handleClearCache(const std::string& libBinPath)
260 {
261 try
262 {
263 if (std::filesystem::exists(libBinPath))
264 {
265 std::filesystem::remove(libBinPath);
266 colorPrint(ConsoleColor::Green, "Cache cleared successfully: ", libBinPath);
267 }
268 else
269 {
270 colorPrint(ConsoleColor::Yellow, "No cache file found to clear: ", libBinPath);
271 }
272 }
273 catch (const std::filesystem::filesystem_error& e)
274 {
275 colorPrint(ConsoleColor::Red, "Error clearing cache: ", e.what());
276 }
277 }
278
287 static void handleUpdateCacheRun(const std::string& libBinPath)
288 {
290 << "-- Updating Cache and running app..."
292 handleClearCache(libBinPath);
293 shouldRunApp = true;
294 }
295
296 static void parseSongMap()
297 {
298 parseSongTree = true;
299 shouldRunApp = true;
300 }
301
302 static void handlePrintSongTree()
303 {
304 song_tree_parse_state.printSongTree = true;
305 parseSongMap();
306 }
307
308 static void handlePrintArtistsAll()
309 {
310 parseSongMap();
311 song_tree_parse_state.printArtistsAll = true;
312 }
313
314 static void handlePrintSongsByArtist(std::string artistName)
315 {
316 parseSongMap();
317 song_tree_parse_state.printSongsByArtist = true;
318 song_tree_parse_state.artist = artistName;
319 }
320 static void handlePrintSongsByGenreAll()
321 {
322 parseSongMap();
323 song_tree_parse_state.printSongsGenreAll = true;
324 }
325 static void handleSongInfo(std::string songName)
326 {
327 parseSongMap();
328 song_tree_parse_state.printSongInfo = true;
329 song_tree_parse_state.song = songName;
330 }
331};
constexpr const char * REPOSITORY_URL
Definition arg-handler.hpp:22
bool parseSongTree
Definition arg-handler.hpp:26
constexpr const char * DBUS_SERVICE_NAME
DBus service name used by inLimbo.
Definition arg-handler.hpp:19
ConsoleColor
Represents ANSI console colors for colored output.
Definition arg-handler.hpp:44
@ Cyan
Cyan color code.
Definition arg-handler.hpp:50
@ Yellow
Yellow color code.
Definition arg-handler.hpp:48
@ Reset
Resets the console color to default.
Definition arg-handler.hpp:45
@ Blue
Blue color code.
Definition arg-handler.hpp:47
@ Magenta
Magenta color code.
Definition arg-handler.hpp:51
@ Green
Green color code.
Definition arg-handler.hpp:46
@ Red
Red color code.
Definition arg-handler.hpp:49
constexpr const char * VERSION_FILE_NAME
Definition arg-handler.hpp:21
bool shouldRunApp
Indicates if the application should proceed to run after handling arguments.
Definition arg-handler.hpp:24
Handles processing of command-line arguments and executes corresponding actions.
Definition arg-handler.hpp:90
static void handleArguments(const CommandLineArgs &cmdArgs, const std::string &programName, const Paths &paths)
Handles command-line arguments and executes corresponding actions.
Definition arg-handler.hpp:119
static auto processSongTreeArguments(SongTree &song_tree) -> void
Definition arg-handler.hpp:148
static SongTreeState song_tree_parse_state
Definition arg-handler.hpp:106
Provides utilities for managing console colors.
Definition arg-handler.hpp:62
static auto getColor(ConsoleColor color) -> std::string_view
Retrieves the ANSI color code for a given ConsoleColor.
Definition arg-handler.hpp:70
Parses and validates command-line arguments.
Definition cmd-line-args.hpp:26
void printUsage(const std::string &programName) const
Prints usage information and program details.
Definition cmd-line-args.hpp:103
auto hasFlag(const std::string &flag) const -> bool
Checks if a specific flag was provided.
Definition cmd-line-args.hpp:80
auto get(const std::string &flag, const std::string &defaultValue="") const -> std::string
Retrieves the value associated with a flag.
Definition cmd-line-args.hpp:67
Represents a hierarchical tree structure to store songs by artist, album, disc number,...
Definition songmap.hpp:90
Represents essential application paths.
Definition arg-handler.hpp:100
std::string libBinPath
Path to the binary library.
Definition arg-handler.hpp:102
std::string configPath
Path to the configuration file.
Definition arg-handler.hpp:101
std::string cacheDir
Path to the cache directory.
Definition arg-handler.hpp:103
Definition arg-handler.hpp:29
bool printSongTree
Definition arg-handler.hpp:30
std::string artist
Definition arg-handler.hpp:35
bool printArtistsAll
Definition arg-handler.hpp:31
bool printSongInfo
Definition arg-handler.hpp:34
bool printSongsByArtist
Definition arg-handler.hpp:32
std::string song
Definition arg-handler.hpp:36
bool printSongsGenreAll
Definition arg-handler.hpp:33