Compare commits

..
2 Commits
3 changed files with 112 additions and 23 deletions
+14 -3
View File
@@ -405,16 +405,27 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
size_t lineLength = line.size();
size_t commentStart = line.find(commentCharacter, indentation);
size_t commentEnd = line.find(commentTerminationCharacter, commentStart);
bool hasComment = commentStart != std::string::npos && commentEnd != std::string::npos;
size_t commentEnd = line.rfind(commentTerminationCharacter);
bool hasComment = commentStart != std::string::npos && (commentTerminationCharacter.empty() || commentEnd != std::string::npos);
// Lines that have a comment at the *end* of the line aren't considered commented lines
if (hasComment && line.find_first_not_of(" \t") < commentStart) {
hasComment = false;
}
// Uncomment line
if (hasComment && !addComment) {
size_t contentStart = line.find_first_not_of(" \t", commentStart + commentCharacter.size());
size_t contentLength = commentEnd - contentStart;
// Trim trailing whitespace
std::string content = line.substr(contentStart, contentLength);
content = content.substr(0, content.find_last_not_of(" \t") + 1);
line = line.substr(0, indentation)
+ line.substr(contentStart, contentLength);
+ content;
}
// Comment line
else if (!hasComment && addComment) {
size_t contentStart = line.find_first_not_of(" \t");
size_t contentLength = line.size() - contentStart;
+14 -8
View File
@@ -72,18 +72,24 @@ void Machine::fetchSession()
int8_t likelyWayland = 0;
// Detect via environment variable
std::string env;
env = std::string(std::getenv("XDG_SESSION_TYPE"));
if (env == "wayland") {
const char* env;
env = std::getenv("XDG_SESSION_TYPE");
if (env != nullptr) {
auto session = std::string(env);
if (session == "wayland") {
likelyWayland++;
}
else if (env == "x11") {
else if (session == "x11") {
likelyWayland--;
}
env = std::string(std::getenv("WAYLAND_DISPLAY"));
if (env.find("wayland-", 0) == 0) {
}
env = std::getenv("WAYLAND_DISPLAY");
if (env != nullptr) {
auto display = std::string(env);
if (display.find("wayland-", 0) == 0) {
likelyWayland++;
}
}
// Detect via Wayland socket
auto socket = std::filesystem::path("/run/user") / std::to_string(uid());
@@ -117,12 +123,12 @@ void Machine::fetchSession()
std::string command;
std::getline(stream, command);
if (command == "Xwayland" || command == "hyprland" || command == "sway") {
if (command == "Xwayland" || command == "sway" || command == "hyprland") {
likelyWayland++;
break;
}
if (command == "Xorg" || command == "xinit" || command == "bspwm") {
if (command == "Xorg" || command == "xinit" || command == "i3" || command == "bspwm") {
likelyWayland--;
break;
}
+80 -8
View File
@@ -1,11 +1,10 @@
/*
* Copyright (C) 2022 Riyyi
* Copyright (C) 2022,2025 Riyyi
*
* SPDX-License-Identifier: MIT
*/
#include <cstddef> // size_t
#include <cstdint> // uint32_t
#include <cstdio> // stderr
#include <filesystem> // path
#include <string>
@@ -13,12 +12,12 @@
#include <unordered_map>
#include <vector>
#include "ruc/file.h"
#include "config.h"
#include "dotfile.h"
#include "machine.h"
#include "macro.h"
#include "ruc/file.h"
#include "ruc/system.h"
#include "testcase.h"
#include "testsuite.h"
@@ -28,7 +27,7 @@ const size_t homeDirectorySize = homeDirectory.string().size();
void createTestDotfiles(const std::vector<std::string>& fileNames, const std::vector<std::string>& fileContents, bool asRoot = false)
{
EXPECT(fileNames.size() == fileContents.size(), return );
EXPECT(fileNames.size() == fileContents.size(), return);
if (root && !asRoot) {
setegid(Machine::the().gid());
@@ -503,7 +502,7 @@ TEST_CASE(PushDotfilesWithIgnorePattern)
TEST_CASE(PushDotfilesSelectivelyComment)
{
std::vector<std::string> fileNames;
for (size_t i = 0; i < 36; ++i) {
for (size_t i = 0; i < 44; ++i) {
fileNames.push_back("__test-file-" + std::to_string(i + 1));
}
@@ -678,6 +677,43 @@ test data /**/ comment
/* test data /**/ uncomment */
/* <<< */
)",
// Comment <!---->
"<!-- >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " -->" + R"(
test data <!----> comment
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + " -->" + R"(
test data <!----> comment
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + " -->" + R"(
test data <!----> comment
<!-- <<< -->
)",
" <!-- >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " -->" + R"(
test data <!----> comment
<!-- <<< -->
)",
// Uncomment <!---->
"<!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
<!-- test data <!----> uncomment -->
<!-- <<< -->
)",
" <!-- >>> distro="
+ distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
<!-- test data <!----> uncomment -->
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
<!-- test data <!----> uncomment -->
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
<!-- test data <!----> uncomment -->
<!-- <<< -->
)",
};
std::vector<std::string> pushedFileContents = {
@@ -846,6 +882,42 @@ test data /**/ uncomment
test data /**/ uncomment
/* <<< */
)",
// Comment <!---->
"<!-- >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " -->" + R"(
<!-- test data <!----> comment -->
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + placeholder + " user=" + username + " -->" + R"(
<!-- test data <!----> comment -->
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + placeholder + " -->" + R"(
<!-- test data <!----> comment -->
<!-- <<< -->
)",
" <!-- >>> distro=" + placeholder + " hostname=" + hostname + " user=" + username + " -->" + R"(
<!-- test data <!----> comment -->
<!-- <<< -->
)",
// Uncomment <!---->
"<!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
test data <!----> uncomment
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
test data <!----> uncomment
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
test data <!----> uncomment
<!-- <<< -->
)",
" <!-- >>> distro=" + distro + " hostname=" + hostname + " user=" + username + " -->" + R"(
test data <!----> uncomment
<!-- <<< -->
)",
};
createTestDotfiles(fileNames, fileContents);
@@ -866,7 +938,7 @@ test data /**/ uncomment
TEST_CASE(AddSystemDotfiles)
{
EXPECT(geteuid() == 0, return );
EXPECT(geteuid() == 0, return);
Config::the().setSystemPatterns({ "/etc/", "/usr/lib/" });
Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" });
@@ -881,7 +953,7 @@ TEST_CASE(AddSystemDotfiles)
TEST_CASE(PullSystemDotfiles)
{
EXPECT(geteuid() == 0, return );
EXPECT(geteuid() == 0, return);
createTestDotfiles({ "etc/group" }, { "" }, true);