Skip to content

Commit

Permalink
general refactors and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BrookJeynes committed Jun 5, 2024
1 parent d86f021 commit 6c09139
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 94 deletions.
19 changes: 10 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ const Build = blk: {
};

pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const build_options = b.addOptions();
build_options.step.name = "build options";
const build_options_module = build_options.createModule();
build_options.addOption([]const u8, "minimum_zig_string", minimum_zig_version);
build_options.addOption(std.SemanticVersion, "zfe_version", version);

// Building targets for release.
const build_all = b.option(bool, "build-all-targets", "Build all targets in ReleaseSafe mode.") orelse false;
if (build_all) {
try build_targets(b, build_options_module);
return;
}

const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const libvaxis = b.dependency("vaxis", .{ .target = target }).module("vaxis");
const fuzzig = b.dependency("fuzzig", .{ .target = target }).module("fuzzig");
const zuid = b.dependency("zuid", .{ .target = target }).module("zuid");
Expand All @@ -48,12 +55,6 @@ pub fn build(b: *std.Build) !void {
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

// Building targets for release.
const build_all = b.option(bool, "build-all-targets", "Build all targets in ReleaseSafe mode.") orelse false;
if (build_all) {
try build_targets(b, build_options_module);
}
}

fn build_targets(b: *std.Build, build_options_module: *std.Build.Module) !void {
Expand Down
4 changes: 4 additions & 0 deletions release_zip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
zip -r ./zig-out/x86_64-linux.zip ./zig-out/x86_64-linux/
zip -r ./zig-out/x86_64-macos.zip ./zig-out/x86_64-macos/
zip -r ./zig-out/aarch64-linux.zip ./zig-out/aarch64-linux/
zip -r ./zig-out/aarch64-macos.zip ./zig-out/aarch64-macos/
111 changes: 54 additions & 57 deletions src/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Notification = @import("./notification.zig");
const config = &@import("./config.zig").config;
const List = @import("./list.zig").List;
const Directories = @import("./directories.zig");
const CircStack = @import("./circ_stack.zig").CircularStack;

const zuid = @import("zuid");

Expand All @@ -24,24 +25,21 @@ pub const State = enum {
rename,
};

const Effect = enum {
const InputReturnStatus = enum {
exit,
default,
};

const ActionPaths = struct {
/// Allocated.
old: []const u8,
/// Allocated.
new: []const u8,
};

pub const Action = union(enum) {
delete: struct {
/// Allocated.
old_path: []const u8,
/// Allocated.
tmp_path: []const u8,
},
rename: struct {
/// Allocated.
old_path: []const u8,
/// Allocated.
new_path: []const u8,
},
delete: ActionPaths,
rename: ActionPaths,
};

const Event = union(enum) {
Expand All @@ -52,15 +50,16 @@ const Event = union(enum) {
const top_div = 1;
const info_div = 1;
const bottom_div = 1;
const actions_len = 100;

const App = @This();

alloc: std.mem.Allocator,
vx: vaxis.Vaxis = undefined,
tty: vaxis.Tty = undefined,
logger: Logger,
state: State = State.normal,
actions: std.ArrayList(Action),
state: State = .normal,
actions: CircStack(Action, actions_len),

// Used to detect whether to re-render an image.
current_item_path_buf: [std.fs.max_path_bytes]u8 = undefined,
Expand Down Expand Up @@ -96,28 +95,23 @@ pub fn init(alloc: std.mem.Allocator) !App {
.logger = Logger{},
.text_input = TextInput.init(alloc, &vx.unicode),
.notification = Notification{},
.actions = std.ArrayList(Action).init(alloc),
.actions = CircStack(Action, actions_len).init(),
.last_known_height = vx.window().height,
};
}

pub fn deinit(self: *App) void {
for (self.actions.items) |action| {
for (self.actions.buf[0..self.actions.count]) |action| {
switch (action) {
.delete => |a| {
self.alloc.free(a.tmp_path);
self.alloc.free(a.old_path);
},
.rename => |a| {
self.alloc.free(a.new_path);
self.alloc.free(a.old_path);
.delete, .rename => |a| {
self.alloc.free(a.new);
self.alloc.free(a.old);
},
}
}

self.directories.deinit();
self.text_input.deinit();
self.actions.deinit();
self.vx.deinit(self.alloc, self.tty.anyWriter());
self.tty.deinit();
}
Expand Down Expand Up @@ -164,7 +158,7 @@ pub fn inputToSlice(self: *App) []const u8 {
return self.text_input.sliceToCursor(&self.text_input_buf);
}

pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !Effect {
pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !InputReturnStatus {
switch (event) {
.key_press => |key| {
if ((key.codepoint == 'c' and key.mods.ctrl) or key.codepoint == 'q') {
Expand All @@ -187,7 +181,7 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !
}
};

if (self.directories.history.popOrNull()) |history| {
if (self.directories.history.pop()) |history| {
self.directories.entries.selected = history.selected;
self.directories.entries.offset = history.offset;
}
Expand All @@ -208,7 +202,7 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !
if (self.directories.dir.openDir(entry.name, .{ .iterate = true })) |dir| {
self.directories.dir = dir;

try self.directories.history.append(.{
self.directories.history.push(.{
.selected = self.directories.entries.selected,
.offset = self.directories.entries.offset,
});
Expand Down Expand Up @@ -274,7 +268,10 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !

try self.notification.write("Deleting item...", .info);
if (self.directories.dir.rename(entry.name, tmp_path)) {
try self.actions.append(.{ .delete = .{ .old_path = old_path, .tmp_path = tmp_path } });
// TODO: Will leak memory if pushing to a full stack.
self.actions.push(.{
.delete = .{ .old = old_path, .new = tmp_path },
});
try self.notification.write("Deleted item.", .info);

self.directories.remove_selected();
Expand All @@ -289,16 +286,15 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !
self.state = .new_file;
},
'u' => {
if (self.actions.items.len > 0) {
if (self.actions.pop()) |action| {
const selected = self.directories.entries.selected;

const action = self.actions.pop();
switch (action) {
.delete => |a| {
// TODO: Will overwrite an item if it has the same name.
if (self.directories.dir.rename(a.tmp_path, a.old_path)) {
defer self.alloc.free(a.tmp_path);
defer self.alloc.free(a.old_path);
if (self.directories.dir.rename(a.new, a.old)) {
defer self.alloc.free(a.new);
defer self.alloc.free(a.old);

self.directories.cleanup();
const fuzzy = self.inputToSlice();
Expand All @@ -315,9 +311,9 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !
},
.rename => |a| {
// TODO: Will overwrite an item if it has the same name.
if (self.directories.dir.rename(a.new_path, a.old_path)) {
defer self.alloc.free(a.new_path);
defer self.alloc.free(a.old_path);
if (self.directories.dir.rename(a.new, a.old)) {
defer self.alloc.free(a.new);
defer self.alloc.free(a.old);

self.directories.cleanup();
const fuzzy = self.inputToSlice();
Expand All @@ -340,19 +336,19 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !
}
},
'/' => {
self.state = State.fuzzy;
self.state = .fuzzy;
},
'R' => {
self.state = State.rename;
self.state = .rename;

const entry = try self.directories.get_selected();
self.text_input.insertSliceAtCursor(entry.name) catch {
self.state = State.normal;
self.state = .normal;
try self.notification.write_err(.UnableToRename);
};
},
'c' => {
self.state = State.change_dir;
self.state = .change_dir;
},
else => {
// log.debug("codepoint: {d}\n", .{key.codepoint});
Expand All @@ -367,7 +363,7 @@ pub fn handle_normal_event(self: *App, event: Event, loop: *vaxis.Loop(Event)) !
return .default;
}

pub fn handle_input_event(self: *App, event: Event) !Effect {
pub fn handle_input_event(self: *App, event: Event) !InputReturnStatus {
switch (event) {
.key_press => |key| {
if ((key.codepoint == 'c' and key.mods.ctrl) or key.codepoint == 'q') {
Expand All @@ -390,11 +386,9 @@ pub fn handle_input_event(self: *App, event: Event) !Effect {
}

self.text_input.clearAndFree();
self.state = State.normal;
self.state = .normal;
},
Key.enter => {
// TODO: Do these actions really have to re-populate or can we
// just append.
const selected = self.directories.entries.selected;
switch (self.state) {
.new_dir => {
Expand Down Expand Up @@ -454,10 +448,13 @@ pub fn handle_input_event(self: *App, event: Event) !Effect {
error.PathAlreadyExists => try self.notification.write_err(.ItemAlreadyExists),
else => try self.notification.write_err(.UnknownError),
};
try self.actions.append(.{ .rename = .{
.old_path = try std.fs.path.join(self.alloc, &.{ dir_prefix, old.name }),
.new_path = try std.fs.path.join(self.alloc, &.{ dir_prefix, new }),
} });
// TODO: Will leak memory if pushing to a full stack.
self.actions.push(.{
.rename = .{
.old = try std.fs.path.join(self.alloc, &.{ dir_prefix, old.name }),
.new = try std.fs.path.join(self.alloc, &.{ dir_prefix, new }),
},
});

self.directories.cleanup();
self.directories.populate_entries("") catch |err| {
Expand All @@ -481,7 +478,7 @@ pub fn handle_input_event(self: *App, event: Event) !Effect {
else => try self.notification.write_err(.UnknownError),
}
};
self.directories.history.clearAndFree();
self.directories.history.reset();
} else |err| {
switch (err) {
error.AccessDenied => try self.notification.write_err(.PermissionDenied),
Expand All @@ -495,7 +492,7 @@ pub fn handle_input_event(self: *App, event: Event) !Effect {
},
else => {},
}
self.state = State.normal;
self.state = .normal;
self.directories.entries.selected = selected;
},
else => {
Expand Down Expand Up @@ -573,7 +570,7 @@ fn draw_preview(self: *App, win: vaxis.Window, file_name_win: vaxis.Window) !voi
});

// Populate preview bar
if (self.directories.entries.all().len > 0 and config.preview_file == true) {
if (self.directories.entries.len() > 0 and config.preview_file == true) {
const entry = try self.directories.get_selected();

@memcpy(&self.last_item_path_buf, &self.current_item_path_buf);
Expand Down Expand Up @@ -662,13 +659,13 @@ fn draw_preview(self: *App, win: vaxis.Window, file_name_win: vaxis.Window) !voi
break :file;
}

if (self.directories.pdf_contents.len > 0) {
self.directories.alloc.free(self.directories.pdf_contents);
if (self.directories.pdf_contents) |pdf_contents| {
self.directories.alloc.free(pdf_contents);
}
self.directories.pdf_contents = try stdout.toOwnedSlice();
_ = try preview_win.print(&.{
.{
.text = self.directories.pdf_contents,
.text = self.directories.pdf_contents.?,
},
}, .{});
break :file;
Expand Down Expand Up @@ -701,7 +698,7 @@ fn draw_preview(self: *App, win: vaxis.Window, file_name_win: vaxis.Window) !voi
fn draw_file_info(self: *App, win: vaxis.Window, file_info_buf: []u8) !vaxis.Window {
const file_info = try std.fmt.bufPrint(file_info_buf, "{d}/{d} {s} {s}", .{
self.directories.entries.selected + 1,
self.directories.entries.items.items.len,
self.directories.entries.len(),
std.fs.path.extension(if (self.directories.get_selected()) |entry| entry.name else |_| ""),
std.fmt.fmtIntSizeDec((try self.directories.dir.metadata()).size()),
});
Expand All @@ -725,7 +722,7 @@ fn draw_current_dir_list(self: *App, win: vaxis.Window, abs_file_path: vaxis.Win
.width = if (config.preview_file) .{ .limit = win.width / 2 } else .{ .limit = win.width },
.height = .{ .limit = win.height - (abs_file_path.height + file_information.height + top_div + bottom_div) },
});
try self.directories.write_entries(current_dir_list_win, config.styles.selected_list_item, config.styles.list_item, null);
try self.directories.write_entries(current_dir_list_win, config.styles.selected_list_item, config.styles.list_item);

self.last_known_height = current_dir_list_win.height;
}
Expand Down
35 changes: 35 additions & 0 deletions src/circ_stack.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const std = @import("std");

pub fn CircularStack(comptime T: type, comptime capacity: usize) type {
return struct {
const Self = @This();

head: usize = 0,
count: usize = 0,
buf: [capacity]T = undefined,

pub fn init() Self {
return Self{};
}

pub fn reset(self: *Self) void {
self.head = 0;
self.count = 0;
}

pub fn push(self: *Self, v: T) void {
self.buf[self.head] = v;
self.head = (self.head + 1) % capacity;
if (self.count != capacity) self.count += 1;
}

pub fn pop(self: *Self) ?T {
if (self.count == 0) return null;

self.head = (self.head - 1) % capacity;
const value = self.buf[self.head];
self.count -= 1;
return value;
}
};
}
Loading

0 comments on commit 6c09139

Please sign in to comment.