feat(console+core): tui, core
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Mutex = std.Thread.Mutex;
|
||||
|
||||
const Error = error{
|
||||
Deinitialized,
|
||||
};
|
||||
|
||||
pub const DeinitTracker = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
deinited: bool,
|
||||
usageCount: u16,
|
||||
mutex: Mutex,
|
||||
|
||||
pub fn init(self: *DeinitTracker, allocator: std.mem.Allocator) void {
|
||||
self.* = .{
|
||||
.allocator = allocator,
|
||||
.deinited = false,
|
||||
.usageCount = 1,
|
||||
.mutex = Mutex{},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn use(self: *DeinitTracker) Error!void {
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
if (self.deinited) return Error.Deinitialized;
|
||||
|
||||
self.usageCount += 1;
|
||||
}
|
||||
|
||||
pub fn unuse(self: *DeinitTracker) void {
|
||||
self.mutex.lock();
|
||||
defer self.mutex.unlock();
|
||||
|
||||
self.usageCount -= 1;
|
||||
|
||||
if (self.usageCount == 0) {
|
||||
self.allocator.destroy(self);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,12 +1,10 @@
|
||||
const std = @import("std");
|
||||
const Provider = @import("provider/provider.zig").Provider;
|
||||
const DeinitTracker = @import("deinit.zig").DeinitTracker;
|
||||
const consts = @import("consts.zig");
|
||||
|
||||
pub const Item = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
provider: Provider,
|
||||
deinitTracker: *DeinitTracker,
|
||||
name: []const u8,
|
||||
displayName: []const u8,
|
||||
fullName: FullName,
|
||||
@@ -20,8 +18,6 @@ pub const Item = struct {
|
||||
self.allocator.free(self.displayName);
|
||||
self.allocator.free(self.fullName.path);
|
||||
self.allocator.free(self.nativePath.path);
|
||||
self.deinitTracker.deinited = true;
|
||||
self.deinitTracker.unuse();
|
||||
for (self.errors.items) |e| {
|
||||
self.allocator.free(e.content);
|
||||
}
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -2,9 +2,9 @@ pub const VTable = struct {
|
||||
getItemByFullName: *const fn (
|
||||
self: *anyopaque,
|
||||
fullName: FullName,
|
||||
initContext: *const InitContext,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) GetItemsError!Item,
|
||||
) GetItemsError!*Item,
|
||||
};
|
||||
|
||||
pub const GetItemsError = error{
|
||||
@@ -19,13 +19,17 @@ pub const Provider = struct {
|
||||
pub inline fn getItemByFullName(
|
||||
self: *const Provider,
|
||||
fullName: FullName,
|
||||
initContext: *const InitContext,
|
||||
allocator: std.mem.Allocator,
|
||||
globalAllocator: std.mem.Allocator,
|
||||
) GetItemsError!Item {
|
||||
return &self.vtable.getItemByFullName(self.object, fullName, allocator, globalAllocator);
|
||||
) GetItemsError!*Item {
|
||||
return self.vtable.getItemByFullName(self.object, fullName, initContext, allocator);
|
||||
}
|
||||
};
|
||||
|
||||
pub const InitContext = struct {
|
||||
skipChildInit: bool = false,
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
const models = @import("../models.zig");
|
||||
const Item = models.Item;
|
||||
|
||||
@@ -9,7 +9,8 @@ const Item = models.Item;
|
||||
pub const Tab = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
currentLocation: ?*Container,
|
||||
currentItems: ?std.ArrayList(Item),
|
||||
currentItems: ?std.ArrayList(*Item),
|
||||
currentItemsChanged: bool = false,
|
||||
threadPool: *std.Thread.Pool,
|
||||
_private: Private,
|
||||
|
||||
@@ -41,17 +42,46 @@ pub const Tab = struct {
|
||||
self.currentLocation = newLocation;
|
||||
|
||||
//TODO: Proper error handling
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItems, .{ self, newLocation }) catch unreachable;
|
||||
std.Thread.Pool.spawn(self.threadPool, loadItemsWrapper, .{ self, newLocation }) catch unreachable;
|
||||
}
|
||||
|
||||
fn loadItems(self: *Tab, location: *Container) void {
|
||||
fn loadItemsWrapper(self: *Tab, location: *Container) void {
|
||||
loadItems(self, location) catch return;
|
||||
}
|
||||
fn loadItems(self: *Tab, location: *Container) !void {
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
if (self.currentItems) |items| {
|
||||
items.deinit();
|
||||
}
|
||||
|
||||
self._private.currentItemsAllocator = std.heap.ArenaAllocator.init(self.allocator);
|
||||
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.currentItems = std.ArrayList(*Item).init(allocator);
|
||||
errdefer {
|
||||
self.currentItems.?.deinit();
|
||||
self.currentItems = null;
|
||||
}
|
||||
|
||||
while (location.childrenLoading) {
|
||||
std.Thread.sleep(1 * std.time.ns_per_ms);
|
||||
}
|
||||
|
||||
for (location.children.items) |item| {
|
||||
_ = item;
|
||||
const resolvedItem = try location.item.provider.getItemByFullName(item, &.{ .skipChildInit = false }, allocator);
|
||||
try self.currentItems.?.append(resolvedItem);
|
||||
self.currentItemsChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +92,5 @@ pub const Tab = struct {
|
||||
if (self._private.currentItemsAllocator) |arena| {
|
||||
arena.deinit();
|
||||
}
|
||||
self.allocator.destroy(self);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user