diff --git a/build.zig b/build.zig index ebbe583..e45ff8f 100644 --- a/build.zig +++ b/build.zig @@ -4,6 +4,16 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const sandbox_mod = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + const sandbox_exe = b.addExecutable(.{ + .name = "sandbox_ftime", + .root_module = sandbox_mod, + }); + const console_exe_mod = b.createModule(.{ .root_source_file = b.path("src/console_main.zig"), .target = target, @@ -41,6 +51,9 @@ pub fn build(b: *std.Build) void { gui_exe.root_module.addImport("dvui", dvui.module("dvui_raylib")); b.installArtifact(gui_exe); + const run_sandbox_cmd = b.addRunArtifact(sandbox_exe); + run_sandbox_cmd.step.dependOn(b.getInstallStep()); + const run_console_cmd = b.addRunArtifact(console_exe); run_console_cmd.step.dependOn(b.getInstallStep()); @@ -52,6 +65,9 @@ pub fn build(b: *std.Build) void { run_gui_cmd.addArgs(args); } + const run_sandbox_step = b.step("run:s", "Run the sandbox"); + run_sandbox_step.dependOn(&run_sandbox_cmd.step); + const run_console_step = b.step("run:console", "Run the console app"); run_console_step.dependOn(&run_console_cmd.step); diff --git a/src/core/provider/local.zig b/src/core/provider/local.zig index 50f9923..c60b111 100644 --- a/src/core/provider/local.zig +++ b/src/core/provider/local.zig @@ -19,8 +19,8 @@ fn loadChildren(container: *Container) void { } var dir = std.fs.cwd().openDir(container.item.nativePath.path, .{ .iterate = true }) catch { - const errorContent = std.fmt.allocPrint(container.item.allocator, "Could not open directory '{s}'.", .{container.item.nativePath.path}) catch return; - container.item.errors.append(.{ .content = errorContent }) catch return; + // const errorContent = std.fmt.allocPrint(container.item.allocator, "Could not open directory '{s}'.", .{container.item.nativePath.path}) catch return; + // container.item.errors.append(.{ .content = errorContent }) catch return; return; }; defer dir.close(); @@ -56,9 +56,27 @@ pub const LocalContentProvider = struct { initContext: *const InitContext, allocator: std.mem.Allocator, ) GetItemsError!*Item { - const stat = std.fs.cwd().statFile(fullName.path) catch return GetItemsError.NotExists; + const path = try std.mem.replaceOwned(u8, allocator, fullName.path, "/", "\\"); + defer allocator.free(path); - return switch (stat.kind) { + const kind: union(enum) { directory, file } = blk: { + // FIXME: properly handle different errors + var dir = std.fs.cwd().openDir(path, .{}); + if (dir) |*d| { + d.close(); + break :blk .directory; + } else |_| {} + + var file = std.fs.cwd().openFile(path, .{}); + if (file) |*f| { + f.close(); + break :blk .file; + } else |_| {} + + return GetItemsError.NotExists; + }; + + return switch (kind) { .directory => blk: { const container = try allocator.create(Container); container.* = Container{ @@ -106,9 +124,6 @@ pub const LocalContentProvider = struct { break :blk &element.item; }, - else => @panic( - "Unsupported file type\n", - ), }; } diff --git a/src/core/provider/provider.zig b/src/core/provider/provider.zig index c7d222a..5f54285 100644 --- a/src/core/provider/provider.zig +++ b/src/core/provider/provider.zig @@ -10,6 +10,7 @@ pub const VTable = struct { pub const GetItemsError = error{ OutOfMemory, NotExists, + NotSupported, }; pub const Provider = struct { diff --git a/src/core/tab/tab.zig b/src/core/tab/tab.zig index 8928c03..139f82d 100644 --- a/src/core/tab/tab.zig +++ b/src/core/tab/tab.zig @@ -17,7 +17,7 @@ pub const Tab = struct { _private: Private, const Private = struct { - currentItemsAllocator: ?std.heap.ArenaAllocator, + currentItemsAllocator: std.heap.ArenaAllocator, }; pub fn init( @@ -31,7 +31,7 @@ pub const Tab = struct { .currentLocation = null, .threadPool = threadPool, ._private = .{ - .currentItemsAllocator = null, + .currentItemsAllocator = std.heap.ArenaAllocator.init(allocator), }, }; } @@ -51,9 +51,7 @@ pub const Tab = struct { loadItems(self, location) catch return; } fn loadItems(self: *Tab, location: *Container) !void { - if (self._private.currentItemsAllocator) |arena| { - arena.deinit(); - } + _ = self._private.currentItemsAllocator.reset(.retain_capacity); { self.currentItems.mutex.lock(); @@ -63,16 +61,14 @@ pub const Tab = struct { } } - self._private.currentItemsAllocator = std.heap.ArenaAllocator.init(self.allocator); - const arenaAllocator = &self._private.currentItemsAllocator.?; + const arenaAllocator = &self._private.currentItemsAllocator; const arena = arenaAllocator.allocator(); var threadSafeAllocator = std.heap.ThreadSafeAllocator{ .child_allocator = arena }; const allocator = threadSafeAllocator.allocator(); errdefer { - arenaAllocator.deinit(); - self._private.currentItemsAllocator = null; + _ = self._private.currentItemsAllocator.reset(.free_all); } { @@ -80,18 +76,23 @@ pub const Tab = struct { defer self.currentItems.mutex.unlock(); self.currentItems.data = std.ArrayList(*Item).init(allocator); - errdefer { - self.currentItems.data.?.deinit(); - self.currentItems.data = null; - } + } + errdefer { + self.currentItems.data.?.deinit(); + self.currentItems.data = null; } + //TODO: add async while (location.childrenLoading) { std.Thread.sleep(1 * std.time.ns_per_ms); } for (location.children.items) |item| { - const resolvedItem = try location.item.provider.getItemByFullName(item, &.{ .skipChildInit = false }, allocator); + const resolvedItem = location.item.provider.getItemByFullName(item, &.{ .skipChildInit = true }, allocator) catch |e| { + //TODO: save error to container errors + std.debug.print("error {} {s}\r\n", .{e, item.path}); + continue; + }; self.currentItems.mutex.lock(); defer self.currentItems.mutex.unlock(); @@ -105,8 +106,6 @@ pub const Tab = struct { if (self.currentLocation) |c| { c.item.deinit(); } - if (self._private.currentItemsAllocator) |arena| { - arena.deinit(); - } + self._private.currentItemsAllocator.deinit(); } }; diff --git a/src/gui/main.zig b/src/gui/main.zig index 057a919..5d5f2cb 100644 --- a/src/gui/main.zig +++ b/src/gui/main.zig @@ -72,13 +72,14 @@ pub fn main() !void { .tab = tab1, }, }; + defer model.core_model.deinit(); try pool.spawn(core.data_loop, .{&model.core_model}); // init Raylib backend (creates OS window) // initWindow() means the backend calls CloseWindow for you in deinit() var backend = try RaylibBackend.initWindow(.{ - .gpa = gpa, + .gpa = allocator, .size = .{ .w = 800.0, .h = 600.0 }, .vsync = vsync, .title = "DVUI Raylib Standalone Example", @@ -88,7 +89,7 @@ pub fn main() !void { backend.log_events = true; // init dvui Window (maps onto a single OS window) - var win = try dvui.Window.init(@src(), gpa, backend.backend(), .{}); + var win = try dvui.Window.init(@src(), allocator, backend.backend(), .{}); defer win.deinit(); main_loop: while (true) { diff --git a/src/main.zig b/src/main.zig index a2f3535..efbbdf6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -20,21 +20,26 @@ pub fn main() !void { var localContentProvider = local_provider.LocalContentProvider{ .threadPool = &pool }; const fullName: models.FullName = .{ .path = "/home/adam/1.txt" }; - var item = try localContentProvider.getItemByFullName(fullName, allocator, allocator); + var item = try localContentProvider.getItemByFullName(fullName, &.{}, allocator); defer item.deinit(); - const homeFullName: models.FullName = .{ .path = "/home/adam/" }; - var homeItem = try localContentProvider.getItemByFullName(homeFullName, allocator, allocator); - defer homeItem.deinit(); + const homeFullName: models.FullName = .{ .path = "C:\\Users\\adakovacs" }; + const homeItem = try localContentProvider.getItemByFullName(homeFullName, &.{}, allocator); + // defer homeItem.deinit(); const c = switch (homeItem.item) { .container => |c| c, .element => unreachable, }; - var tab1 = Tab.create(&pool, allocator); + var tab1 = try allocator.create(Tab); + defer allocator.destroy(tab1); + + tab1.init(&pool, allocator); + defer tab1.deinit(); + tab1.setCurrentLocation(c); - std.Thread.sleep(1 * std.time.ns_per_s); + std.Thread.sleep(5 * std.time.ns_per_s); } const std = @import("std");