fix: load help.md from memory, not disk

This commit is contained in:
CJ van den Berg 2024-02-29 15:55:21 +01:00
parent 5c72e1dfe4
commit baf24d5504
2 changed files with 10 additions and 2 deletions

View file

@ -996,6 +996,13 @@ pub fn load_from_string(self: *const Self, s: []const u8) !Root {
return self.load(stream.reader(), s.len); return self.load(stream.reader(), s.len);
} }
pub fn load_from_string_and_update(self: *Self, file_path: []const u8, s: []const u8) !void {
self.root = try self.load_from_string(s);
self.file_path = try self.a.dupe(u8, file_path);
self.last_save = self.root;
self.file_exists = false;
}
pub fn load_from_file(self: *const Self, file_path: []const u8, file_exists: *bool) !Root { pub fn load_from_file(self: *const Self, file_path: []const u8, file_exists: *bool) !Root {
const file = cwd().openFile(file_path, .{ .mode = .read_only }) catch |e| switch (e) { const file = cwd().openFile(file_path, .{ .mode = .read_only }) catch |e| switch (e) {
error.FileNotFound => return self.new_file(file_exists), error.FileNotFound => return self.new_file(file_exists),

View file

@ -340,7 +340,8 @@ pub const Editor = struct {
fn open_scratch(self: *Self, file_path: []const u8, content: []const u8) !void { fn open_scratch(self: *Self, file_path: []const u8, content: []const u8) !void {
var new_buf = try Buffer.create(self.a); var new_buf = try Buffer.create(self.a);
errdefer new_buf.deinit(); errdefer new_buf.deinit();
try new_buf.load_from_string(content); try new_buf.load_from_string_and_update(file_path, content);
new_buf.file_exists = true;
return self.open_buffer(file_path, new_buf); return self.open_buffer(file_path, new_buf);
} }
@ -2603,7 +2604,7 @@ pub const Editor = struct {
var file_path: []const u8 = undefined; var file_path: []const u8 = undefined;
var content: []const u8 = undefined; var content: []const u8 = undefined;
if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) { if (ctx.args.match(.{ tp.extract(&file_path), tp.extract(&content) }) catch false) {
self.open(file_path) catch |e| return tp.exit_error(e); self.open_scratch(file_path, content) catch |e| return tp.exit_error(e);
self.clamp(); self.clamp();
} else return tp.exit_error(error.InvalidArgument); } else return tp.exit_error(error.InvalidArgument);
} }