From c5b8ad03d03ddbd2b0ff8615c2f6dba31374b6a8 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:49:49 +0200 Subject: [PATCH] core: move to hyprutils for utils (#362) * core: move to hyprutils for utils * Nix: add hyprutils dep --------- Co-authored-by: Mihai Fufezan --- CMakeLists.txt | 2 +- flake.lock | 60 ++++++++++++++++++------------ flake.nix | 8 ++++ nix/default.nix | 2 + nix/overlays.nix | 1 + src/helpers/VarList.cpp | 55 ---------------------------- src/helpers/VarList.hpp | 63 -------------------------------- src/renderer/widgets/IWidget.cpp | 4 +- 8 files changed, 52 insertions(+), 143 deletions(-) delete mode 100644 src/helpers/VarList.cpp delete mode 100644 src/helpers/VarList.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index cfab691..7bf6a74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ message(STATUS "Checking deps...") find_package(Threads REQUIRED) find_package(PkgConfig REQUIRED) find_package(OpenGL REQUIRED) -pkg_check_modules(deps REQUIRED IMPORTED_TARGET wayland-client wayland-protocols wayland-egl hyprlang>=0.4.0 egl opengl xkbcommon libjpeg libwebp libmagic cairo pangocairo libdrm gbm) +pkg_check_modules(deps REQUIRED IMPORTED_TARGET wayland-client wayland-protocols wayland-egl hyprlang>=0.4.0 egl opengl xkbcommon libjpeg libwebp libmagic cairo pangocairo libdrm gbm hyprutils>=0.1.1) file(GLOB_RECURSE SRCFILES CONFIGURE_DEPENDS "src/*.cpp") add_executable(hyprlock ${SRCFILES}) diff --git a/flake.lock b/flake.lock index 5a4bc9a..7a53cde 100644 --- a/flake.lock +++ b/flake.lock @@ -2,17 +2,22 @@ "nodes": { "hyprlang": { "inputs": { + "hyprutils": [ + "hyprutils" + ], "nixpkgs": [ "nixpkgs" ], - "systems": "systems" + "systems": [ + "systems" + ] }, "locked": { - "lastModified": 1713121246, - "narHash": "sha256-502X0Q0fhN6tJK7iEUA8CghONKSatW/Mqj4Wappd++0=", + "lastModified": 1717881852, + "narHash": "sha256-XeeVoKHQgfKuXoP6q90sUqKyl7EYy3ol2dVZGM+Jj94=", "owner": "hyprwm", "repo": "hyprlang", - "rev": "78fcaa27ae9e1d782faa3ff06c8ea55ddce63706", + "rev": "ec6938c66253429192274d612912649a0cfe4d28", "type": "github" }, "original": { @@ -21,13 +26,36 @@ "type": "github" } }, + "hyprutils": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "systems": [ + "systems" + ] + }, + "locked": { + "lastModified": 1717881334, + "narHash": "sha256-a0inRgJhPL6v9v7RPM/rx1kbXdfe3xJA1c9z0ZkYnh4=", + "owner": "hyprwm", + "repo": "hyprutils", + "rev": "0693f9398ab693d89c9a0aa3b3d062dd61b7a60e", + "type": "github" + }, + "original": { + "owner": "hyprwm", + "repo": "hyprutils", + "type": "github" + } + }, "nixpkgs": { "locked": { - "lastModified": 1712963716, - "narHash": "sha256-WKm9CvgCldeIVvRz87iOMi8CFVB1apJlkUT4GGvA0iM=", + "lastModified": 1717602782, + "narHash": "sha256-pL9jeus5QpX5R+9rsp3hhZ+uplVHscNJh8n8VpqscM0=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "cfd6b5fc90b15709b780a5a1619695a88505a176", + "rev": "e8057b67ebf307f01bdcc8fba94d94f75039d1f6", "type": "github" }, "original": { @@ -40,8 +68,9 @@ "root": { "inputs": { "hyprlang": "hyprlang", + "hyprutils": "hyprutils", "nixpkgs": "nixpkgs", - "systems": "systems_2" + "systems": "systems" } }, "systems": { @@ -58,21 +87,6 @@ "repo": "default-linux", "type": "github" } - }, - "systems_2": { - "locked": { - "lastModified": 1689347949, - "narHash": "sha256-12tWmuL2zgBgZkdoB6qXZsgJEH9LR3oUgpaQq2RbI80=", - "owner": "nix-systems", - "repo": "default-linux", - "rev": "31732fcf5e8fea42e59c2488ad31a0e651500f68", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default-linux", - "type": "github" - } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 40e1096..54dbbe2 100644 --- a/flake.nix +++ b/flake.nix @@ -5,9 +5,17 @@ nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; systems.url = "github:nix-systems/default-linux"; + hyprutils = { + url = "github:hyprwm/hyprutils"; + inputs.nixpkgs.follows = "nixpkgs"; + inputs.systems.follows = "systems"; + }; + hyprlang = { url = "github:hyprwm/hyprlang"; inputs.nixpkgs.follows = "nixpkgs"; + inputs.systems.follows = "systems"; + inputs.hyprutils.follows = "hyprutils"; }; }; diff --git a/nix/default.nix b/nix/default.nix index 028c039..a035b7c 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -12,6 +12,7 @@ libxkbcommon, mesa, hyprlang, + hyprutils, pam, pango, wayland, @@ -39,6 +40,7 @@ stdenv.mkDerivation { libxkbcommon mesa hyprlang + hyprutils pam pango wayland diff --git a/nix/overlays.nix b/nix/overlays.nix index 7fb96f2..414b1ae 100644 --- a/nix/overlays.nix +++ b/nix/overlays.nix @@ -12,6 +12,7 @@ in { hyprlock = lib.composeManyExtensions [ inputs.hyprlang.overlays.default + inputs.hyprutils.overlays.default (final: prev: { hyprlock = prev.callPackage ./default.nix { stdenv = prev.gcc13Stdenv; diff --git a/src/helpers/VarList.cpp b/src/helpers/VarList.cpp deleted file mode 100644 index 518acde..0000000 --- a/src/helpers/VarList.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "VarList.hpp" -#include -#include - -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; -} - -CVarList::CVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) { - if (in.empty()) - m_vArgs.emplace_back(""); - - std::string args{in}; - size_t idx = 0; - size_t pos = 0; - std::ranges::replace_if( - args, [&](const char& c) { return delim == 's' ? std::isspace(c) : c == delim; }, 0); - - for (const auto& s : args | std::views::split(0)) { - if (removeEmpty && s.empty()) - continue; - if (++idx == lastArgNo) { - m_vArgs.emplace_back(removeBeginEndSpacesTabs(in.substr(pos))); - break; - } - pos += s.size() + 1; - m_vArgs.emplace_back(removeBeginEndSpacesTabs(std::string_view{s}.data())); - } -} - -std::string CVarList::join(const std::string& joiner, size_t from, size_t to) const { - size_t last = to == 0 ? size() : to; - - std::string rolling; - for (size_t i = from; i < last; ++i) { - rolling += m_vArgs[i] + (i + 1 < last ? joiner : ""); - } - - return rolling; -} \ No newline at end of file diff --git a/src/helpers/VarList.hpp b/src/helpers/VarList.hpp deleted file mode 100644 index 1374da6..0000000 --- a/src/helpers/VarList.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once -#include -#include -#include - -class CVarList { - public: - /** Split string into arg list - @param lastArgNo stop splitting after argv reaches maximum size, last arg will contain rest of unsplit args - @param delim if delimiter is 's', use std::isspace - @param removeEmpty remove empty args from argv - */ - CVarList(const std::string& in, const size_t maxSize = 0, const char delim = ',', const bool removeEmpty = false); - - ~CVarList() = default; - - size_t size() const { - return m_vArgs.size(); - } - - std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const; - - void map(std::function func) { - for (auto& s : m_vArgs) - func(s); - } - - void append(const std::string arg) { - m_vArgs.emplace_back(arg); - } - - std::string operator[](const size_t& idx) const { - if (idx >= m_vArgs.size()) - return ""; - return m_vArgs[idx]; - } - - // for range-based loops - std::vector::iterator begin() { - return m_vArgs.begin(); - } - std::vector::const_iterator begin() const { - return m_vArgs.begin(); - } - std::vector::iterator end() { - return m_vArgs.end(); - } - std::vector::const_iterator end() const { - return m_vArgs.end(); - } - - bool contains(const std::string& el) { - for (auto& a : m_vArgs) { - if (a == el) - return true; - } - - return false; - } - - private: - std::vector m_vArgs; -}; \ No newline at end of file diff --git a/src/renderer/widgets/IWidget.cpp b/src/renderer/widgets/IWidget.cpp index b1879fc..08aa1c2 100644 --- a/src/renderer/widgets/IWidget.cpp +++ b/src/renderer/widgets/IWidget.cpp @@ -1,10 +1,12 @@ #include "IWidget.hpp" #include "../../helpers/Log.hpp" -#include "../../helpers/VarList.hpp" #include "../../core/hyprlock.hpp" #include #include #include +#include + +using namespace Hyprutils::String; #if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 190100 #pragma comment(lib, "date-tz")