Skip to content

Commit

Permalink
util: add get_data_path() for looking the data dirs instead of hardco…
Browse files Browse the repository at this point in the history
…de /usr/share

Also some cleanup prettify_wm_name()
  • Loading branch information
Toni500github committed Oct 27, 2024
1 parent b0ed9fe commit dc61c31
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 71 deletions.
3 changes: 2 additions & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ layout = [
# or "off" for disabling ascii-art or image displaying
source-path = "os"
# Path to where we'll take all the distros/OSs ascii arts
# Path to where we'll take all the distros/OSs ascii arts.
# If on NixOS, comment/remove this line.
# note: it MUST contain an "ascii" subdirectory
data-dir = "/usr/share/customfetch"
Expand Down
3 changes: 3 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ void ctrl_d_handler(const std::istream& cin);
std::string expandVar(std::string ret);
bool taur_exec(const std::vector<std::string_view> cmd_str, const bool noerror_print = true);
std::string which(const std::string_view command);
std::string get_data_path(const std::string_view file);
std::string get_data_dir();
std::string get_relative_path(const std::string_view relative_path, const std::string_view _env, const long long mode);
bool read_binary_file(std::ifstream& f, std::string& ret);
void replace_str(std::string& str, const std::string_view from, const std::string_view to);
bool read_exec(std::vector<const char*> cmd, std::string& output, bool useStdErr = false, bool noerror_print = true);
Expand Down
2 changes: 1 addition & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
this->logo_padding_top = getValue<std::uint16_t>("config.logo-padding-top", 0);
this->ascii_logo_type = getValue<std::string>("config.ascii-logo-type", "");
this->source_path = getValue<std::string>("config.source-path", "os");
this->data_dir = getValue<std::string>("config.data-dir", "/usr/share/customfetch");
this->data_dir = getValue<std::string>("config.data-dir", get_data_path("customfetch"));
this->font = getValue<std::string>("gui.font", "Liberation Mono Normal 12");
this->gui_bg_image = getValue<std::string>("gui.bg-image", "disable");
this->builtin_title_sep = getValue<std::string>("config.title-sep", "-");
Expand Down
126 changes: 62 additions & 64 deletions src/query/unix/utils/dewm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,83 +66,82 @@ std::string prettify_wm_name(const std::string_view name) noexcept
// ngl this looks beatiful thanks to clang-format :D
switch (fnv1a16::hash(str_tolower(name.data())))
{
case "2bwm"_fnv1a16: return "2bwm";
case "9wm"_fnv1a16: return "9wm";
case "awesome"_fnv1a16: return "awesome";
case "beryl"_fnv1a16: return "beryl";
case "blackbox"_fnv1a16: return "blackbox";
case "bspwm"_fnv1a16: return "bspwm";
case "budgie-wm"_fnv1a16: return "budgie-wm";
case "chromeos-wm"_fnv1a16: return "chromeos-wm";
case "cinnamon"_fnv1a16: return "Muffin";
case "compiz"_fnv1a16: return "compiz";
case "deepin-wm"_fnv1a16: return "deepin-wm";
case "dminiwm"_fnv1a16: return "dminiwm";
case "dtwm"_fnv1a16: return "dtwm";
case "dwm"_fnv1a16: return "dwm";
case "e16"_fnv1a16: return "e16";
case "echinus"_fnv1a16: return "echinus";
case "emerald"_fnv1a16: return "emerald";
case "enlightenment"_fnv1a16: return "enlightenment";
case "finder"_fnv1a16: return "finder";
case "fluxbox"_fnv1a16: return "fluxbox";
case "flwm"_fnv1a16: return "flwm";
case "flwm_topside"_fnv1a16: return "flwm_topside";
case "fvwm"_fnv1a16: return "fvwm";
case "cinnamon"_fnv1a16: return "Muffin";
case "hyprland"_fnv1a16: return "Hyprland";
case "kwin"_fnv1a16: return "Kwin";
case "marco"_fnv1a16: return "Marco";
case "muffin"_fnv1a16: return "Muffin";
case "metacity"_fnv1a16: return "Metacity";
case "openbox"_fnv1a16: return "Openbox";
case "qtile"_fnv1a16: return "Qtile";
case "xfwm4"_fnv1a16: return "Xfwm4";
case "xmonad"_fnv1a16: return "XMonad";

case "gnome-shell"_fnv1a16:
case "gnome-session-binary"_fnv1a16:
case "mutter"_fnv1a16: return "Mutter";

case "herbstluftwm"_fnv1a16: return "herbstluftwm";
case "howm"_fnv1a16: return "howm";
case "hyprland"_fnv1a16: return "Hyprland";
case "i3"_fnv1a16: return "i3";
case "i3wm"_fnv1a16: return "i3wm";
case "icewm"_fnv1a16: return "icewm";
case "kwin"_fnv1a16: return "Kwin";
case "marco"_fnv1a16: return "Marco";
case "metacity"_fnv1a16: return "Metacity";
case "monsterwm"_fnv1a16: return "monsterwm";
case "muffin"_fnv1a16: return "Muffin";
case "musca"_fnv1a16: return "musca";
case "mwm"_fnv1a16: return "mwm";
case "notion"_fnv1a16: return "notion";
case "openbox"_fnv1a16: return "Openbox";
case "pekwm"_fnv1a16: return "pekwm";
case "qtile"_fnv1a16: return "Qtile";
case "ratpoison"_fnv1a16: return "ratpoison";
case "sawfish"_fnv1a16: return "sawfish";
case "scrotwm"_fnv1a16: return "scrotwm";
case "spectrwm"_fnv1a16: return "spectrwm";
case "stumpwm"_fnv1a16: return "stumpwm";
case "subtle"_fnv1a16: return "subtle";
case "sway"_fnv1a16: return "sway";
case "swm"_fnv1a16: return "swm";
case "tinywm"_fnv1a16: return "tinywm";
case "twin"_fnv1a16: return "twin";
case "wayfire"_fnv1a16: return "wayfire";
case "weston"_fnv1a16: return "weston";
case "wmaker"_fnv1a16: return "wmaker";
case "wmfs"_fnv1a16: return "wmfs";
case "wmii"_fnv1a16: return "wmii";
case "xfwm4"_fnv1a16: return "Xfwm4";
case "xmonad"_fnv1a16: return "XMonad";
case "mutter"_fnv1a16: return "Mutter";

case "2bwm"_fnv1a16:
case "9wm"_fnv1a16:
case "awesome"_fnv1a16:
case "beryl"_fnv1a16:
case "blackbox"_fnv1a16:
case "bspwm"_fnv1a16:
case "budgie-wm"_fnv1a16:
case "chromeos-wm"_fnv1a16:
case "compiz"_fnv1a16:
case "deepin-wm"_fnv1a16:
case "dminiwm"_fnv1a16:
case "dtwm"_fnv1a16:
case "dwm"_fnv1a16:
case "e16"_fnv1a16:
case "echinus"_fnv1a16:
case "emerald"_fnv1a16:
case "enlightenment"_fnv1a16:
case "finder"_fnv1a16:
case "fluxbox"_fnv1a16:
case "flwm"_fnv1a16:
case "flwm_topside"_fnv1a16:
case "fvwm"_fnv1a16:
case "herbstluftwm"_fnv1a16:
case "howm"_fnv1a16:
case "i3"_fnv1a16:
case "i3wm"_fnv1a16:
case "icewm"_fnv1a16:
case "monsterwm"_fnv1a16:
case "musca"_fnv1a16:
case "mwm"_fnv1a16:
case "notion"_fnv1a16:
case "pekwm"_fnv1a16:
case "ratpoison"_fnv1a16:
case "sawfish"_fnv1a16:
case "scrotwm"_fnv1a16:
case "spectrwm"_fnv1a16:
case "stumpwm"_fnv1a16:
case "subtle"_fnv1a16:
case "sway"_fnv1a16:
case "swm"_fnv1a16:
case "tinywm"_fnv1a16:
case "twin"_fnv1a16:
case "wayfire"_fnv1a16:
case "weston"_fnv1a16:
case "wmaker"_fnv1a16:
case "wmfs"_fnv1a16:
case "wmii"_fnv1a16: return name.data();
}

return MAGIC_LINE;
}

std::string get_mate_version()
{
constexpr std::string_view path = "/usr/share/mate-about/mate-version.xml";
std::ifstream f(path.data(), std::ios::in);
const std::string& path = get_data_path("mate-about/mate-version.xml");
std::ifstream f(path, std::ios::in);
if (!f.is_open())
{
std::string ret;
read_exec({ "mate-session", "--version" }, ret);

// erase doesn't remove the nth character, only the ones before it, so we have to add 1.
ret.erase(0, ret.rfind(' ') + 1);
return ret;
}
Expand Down Expand Up @@ -175,7 +174,6 @@ std::string get_kwin_version()
else
read_exec({ "kwin_x11", "--version" }, ret);

// erase doesn't remove the nth character, only the ones before it, so we have to add 1.
ret.erase(0, ret.rfind(' ') + 1);
return ret;
}
Expand Down Expand Up @@ -210,7 +208,7 @@ std::string get_cinnamon_version()
return env;

std::string line;
std::ifstream f("/usr/share/applications/cinnamon.desktop", std::ios::in);
std::ifstream f(get_data_path("applications/cinnamon.desktop"), std::ios::in);
if (!f.is_open())
{
std::string ret = get_cinnamon_version_binary();
Expand Down
28 changes: 23 additions & 5 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,12 @@ bool read_binary_file(std::ifstream& f, std::string& ret)
return false;
}

std::string which(const std::string_view command)
std::string get_relative_path(const std::string_view relative_path, const std::string_view _env, const long long mode)
{
const std::string_view env = std::getenv("PATH");
const char *env = std::getenv(_env.data());
if (!env)
return UNKNOWN;

struct stat sb;
std::string fullPath;
fullPath.reserve(1024);
Expand All @@ -289,16 +292,31 @@ std::string which(const std::string_view command)
// -300ns for not creating a string. stonks
fullPath += dir;
fullPath += '/';
fullPath += command.data();
if ((stat(fullPath.data(), &sb) == 0) && sb.st_mode & S_IXUSR)
return fullPath.data();
fullPath += relative_path.data();
if ((stat(fullPath.c_str(), &sb) == 0) && sb.st_mode & mode)
return fullPath.c_str();

fullPath.clear();
}

return UNKNOWN; // not found
}

std::string which(const std::string_view command)
{
return get_relative_path(command, "PATH", S_IXUSR);
}

std::string get_data_path(const std::string_view file)
{
return get_relative_path(file, "XDG_DATA_DIRS", S_IFREG);
}

std::string get_data_dir()
{
return get_relative_path("", "XDG_DATA_DIRS", S_IFDIR);
}

// https://gist.github.com/GenesisFR/cceaf433d5b42dcdddecdddee0657292
void replace_str(std::string& str, const std::string_view from, const std::string_view to)
{
Expand Down

0 comments on commit dc61c31

Please sign in to comment.