mirror of
https://mzte.de/git/LordMZTE/dotfiles.git
synced 2025-03-04 20:39:59 +01:00
update randomwallpaper
This commit is contained in:
parent
309f04ce26
commit
1f96927106
3 changed files with 59 additions and 61 deletions
55
scripts/randomwallpaper/src/Walker.zig
Normal file
55
scripts/randomwallpaper/src/Walker.zig
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
files: std.ArrayList([]u8),
|
||||||
|
filename_arena: std.heap.ArenaAllocator,
|
||||||
|
buf: [64]u8 = undefined,
|
||||||
|
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
pub fn init(alloc: std.mem.Allocator) Self {
|
||||||
|
return Self{
|
||||||
|
.files = std.ArrayList([]u8).init(alloc),
|
||||||
|
.filename_arena = std.heap.ArenaAllocator.init(alloc),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Self) void {
|
||||||
|
self.filename_arena.deinit();
|
||||||
|
self.files.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn walk(self: *Self, dir: std.fs.IterableDir) anyerror!void {
|
||||||
|
var iter = dir.iterate();
|
||||||
|
while (try iter.next()) |e| {
|
||||||
|
switch (e.kind) {
|
||||||
|
.File => {
|
||||||
|
const path = try dir.dir.realpathAlloc(self.filename_arena.allocator(), e.name);
|
||||||
|
try self.files.append(path);
|
||||||
|
},
|
||||||
|
.Directory => {
|
||||||
|
var subdir = try dir.dir.openIterableDir(e.name, .{});
|
||||||
|
defer subdir.close();
|
||||||
|
try self.walk(subdir);
|
||||||
|
},
|
||||||
|
.SymLink => {
|
||||||
|
const p = try dir.dir.readLink(e.name, &self.buf);
|
||||||
|
var subdir = dir.dir.openIterableDir(p, .{}) catch |err| {
|
||||||
|
switch (err) {
|
||||||
|
std.fs.Dir.OpenError.NotDir => {
|
||||||
|
const fpath = try self.filename_arena.allocator().dupe(u8, p);
|
||||||
|
try self.files.append(fpath);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
return err;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
defer subdir.close();
|
||||||
|
|
||||||
|
try self.walk(subdir);
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const xinerama = @import("xinerama.zig");
|
const xinerama = @import("xinerama.zig");
|
||||||
const Walker = @import("walker.zig").Walker;
|
const Walker = @import("Walker.zig");
|
||||||
const c = @import("ffi.zig").c;
|
const c = @import("ffi.zig").c;
|
||||||
|
|
||||||
pub fn main() !u8 {
|
pub fn main() !u8 {
|
||||||
|
@ -12,9 +12,9 @@ pub fn main() !u8 {
|
||||||
defer walker.deinit();
|
defer walker.deinit();
|
||||||
|
|
||||||
try walker.walk(
|
try walker.walk(
|
||||||
try std.fs.openDirAbsolute(
|
try std.fs.openIterableDirAbsolute(
|
||||||
"/usr/share/backgrounds/",
|
"/usr/share/backgrounds/",
|
||||||
Walker.open_opts,
|
.{},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -52,6 +52,6 @@ pub fn main() !u8 {
|
||||||
|
|
||||||
fn walkLocalWps(walker: *Walker, home_s: []const u8) !void {
|
fn walkLocalWps(walker: *Walker, home_s: []const u8) !void {
|
||||||
const home = std.fs.cwd().openDir(home_s, .{}) catch return;
|
const home = std.fs.cwd().openDir(home_s, .{}) catch return;
|
||||||
const local_wp = home.openDir(".local/share/backgrounds/", Walker.open_opts) catch return;
|
const local_wp = home.openIterableDir(".local/share/backgrounds/", .{}) catch return;
|
||||||
try walker.walk(local_wp);
|
try walker.walk(local_wp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,57 +0,0 @@
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
pub const Walker = struct {
|
|
||||||
files: std.ArrayList([]u8),
|
|
||||||
filename_arena: std.heap.ArenaAllocator,
|
|
||||||
buf: [64]u8 = undefined,
|
|
||||||
|
|
||||||
pub const open_opts = std.fs.Dir.OpenDirOptions{ .iterate = true };
|
|
||||||
|
|
||||||
pub fn init(alloc: std.mem.Allocator) Walker {
|
|
||||||
return Walker{
|
|
||||||
.files = std.ArrayList([]u8).init(alloc),
|
|
||||||
.filename_arena = std.heap.ArenaAllocator.init(alloc),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *Walker) void {
|
|
||||||
self.filename_arena.deinit();
|
|
||||||
self.files.deinit();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn walk(self: *Walker, dir: std.fs.Dir) anyerror!void {
|
|
||||||
var iter = dir.iterate();
|
|
||||||
while (try iter.next()) |e| {
|
|
||||||
switch (e.kind) {
|
|
||||||
.File => {
|
|
||||||
const path = try dir.realpathAlloc(self.filename_arena.allocator(), e.name);
|
|
||||||
try self.files.append(path);
|
|
||||||
},
|
|
||||||
.Directory => {
|
|
||||||
var subdir = try dir.openDir(e.name, open_opts);
|
|
||||||
defer subdir.close();
|
|
||||||
try self.walk(subdir);
|
|
||||||
},
|
|
||||||
.SymLink => {
|
|
||||||
const p = try dir.readLink(e.name, &self.buf);
|
|
||||||
var subdir = dir.openDir(p, open_opts) catch |err| {
|
|
||||||
switch (err) {
|
|
||||||
std.fs.Dir.OpenError.NotDir => {
|
|
||||||
const fpath = try self.filename_arena.allocator().dupe(u8, p);
|
|
||||||
try self.files.append(fpath);
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
return err;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
};
|
|
||||||
defer subdir.close();
|
|
||||||
|
|
||||||
try self.walk(subdir);
|
|
||||||
},
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
Loading…
Add table
Reference in a new issue