-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
#include "../string/VarList.hpp" | ||
#include <string> | ||
#include <optional> | ||
|
||
namespace Hyprutils { | ||
namespace Path { | ||
bool checkConfigExists(std::string, std::string); | ||
std::string fullConfigPath(std::string, std::string); | ||
std::optional<std::string> getHome(); | ||
std::optional<String::CVarList> getXdgConfigDirs(); | ||
std::optional<std::string> getXdgConfigHome(); | ||
std::optional<std::string> findConfig(std::string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <hyprutils/path/Path.hpp> | ||
#include <hyprutils/string/VarList.hpp> | ||
#include <filesystem> | ||
|
||
using namespace Hyprutils; | ||
|
||
namespace Hyprutils::Path { | ||
std::string fullConfigPath(std::string path, std::string programName) { | ||
return path + "/hypr/" + programName + ".conf"; | ||
} | ||
|
||
bool checkConfigExists(std::string path, std::string programName) { | ||
return std::filesystem::exists(fullConfigPath(path, programName)); | ||
} | ||
|
||
std::optional<std::string> getHome() { | ||
static const auto homeDir = getenv("HOME"); | ||
|
||
if (!homeDir || !std::filesystem::path(homeDir).is_absolute()) | ||
return std::nullopt; | ||
|
||
return std::string(homeDir).append("/.config"); | ||
} | ||
|
||
std::optional<String::CVarList> getXdgConfigDirs() { | ||
static const auto xdgConfigDirs = getenv("XDG_CONFIG_DIRS"); | ||
|
||
if (!xdgConfigDirs) | ||
return std::nullopt; | ||
|
||
static const auto xdgConfigDirsList = String::CVarList(xdgConfigDirs, 0, ':'); | ||
|
||
return xdgConfigDirsList; | ||
} | ||
|
||
std::optional<std::string> getXdgConfigHome() { | ||
static const auto xdgConfigHome = getenv("XDG_CONFIG_HOME"); | ||
|
||
if (!xdgConfigHome || !std::filesystem::path(xdgConfigHome).is_absolute()) | ||
return std::nullopt; | ||
|
||
return xdgConfigHome; | ||
} | ||
|
||
std::optional<std::string> findConfig(std::string programName) { | ||
bool xdgConfigHomeExists = false; | ||
static const auto xdgConfigHome = getXdgConfigHome(); | ||
if (xdgConfigHome.has_value()) { | ||
if (checkConfigExists(xdgConfigHome.value(), programName)) | ||
return fullConfigPath(xdgConfigHome.value(), programName); | ||
else | ||
xdgConfigHomeExists = true; | ||
} | ||
|
||
bool homeExists = false; | ||
static const auto home = getHome(); | ||
if (home.has_value()) { | ||
if (checkConfigExists(home.value(), programName)) | ||
return fullConfigPath(home.value(), programName); | ||
else | ||
homeExists = true; | ||
} | ||
|
||
static const auto xdgConfigDirs = getXdgConfigDirs(); | ||
if (xdgConfigDirs.has_value()) { | ||
for (auto dir : xdgConfigDirs.value()) { | ||
if (checkConfigExists(dir, programName)) | ||
return fullConfigPath(dir, programName); | ||
} | ||
} | ||
|
||
if (checkConfigExists("/etc/xdg", programName)) | ||
return fullConfigPath("/etc/xdg", programName); | ||
|
||
if (xdgConfigHomeExists) | ||
return xdgConfigHome; | ||
else if (homeExists) | ||
return home; | ||
else | ||
return std::nullopt; | ||
} | ||
} |