update randomwallpaper

This commit is contained in:
LordMZTE 2022-08-05 14:36:41 +02:00
parent 309f04ce26
commit 1f96927106
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 59 additions and 61 deletions

View 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 => {},
}
}
}

View file

@ -1,6 +1,6 @@
const std = @import("std");
const xinerama = @import("xinerama.zig");
const Walker = @import("walker.zig").Walker;
const Walker = @import("Walker.zig");
const c = @import("ffi.zig").c;
pub fn main() !u8 {
@ -12,9 +12,9 @@ pub fn main() !u8 {
defer walker.deinit();
try walker.walk(
try std.fs.openDirAbsolute(
try std.fs.openIterableDirAbsolute(
"/usr/share/backgrounds/",
Walker.open_opts,
.{},
),
);
@ -52,6 +52,6 @@ pub fn main() !u8 {
fn walkLocalWps(walker: *Walker, home_s: []const u8) !void {
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);
}

View file

@ -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 => {},
}
}
}
};