feat: cleanup appstate, move sort to tab

This commit is contained in:
2025-05-26 17:22:23 +02:00
parent c4c901f1d4
commit 095b8659d6
9 changed files with 270 additions and 159 deletions

View File

@@ -1,4 +1,4 @@
pub fn handle(action: Action, appState: *AppState, arena: std.mem.Allocator) !void {
pub fn handle(action: Action, appState: *AppState, arena: std.mem.Allocator) !ActionResult {
switch (action) {
.GoUp => {
if (appState.currentTab.currentLocation) |currentLocation| {
@@ -6,14 +6,76 @@ pub fn handle(action: Action, appState: *AppState, arena: std.mem.Allocator) !vo
if (parent) |p| {
const path = try arena.dupe(u8, p.path.path);
try appState.currentTab.setCurrentLocation(.{ .path = path });
return ActionResult.Handled;
} else return error.NoParent;
} else return error.NoCurrentLocation;
},
.Enter => unreachable,
.Enter => {},
.SelectNext => {
return selectNextIndex(appState.currentTab, .Next);
},
.SelectPrevious => {
return selectNextIndex(appState.currentTab, .Previous);
},
}
return ActionResult.None;
}
const SelectStep = enum {
Next,
Previous,
NextPage,
PreviousPage,
};
fn selectNextIndex(currentTab: *Tab, step: SelectStep) ActionResult {
const index = blk: {
currentTab.currentItems.mutex.lock();
defer currentTab.currentItems.mutex.unlock();
const currentItems = currentTab.currentItems.data orelse return ActionResult.None;
if (currentTab.currentItem) |*currentItem| {
const index = indexOf(Arc(*models.Item), models.FullName, currentItems.items, currentItem, arcFullNameEql);
if (index) |i| {
break :blk switch (step) {
.Next => i +| 1,
.Previous => i -| 1,
.NextPage => i +| 8,
.PreviousPage => i +| 8,
};
}
}
break :blk 0;
};
currentTab.selectItem(index) catch {
//TODO
};
return ActionResult.Handled;
}
fn arcFullNameEql(arcLhs: *const Arc(*models.Item), rhs: *const models.FullName) bool {
const arcLhs2 = arcLhs.retain();
defer if (arcLhs2.releaseUnwrap()) |i| i.deinit();
return models.FullName.eql(&arcLhs2.value.*.fullName, rhs);
}
fn indexOf(comptime TSlice: type, comptime TItem: type, slice: []const TSlice, value: *const TItem, compare: *const fn (a: *const TSlice, b: *const TItem) bool) ?usize {
for (0.., slice) |i, *item| {
if (compare(item, value)) return i;
}
return null;
}
const std = @import("std");
const Action = @import("action.zig").Action;
const ActionResult = @import("action.zig").ActionResult;
const AppState = @import("../app_state.zig").AppState;
const RootProvider = @import("../provider/root.zig").RootProvider;
const models = @import("../models.zig");
const Arc = @import("zigrc").Arc;
const Tab = @import("../tab/tab.zig").Tab;