feat(console+core): tui, core
This commit is contained in:
@@ -3,7 +3,7 @@ const models = @import("../models.zig");
|
||||
const Provider = @import("provider.zig").Provider;
|
||||
const ProviderVTable = @import("provider.zig").VTable;
|
||||
const GetItemsError = @import("provider.zig").GetItemsError;
|
||||
const DeinitTracker = @import("../deinit.zig").DeinitTracker;
|
||||
const InitContext = @import("provider.zig").InitContext;
|
||||
|
||||
const FullName = models.FullName;
|
||||
const Item = models.Item;
|
||||
@@ -11,21 +11,14 @@ const ItemEnum = models.ItemEnum;
|
||||
const Element = models.Element;
|
||||
const Container = models.Container;
|
||||
|
||||
fn loadChildren(container: *Container, deinitTracker: *DeinitTracker) void {
|
||||
// TODO: the container might be freed while this runs
|
||||
// Tab should hold something at pass it here
|
||||
fn loadChildren(container: *Container) void {
|
||||
defer {
|
||||
if (!deinitTracker.deinited)
|
||||
container.childrenLoading = false;
|
||||
container.childrenLoading = false;
|
||||
}
|
||||
|
||||
deinitTracker.use() catch {
|
||||
std.debug.print("already deinitialized", .{});
|
||||
return;
|
||||
};
|
||||
defer deinitTracker.unuse();
|
||||
|
||||
var dir = std.fs.cwd().openDir(container.item.nativePath.path, .{ .iterate = true }) catch {
|
||||
if (deinitTracker.deinited) 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;
|
||||
@@ -36,7 +29,6 @@ fn loadChildren(container: *Container, deinitTracker: *DeinitTracker) void {
|
||||
while (it.next() catch return) |entry| {
|
||||
const child = container.item.fullName.getChild(entry.name, container.item.allocator) catch return;
|
||||
|
||||
if (deinitTracker.deinited) return;
|
||||
container.children.append(child) catch return;
|
||||
}
|
||||
}
|
||||
@@ -48,11 +40,11 @@ const VTable: ProviderVTable = .{
|
||||
pub fn getItemByFullNameGeneric(
|
||||
context: *anyopaque,
|
||||
fullName: FullName,
|
||||
initContext: *const InitContext,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) GetItemsError!Item {
|
||||
) GetItemsError!*Item {
|
||||
const self: *LocalContentProvider = @ptrCast(@alignCast(context));
|
||||
return self.getItemByFullName(fullName, allocator, globalAllocator);
|
||||
return self.getItemByFullName(fullName, initContext, allocator);
|
||||
}
|
||||
|
||||
pub const LocalContentProvider = struct {
|
||||
@@ -61,9 +53,9 @@ pub const LocalContentProvider = struct {
|
||||
pub fn getItemByFullName(
|
||||
self: *LocalContentProvider,
|
||||
fullName: FullName,
|
||||
initContext: *const InitContext,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) GetItemsError!Item {
|
||||
) GetItemsError!*Item {
|
||||
const stat = std.fs.cwd().statFile(fullName.path) catch return GetItemsError.NotExists;
|
||||
|
||||
return switch (stat.kind) {
|
||||
@@ -84,12 +76,15 @@ pub const LocalContentProvider = struct {
|
||||
val,
|
||||
fullName,
|
||||
allocator,
|
||||
globalAllocator,
|
||||
);
|
||||
|
||||
try self.threadPool.spawn(loadChildren, .{ container, container.item.deinitTracker });
|
||||
if (!initContext.skipChildInit) {
|
||||
try self.threadPool.spawn(loadChildren, .{container});
|
||||
} else {
|
||||
container.childrenLoading = false;
|
||||
}
|
||||
|
||||
break :blk container.item;
|
||||
break :blk &container.item;
|
||||
},
|
||||
.file => blk: {
|
||||
const element = try allocator.create(Element);
|
||||
@@ -107,10 +102,9 @@ pub const LocalContentProvider = struct {
|
||||
val,
|
||||
fullName,
|
||||
allocator,
|
||||
globalAllocator,
|
||||
);
|
||||
|
||||
break :blk element.item;
|
||||
break :blk &element.item;
|
||||
},
|
||||
else => @panic(
|
||||
"Unsupported file type\n",
|
||||
@@ -124,29 +118,17 @@ pub const LocalContentProvider = struct {
|
||||
innerItem: ItemEnum,
|
||||
fullName: FullName,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) !void {
|
||||
var deinitTracker = try globalAllocator.create(DeinitTracker);
|
||||
deinitTracker.init(globalAllocator);
|
||||
|
||||
const basename = std.fs.path.basename(fullName.path);
|
||||
|
||||
const name = try allocator.alloc(u8, basename.len);
|
||||
@memcpy(name, basename);
|
||||
|
||||
const displayName = try allocator.alloc(u8, basename.len);
|
||||
@memcpy(displayName, basename);
|
||||
|
||||
const fullName2 = try allocator.alloc(u8, fullName.path.len);
|
||||
@memcpy(fullName2, fullName.path);
|
||||
|
||||
const nativePath = try allocator.alloc(u8, fullName.path.len);
|
||||
@memcpy(nativePath, fullName.path);
|
||||
const name = try allocator.dupe(u8, basename);
|
||||
const displayName = try allocator.dupe(u8, basename);
|
||||
const fullName2 = try allocator.dupe(u8, fullName.path);
|
||||
const nativePath = try allocator.dupe(u8, fullName.path);
|
||||
|
||||
item.* = Item{
|
||||
.allocator = allocator,
|
||||
.provider = contentProvider.provider(),
|
||||
.deinitTracker = deinitTracker,
|
||||
.name = name,
|
||||
.displayName = displayName,
|
||||
.fullName = .{ .path = fullName2 },
|
||||
|
||||
Reference in New Issue
Block a user