Prove multi-file terminal coding loop
This commit is contained in:
+19
-3
@@ -163,6 +163,7 @@ fn openLocalEditor(
|
||||
switch (metadata.kind) {
|
||||
.directory => try openDirectoryPreview(allocator, io, &client, path),
|
||||
.file => {
|
||||
if (std.fs.path.dirname(path)) |parent| seedDirectoryRepo(allocator, io, &client, parent) catch {};
|
||||
const bytes = std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .limited(diagnostics.max_file_bytes)) catch |err| switch (err) {
|
||||
error.StreamTooLong => {
|
||||
try client.handleTraceLine("open diagnostic:file_too_large");
|
||||
@@ -184,7 +185,7 @@ fn openLocalEditor(
|
||||
try runLocalEditor(allocator, io, stdout, stderr, &client, path, stat != null and stat.?.kind == .directory);
|
||||
}
|
||||
|
||||
fn openDirectoryPreview(allocator: std.mem.Allocator, io: std.Io, client: *tui.Client, path: []const u8) !void {
|
||||
fn seedDirectoryRepo(allocator: std.mem.Allocator, io: std.Io, client: *tui.Client, path: []const u8) !void {
|
||||
var dir = try std.Io.Dir.cwd().openDir(io, path, .{ .iterate = true });
|
||||
defer dir.close(io);
|
||||
|
||||
@@ -211,6 +212,10 @@ fn openDirectoryPreview(allocator: std.mem.Allocator, io: std.Io, client: *tui.C
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn openDirectoryPreview(allocator: std.mem.Allocator, io: std.Io, client: *tui.Client, path: []const u8) !void {
|
||||
try seedDirectoryRepo(allocator, io, client, path);
|
||||
try client.handleTraceLine("file_picker");
|
||||
}
|
||||
|
||||
@@ -236,6 +241,8 @@ fn runLocalEditor(
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_dir) try client.openFilePicker();
|
||||
|
||||
var raw_terminal = try RawTerminal.enable(stdin_file.handle);
|
||||
defer raw_terminal.restore();
|
||||
|
||||
@@ -259,10 +266,12 @@ fn runLocalEditor(
|
||||
if (client.saved()) |saved_bytes| {
|
||||
const new_save_request = last_client_saved == null or !std.mem.eql(u8, saved_bytes, last_client_saved.?);
|
||||
if (new_save_request) {
|
||||
if (is_dir) {
|
||||
if (is_dir and client.currentPath() == null) {
|
||||
client.setStatusMessage("directory browser has nothing to save");
|
||||
} else {
|
||||
try persistLocalSave(allocator, io, path, saved_bytes, &last_saved, &last_client_saved);
|
||||
const save_path = try resolveLocalSavePath(allocator, path, client.currentPath());
|
||||
defer allocator.free(save_path);
|
||||
try persistLocalSave(allocator, io, save_path, saved_bytes, &last_saved, &last_client_saved);
|
||||
dirty = false;
|
||||
client.setStatusMessage("saved");
|
||||
}
|
||||
@@ -280,6 +289,13 @@ fn runLocalEditor(
|
||||
}
|
||||
}
|
||||
|
||||
fn resolveLocalSavePath(allocator: std.mem.Allocator, launch_path: []const u8, current_path: ?[]const u8) ![]u8 {
|
||||
const selected = current_path orelse launch_path;
|
||||
if (std.fs.path.isAbsolute(selected) or std.mem.indexOfScalar(u8, selected, '/') != null) return allocator.dupe(u8, selected);
|
||||
const base = std.fs.path.dirname(launch_path) orelse ".";
|
||||
return std.fs.path.join(allocator, &.{ base, selected });
|
||||
}
|
||||
|
||||
fn persistLocalSave(
|
||||
allocator: std.mem.Allocator,
|
||||
io: std.Io,
|
||||
|
||||
Reference in New Issue
Block a user