Skip to content

Commit

Permalink
boohoo simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fufexan committed Jul 9, 2024
1 parent 03d26c5 commit 4a9b05b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/hyprutils/path/Path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

namespace Hyprutils {
namespace Path {
bool checkConfigExists(std::string path, std::string programName);
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();
Expand Down
20 changes: 12 additions & 8 deletions src/path/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
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(path + "/hypr/" + programName + ".conf");
return std::filesystem::exists(fullConfigPath(path, programName));
}

std::optional<std::string> getHome() {
Expand All @@ -15,7 +19,7 @@ namespace Hyprutils::Path {
if (!homeDir || !std::filesystem::path(homeDir).is_absolute())
return std::nullopt;

return (std::optional<std::string>)std::string(homeDir).append("/.config");
return std::string(homeDir).append("/.config");
}

std::optional<String::CVarList> getXdgConfigDirs() {
Expand All @@ -24,7 +28,7 @@ namespace Hyprutils::Path {
if (!xdgConfigDirs)
return std::nullopt;

static const std::optional<String::CVarList> xdgConfigDirsList = String::CVarList(xdgConfigDirs, 0, ':');
static const auto xdgConfigDirsList = String::CVarList(xdgConfigDirs, 0, ':');

return xdgConfigDirsList;
}
Expand All @@ -35,15 +39,15 @@ namespace Hyprutils::Path {
if (!xdgConfigHome || !std::filesystem::path(xdgConfigHome).is_absolute())
return std::nullopt;

return (std::optional<std::string>)xdgConfigHome;
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 std::optional<std::string>(xdgConfigHome.value() + "/hypr/" + programName + ".conf");
return fullConfigPath(xdgConfigHome.value(), programName);
else
xdgConfigHomeExists = true;
}
Expand All @@ -52,7 +56,7 @@ namespace Hyprutils::Path {
static const auto home = getHome();
if (home.has_value()) {
if (checkConfigExists(home.value(), programName))
return std::optional<std::string>(home.value() + "/hypr/" + programName + ".conf");
return fullConfigPath(home.value(), programName);
else
homeExists = true;
}
Expand All @@ -61,12 +65,12 @@ namespace Hyprutils::Path {
if (xdgConfigDirs.has_value()) {
for (auto dir : xdgConfigDirs.value()) {
if (checkConfigExists(dir, programName))
return (std::optional<std::string>)dir;
return fullConfigPath(dir, programName);
}
}

if (checkConfigExists("/etc/xdg", programName))
return std::optional<std::string>("/etc/xdg/hypr/" + programName + ".conf");
return fullConfigPath("/etc/xdg/", programName);

if (xdgConfigHomeExists)
return xdgConfigHome;
Expand Down

0 comments on commit 4a9b05b

Please sign in to comment.