Skip to content

Commit

Permalink
core: move to hyprutils for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Jun 9, 2024
1 parent c62f001 commit c27e149
Show file tree
Hide file tree
Showing 46 changed files with 94 additions and 860 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pkg_check_modules(deps REQUIRED IMPORTED_TARGET
wayland-server wayland-client wayland-cursor wayland-protocols
cairo pango pangocairo pixman-1
libdrm libinput hwdata libseat libdisplay-info libliftoff libudev gbm
hyprlang>=0.3.2 hyprcursor>=0.1.7
hyprlang>=0.3.2 hyprcursor>=0.1.7 hyprutils>=0.1.1
)

find_package(hyprwayland-scanner 0.3.10 REQUIRED)
Expand Down
8 changes: 2 additions & 6 deletions hyprctl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <filesystem>
#include <stdarg.h>
#include <regex>
#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

#include "Strings.hpp"

Expand Down Expand Up @@ -262,12 +264,6 @@ std::deque<std::string> splitArgs(int argc, char** argv) {
return result;
}

bool isNumber(const std::string& str, bool allowfloat) {
if (str.empty())
return false;
return std::ranges::all_of(str.begin(), str.end(), [&](char c) { return isdigit(c) != 0 || c == '-' || (allowfloat && c == '.'); });
}

int main(int argc, char** argv) {
bool parseArgs = true;

Expand Down
24 changes: 4 additions & 20 deletions hyprpm/src/core/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,8 @@

#include <toml++/toml.hpp>

static std::string removeBeginEndSpacesTabs(std::string str) {
if (str.empty())
return str;

int countBefore = 0;
while (str[countBefore] == ' ' || str[countBefore] == '\t') {
countBefore++;
}

int countAfter = 0;
while ((int)str.length() - countAfter - 1 >= 0 && (str[str.length() - countAfter - 1] == ' ' || str[str.length() - 1 - countAfter] == '\t')) {
countAfter++;
}

str = str.substr(countBefore, str.length() - countBefore - countAfter);

return str;
}
#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

static std::string execAndGet(std::string cmd) {
cmd += " 2>&1";
Expand Down Expand Up @@ -374,7 +358,7 @@ eHeadersErrors CPluginManager::headersValid() {
if (PATH.ends_with("protocols") || PATH.ends_with("wlroots-hyprland"))
continue;

verHeader = removeBeginEndSpacesTabs(PATH.substr(2)) + "/hyprland/src/version.h";
verHeader = trim(PATH.substr(2)) + "/hyprland/src/version.h";
break;
}

Expand Down Expand Up @@ -447,7 +431,7 @@ bool CPluginManager::updateHeaders(bool force) {
// let us give a bit of leg-room for shallowing
// due to timezones, etc.
const std::string SHALLOW_DATE =
removeBeginEndSpacesTabs(HLVER.date).empty() ? "" : execAndGet("LC_TIME=\"en_US.UTF-8\" date --date='" + HLVER.date + " - 1 weeks' '+\%a \%b \%d \%H:\%M:\%S \%Y'");
trim(HLVER.date).empty() ? "" : execAndGet("LC_TIME=\"en_US.UTF-8\" date --date='" + HLVER.date + " - 1 weeks' '+\%a \%b \%d \%H:\%M:\%S \%Y'");

if (m_bVerbose && bShallow)
progress.printMessageAbove(std::string{Colors::BLUE} + "[v] " + Colors::RESET + "will shallow since: " + SHALLOW_DATE);
Expand Down
5 changes: 4 additions & 1 deletion src/Compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <helpers/SdDaemon.hpp> // for SdNotify
#endif
#include <ranges>
#include "helpers/VarList.hpp"
#include "helpers/varlist/VarList.hpp"
#include "protocols/FractionalScale.hpp"
#include "protocols/PointerConstraints.hpp"
#include "protocols/LayerShell.hpp"
Expand All @@ -24,6 +24,9 @@
#include "desktop/LayerSurface.hpp"
#include "xwayland/XWayland.hpp"

#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

#include <sys/types.h>
#include <sys/stat.h>

Expand Down
2 changes: 1 addition & 1 deletion src/config/ConfigDataValues.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "../defines.hpp"
#include "../helpers/VarList.hpp"
#include "../helpers/varlist/VarList.hpp"
#include <vector>

enum eConfigValueDataTypes {
Expand Down
22 changes: 12 additions & 10 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "../render/decorations/CHyprGroupBarDecoration.hpp"
#include "config/ConfigDataValues.hpp"
#include "helpers/VarList.hpp"
#include "helpers/varlist/VarList.hpp"
#include "../protocols/LayerShell.hpp"

#include <cstdint>
Expand All @@ -13,14 +13,16 @@
#include <sys/types.h>
#include <unistd.h>
#include <glob.h>
#include <xkbcommon/xkbcommon.h>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <ranges>
#include <unordered_set>
#include <xkbcommon/xkbcommon.h>
#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

extern "C" char** environ;

Expand Down Expand Up @@ -2087,8 +2089,8 @@ bool layerRuleValid(const std::string& RULE) {
}

std::optional<std::string> CConfigManager::handleWindowRule(const std::string& command, const std::string& value) {
const auto RULE = removeBeginEndSpacesTabs(value.substr(0, value.find_first_of(',')));
const auto VALUE = removeBeginEndSpacesTabs(value.substr(value.find_first_of(',') + 1));
const auto RULE = trim(value.substr(0, value.find_first_of(',')));
const auto VALUE = trim(value.substr(value.find_first_of(',') + 1));

// check rule and value
if (RULE.empty() || VALUE.empty())
Expand All @@ -2114,8 +2116,8 @@ std::optional<std::string> CConfigManager::handleWindowRule(const std::string& c
}

std::optional<std::string> CConfigManager::handleLayerRule(const std::string& command, const std::string& value) {
const auto RULE = removeBeginEndSpacesTabs(value.substr(0, value.find_first_of(',')));
const auto VALUE = removeBeginEndSpacesTabs(value.substr(value.find_first_of(',') + 1));
const auto RULE = trim(value.substr(0, value.find_first_of(',')));
const auto VALUE = trim(value.substr(value.find_first_of(',') + 1));

// check rule and value
if (RULE.empty() || VALUE.empty())
Expand All @@ -2142,7 +2144,7 @@ std::optional<std::string> CConfigManager::handleLayerRule(const std::string& co
}

std::optional<std::string> CConfigManager::handleWindowRuleV2(const std::string& command, const std::string& value) {
const auto RULE = removeBeginEndSpacesTabs(value.substr(0, value.find_first_of(',')));
const auto RULE = trim(value.substr(0, value.find_first_of(',')));
const auto VALUE = value.substr(value.find_first_of(',') + 1);

if (!windowRuleValid(RULE) && RULE != "unset") {
Expand Down Expand Up @@ -2219,7 +2221,7 @@ std::optional<std::string> CConfigManager::handleWindowRuleV2(const std::string&

result = result.substr(0, min - pos);

result = removeBeginEndSpacesTabs(result);
result = trim(result);

if (!result.empty() && result.back() == ',')
result.pop_back();
Expand Down Expand Up @@ -2341,7 +2343,7 @@ void CConfigManager::updateBlurredLS(const std::string& name, const bool forceBl

std::optional<std::string> CConfigManager::handleBlurLS(const std::string& command, const std::string& value) {
if (value.starts_with("remove,")) {
const auto TOREMOVE = removeBeginEndSpacesTabs(value.substr(7));
const auto TOREMOVE = trim(value.substr(7));
if (std::erase_if(m_dBlurLSNamespaces, [&](const auto& other) { return other == TOREMOVE; }))
updateBlurredLS(TOREMOVE, false);
return {};
Expand All @@ -2358,7 +2360,7 @@ std::optional<std::string> CConfigManager::handleWorkspaceRules(const std::strin
const auto FIRST_DELIM = value.find_first_of(',');

std::string name = "";
auto first_ident = removeBeginEndSpacesTabs(value.substr(0, FIRST_DELIM));
auto first_ident = trim(value.substr(0, FIRST_DELIM));
int id = getWorkspaceIDFromString(first_ident, name);

auto rules = value.substr(FIRST_DELIM + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/config/ConfigManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <xf86drmMode.h>
#include "../helpers/WLClasses.hpp"
#include "../helpers/Monitor.hpp"
#include "../helpers/VarList.hpp"
#include "../helpers/varlist/VarList.hpp"
#include "../desktop/Window.hpp"
#include "../desktop/LayerSurface.hpp"

Expand Down
9 changes: 6 additions & 3 deletions src/debug/HyprCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <typeindex>
#include <numeric>

#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

#include "../config/ConfigDataValues.hpp"
#include "../config/ConfigValue.hpp"
#include "../managers/CursorManager.hpp"
Expand Down Expand Up @@ -826,7 +829,7 @@ std::string bindsRequest(eHyprCtlOutputFormat format, std::string request) {

std::string versionRequest(eHyprCtlOutputFormat format, std::string request) {

auto commitMsg = removeBeginEndSpacesTabs(GIT_COMMIT_MESSAGE);
auto commitMsg = trim(GIT_COMMIT_MESSAGE);
std::replace(commitMsg.begin(), commitMsg.end(), '#', ' ');

if (format == eHyprCtlOutputFormat::FORMAT_NORMAL) {
Expand Down Expand Up @@ -1051,7 +1054,7 @@ std::string dispatchBatch(eHyprCtlOutputFormat format, std::string request) {
request = "";
}

curitem = removeBeginEndSpacesTabs(curitem);
curitem = trim(curitem);
};

nextItem();
Expand Down Expand Up @@ -1305,7 +1308,7 @@ std::string dispatchGetOption(eHyprCtlOutputFormat format, std::string request)
request = "";
}

curitem = removeBeginEndSpacesTabs(curitem);
curitem = trim(curitem);
};

nextItem();
Expand Down
2 changes: 1 addition & 1 deletion src/desktop/Popup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <memory>
#include "Subsurface.hpp"
#include "../helpers/signal/Listener.hpp"
#include "../helpers/signal/Signal.hpp"

class CXDGPopupResource;

Expand Down
5 changes: 4 additions & 1 deletion src/desktop/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "../protocols/core/Compositor.hpp"
#include "../xwayland/XWayland.hpp"

#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

PHLWINDOW CWindow::create(SP<CXWaylandSurface> surface) {
PHLWINDOW pWindow = SP<CWindow>(new CWindow(surface));

Expand Down Expand Up @@ -687,7 +690,7 @@ void CWindow::applyDynamicRule(const SWindowRule& r) {
CGradientValueData activeBorderGradient = {};
CGradientValueData inactiveBorderGradient = {};
bool active = true;
CVarList colorsAndAngles = CVarList(removeBeginEndSpacesTabs(r.szRule.substr(r.szRule.find_first_of(' ') + 1)), 0, 's', true);
CVarList colorsAndAngles = CVarList(trim(r.szRule.substr(r.szRule.find_first_of(' ') + 1)), 0, 's', true);

// Basic form has only two colors, everything else can be parsed as a gradient
if (colorsAndAngles.size() == 2 && !colorsAndAngles[1].contains("deg")) {
Expand Down
5 changes: 4 additions & 1 deletion src/desktop/Workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "../Compositor.hpp"
#include "../config/ConfigValue.hpp"

#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

PHLWORKSPACE CWorkspace::create(int id, int monitorID, std::string name, bool special, bool isEmtpy) {
PHLWORKSPACE workspace = makeShared<CWorkspace>(id, monitorID, name, special, isEmtpy);
workspace->init(workspace);
Expand Down Expand Up @@ -219,7 +222,7 @@ std::string CWorkspace::getConfigName() {
}

bool CWorkspace::matchesStaticSelector(const std::string& selector_) {
auto selector = removeBeginEndSpacesTabs(selector_);
auto selector = trim(selector_);

if (selector.empty())
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/devices/IKeyboard.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "IKeyboard.hpp"
#include "../defines.hpp"
#include "../helpers/VarList.hpp"
#include "../helpers/varlist/VarList.hpp"
#include "../managers/input/InputManager.hpp"

uint32_t IKeyboard::getCapabilities() {
Expand Down
7 changes: 5 additions & 2 deletions src/events/Windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "../protocols/core/Compositor.hpp"
#include "../xwayland/XSurface.hpp"

#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;

// ------------------------------------------------------------ //
// __ _______ _ _ _____ ______ _______ //
// \ \ / /_ _| \ | | __ \ / __ \ \ / / ____| //
Expand Down Expand Up @@ -140,7 +143,7 @@ void Events::listener_mapWindow(void* owner, void* data) {
for (auto& r : PWINDOW->m_vMatchedRules) {
if (r.szRule.starts_with("monitor")) {
try {
const auto MONITORSTR = removeBeginEndSpacesTabs(r.szRule.substr(r.szRule.find(' ')));
const auto MONITORSTR = trim(r.szRule.substr(r.szRule.find(' ')));

if (MONITORSTR == "unset") {
PWINDOW->m_iMonitorID = PMONITOR->ID;
Expand Down Expand Up @@ -235,7 +238,7 @@ void Events::listener_mapWindow(void* owner, void* data) {
continue;

// `group` is a shorthand of `group set`
if (removeBeginEndSpacesTabs(r.szRule) == "group") {
if (trim(r.szRule) == "group") {
PWINDOW->m_eGroupRules |= GROUP_SET;
continue;
}
Expand Down
Loading

0 comments on commit c27e149

Please sign in to comment.