feat(core): appstate
This commit is contained in:
@@ -1,15 +1,4 @@
|
||||
const std = @import("std");
|
||||
const models = @import("../models.zig");
|
||||
const Provider = @import("provider.zig").Provider;
|
||||
const ProviderVTable = @import("provider.zig").VTable;
|
||||
const GetItemsError = @import("provider.zig").GetItemsError;
|
||||
const InitContext = @import("provider.zig").InitContext;
|
||||
|
||||
const FullName = models.FullName;
|
||||
const Item = models.Item;
|
||||
const ItemEnum = models.ItemEnum;
|
||||
const Element = models.Element;
|
||||
const Container = models.Container;
|
||||
pub const LocalProviderId = "local";
|
||||
|
||||
// TODO: the container might be freed while this runs
|
||||
// Tab should hold something at pass it here
|
||||
@@ -18,6 +7,7 @@ fn loadChildren(container: *Container) void {
|
||||
container.childrenLoading = false;
|
||||
}
|
||||
|
||||
std.debug.print("load children {s}", .{container.item.nativePath.path});
|
||||
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;
|
||||
@@ -35,6 +25,8 @@ fn loadChildren(container: *Container) void {
|
||||
|
||||
const VTable: ProviderVTable = .{
|
||||
.getItemByFullName = getItemByFullNameGeneric,
|
||||
.canHandle = canHandleGeneric,
|
||||
.deinit = deinitGeneric,
|
||||
};
|
||||
|
||||
pub fn getItemByFullNameGeneric(
|
||||
@@ -47,6 +39,43 @@ pub fn getItemByFullNameGeneric(
|
||||
return self.getItemByFullName(fullName, initContext, allocator);
|
||||
}
|
||||
|
||||
pub fn canHandleGeneric(
|
||||
context: *anyopaque,
|
||||
fullName: FullName,
|
||||
) bool {
|
||||
const self: *LocalContentProvider = @ptrCast(@alignCast(context));
|
||||
return self.canHandle(fullName);
|
||||
}
|
||||
|
||||
pub fn deinitGeneric(_: *anyopaque) void {}
|
||||
|
||||
pub fn getFullNameByNativePath(allocator: std.mem.Allocator, nativePath: NativePath) !FullName {
|
||||
const correct_sep = try std.mem.replaceOwned(u8, allocator, nativePath.path, std.fs.path.sep_str, "/");
|
||||
defer allocator.free(correct_sep);
|
||||
|
||||
const asd = if (std.mem.startsWith(u8, correct_sep, "/"))
|
||||
correct_sep[1..]
|
||||
else
|
||||
correct_sep;
|
||||
|
||||
const full_name_path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ LocalProviderId, asd });
|
||||
|
||||
return .{ .path = full_name_path };
|
||||
}
|
||||
|
||||
pub fn getNativePathByFullName(allocator: std.mem.Allocator, fullName: FullName) ![]u8 {
|
||||
const fullNameWithoutId = fullName.path[LocalProviderId.len + 1 ..];
|
||||
var native_path = try std.mem.replaceOwned(u8, allocator, fullNameWithoutId, "/", std.fs.path.sep_str);
|
||||
|
||||
if (native_os == .linux) {
|
||||
const linux_native_path = try std.fmt.allocPrint(allocator, "/{s}", .{native_path});
|
||||
allocator.free(native_path);
|
||||
native_path = linux_native_path;
|
||||
}
|
||||
|
||||
return native_path;
|
||||
}
|
||||
|
||||
pub const LocalContentProvider = struct {
|
||||
threadPool: *std.Thread.Pool,
|
||||
|
||||
@@ -56,7 +85,7 @@ pub const LocalContentProvider = struct {
|
||||
initContext: *const InitContext,
|
||||
allocator: std.mem.Allocator,
|
||||
) GetItemsError!*Item {
|
||||
const native_path = try std.mem.replaceOwned(u8, allocator, fullName.path, "/", std.fs.path.sep_str);
|
||||
const native_path = try getNativePathByFullName(allocator, fullName);
|
||||
defer allocator.free(native_path);
|
||||
|
||||
const kind: union(enum) { directory, file } = blk: {
|
||||
@@ -139,7 +168,10 @@ pub const LocalContentProvider = struct {
|
||||
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);
|
||||
const nativePath = try getNativePathByFullName(allocator, fullName);
|
||||
|
||||
const parentFullName = try fullName.getParent(allocator);
|
||||
const parent = if (parentFullName) |p| models.AbsolutePath{ .path = p, .type = .container } else null;
|
||||
|
||||
item.* = Item{
|
||||
.allocator = allocator,
|
||||
@@ -148,12 +180,19 @@ pub const LocalContentProvider = struct {
|
||||
.displayName = displayName,
|
||||
.fullName = .{ .path = fullName2 },
|
||||
.nativePath = models.NativePath{ .path = nativePath },
|
||||
.parent = null,
|
||||
.parent = parent,
|
||||
.item = innerItem,
|
||||
.errors = std.ArrayList(models.Error).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn canHandle(
|
||||
_: *LocalContentProvider,
|
||||
fullName: FullName,
|
||||
) bool {
|
||||
return std.mem.startsWith(u8, fullName.path, "local/");
|
||||
}
|
||||
|
||||
pub fn provider(self: *LocalContentProvider) Provider {
|
||||
return Provider{
|
||||
.object = self,
|
||||
@@ -161,3 +200,18 @@ pub const LocalContentProvider = struct {
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
const models = @import("../models.zig");
|
||||
const Provider = @import("provider.zig").Provider;
|
||||
const ProviderVTable = @import("provider.zig").VTable;
|
||||
const GetItemsError = @import("provider.zig").GetItemsError;
|
||||
const InitContext = @import("provider.zig").InitContext;
|
||||
|
||||
const FullName = models.FullName;
|
||||
const NativePath = models.NativePath;
|
||||
const Item = models.Item;
|
||||
const ItemEnum = models.ItemEnum;
|
||||
const Element = models.Element;
|
||||
const Container = models.Container;
|
||||
const native_os = @import("builtin").os.tag;
|
||||
|
||||
Reference in New Issue
Block a user