summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2022-01-02 09:29:45 +0200
committerGravatar Uko Kokņevičs2022-01-02 09:29:45 +0200
commit0bde08a141b8de9b9d9354b532a84333e02724dc (patch)
treef9c8912766eb62e1a97dc41c91cb9bbb0b8eff9d
parentSupport ~/paths (diff)
downloades-0bde08a141b8de9b9d9354b532a84333e02724dc.tar.gz
es-0bde08a141b8de9b9d9354b532a84333e02724dc.tar.xz
es-0bde08a141b8de9b9d9354b532a84333e02724dc.zip
Added Allocator to Editor.prompt & Editor.promptEx
-rw-r--r--src/Buffer.zig7
-rw-r--r--src/Editor.zig15
-rw-r--r--src/search.zig9
3 files changed, 19 insertions, 12 deletions
diff --git a/src/Buffer.zig b/src/Buffer.zig
index 7f3043b..5bf319c 100644
--- a/src/Buffer.zig
+++ b/src/Buffer.zig
@@ -274,8 +274,8 @@ pub fn forwardChar(self: *Buffer) void {
274 274
275// TODO: Make use of the callback feature to go to the final line while typing in. 275// TODO: Make use of the callback feature to go to the final line while typing in.
276pub fn goToLine(self: *Buffer, editor: *Editor) !void { 276pub fn goToLine(self: *Buffer, editor: *Editor) !void {
277 if (try editor.prompt("Goto line")) |line_str| { 277 if (try editor.prompt(self.allocator, "Goto line")) |line_str| {
278 defer editor.allocator.free(line_str); 278 defer self.allocator.free(line_str);
279 279
280 const line = std.fmt.parseUnsigned(usize, line_str, 0) catch |err| { 280 const line = std.fmt.parseUnsigned(usize, line_str, 0) catch |err| {
281 try editor.setStatusMessage("Couldn't parse '{s}' as an integer: {}", .{ line_str, err }); 281 try editor.setStatusMessage("Couldn't parse '{s}' as an integer: {}", .{ line_str, err });
@@ -434,8 +434,7 @@ pub fn recenterTopBottom(self: *Buffer, screenrows: usize) void {
434 434
435pub fn save(self: *Buffer, editor: *Editor) !void { 435pub fn save(self: *Buffer, editor: *Editor) !void {
436 if (self.file_path == null) { 436 if (self.file_path == null) {
437 // TODO: Editor.prompt should take an Allocator 437 const fname = (try editor.prompt(self.allocator, "Save as")) orelse { return; };
438 const fname = (try editor.prompt("Save as")) orelse { return; };
439 defer self.allocator.free(fname); 438 defer self.allocator.free(fname);
440 439
441 const file_path = try es.files.resolvePath(self.allocator, fname); 440 const file_path = try es.files.resolvePath(self.allocator, fname);
diff --git a/src/Editor.zig b/src/Editor.zig
index 469fc81..d721600 100644
--- a/src/Editor.zig
+++ b/src/Editor.zig
@@ -188,7 +188,7 @@ pub fn open(self: *Editor, name: []const u8) !void {
188} 188}
189 189
190pub fn openFile(self: *Editor) !void { 190pub fn openFile(self: *Editor) !void {
191 const fname_opt = try self.prompt("File name"); 191 const fname_opt = try self.prompt(self.allocator, "File name");
192 if (fname_opt) |fname| { 192 if (fname_opt) |fname| {
193 defer self.allocator.free(fname); 193 defer self.allocator.free(fname);
194 return self.open(fname); 194 return self.open(fname);
@@ -200,19 +200,20 @@ pub fn processKeypress(self: *Editor) !void {
200 try self.current_state(self, self.buffer, key); 200 try self.current_state(self, self.buffer, key);
201} 201}
202 202
203pub fn prompt(self: *Editor, prompt_str: []const u8) !?[]u8 { 203pub fn prompt(self: *Editor, allocator: Allocator, prompt_str: []const u8) !?[]u8 {
204 return self.promptEx(void, error{}, prompt_str, null, {}); 204 return self.promptEx(void, error{}, allocator, prompt_str, null, {});
205} 205}
206 206
207pub fn promptEx( 207pub fn promptEx(
208 self: *Editor, 208 self: *Editor,
209 comptime CallbackData: type, 209 comptime CallbackData: type,
210 comptime CallbackError: type, 210 comptime CallbackError: type,
211 allocator: Allocator,
211 prompt_str: []const u8, 212 prompt_str: []const u8,
212 callback: ?PromptCallback(CallbackData, CallbackError), 213 callback: ?PromptCallback(CallbackData, CallbackError),
213 cb_data: CallbackData, 214 cb_data: CallbackData,
214) !?[]u8 { 215) !?[]u8 {
215 var buf = ArrayList(u8).init(self.allocator); 216 var buf = ArrayList(u8).init(allocator);
216 defer buf.deinit(); 217 defer buf.deinit();
217 218
218 while (true) { 219 while (true) {
@@ -262,7 +263,7 @@ pub fn promptYN(self: *Editor, prompt_str: []const u8) !bool {
262 const full_prompt = try std.fmt.allocPrint(self.allocator, "{s} (Y/N)", .{prompt_str}); 263 const full_prompt = try std.fmt.allocPrint(self.allocator, "{s} (Y/N)", .{prompt_str});
263 defer self.allocator.free(full_prompt); 264 defer self.allocator.free(full_prompt);
264 265
265 var response = try self.prompt(full_prompt); 266 var response = try self.prompt(self.allocator, full_prompt);
266 defer if (response) |str| self.allocator.free(str); 267 defer if (response) |str| self.allocator.free(str);
267 // TODO: This can be improved 268 // TODO: This can be improved
268 while (response != null 269 while (response != null
@@ -275,7 +276,7 @@ pub fn promptYN(self: *Editor, prompt_str: []const u8) !bool {
275 ) 276 )
276 ) { 277 ) {
277 if (response) |str| self.allocator.free(str); 278 if (response) |str| self.allocator.free(str);
278 response = try self.prompt(full_prompt); 279 response = try self.prompt(self.allocator, full_prompt);
279 } 280 }
280 281
281 return response != null and (response.?[0] == 'y' or response.?[0] == 'Y'); 282 return response != null and (response.?[0] == 'y' or response.?[0] == 'Y');
@@ -361,7 +362,7 @@ pub fn setStatusMessage(self: *Editor, comptime fmt: []const u8, args: anytype)
361 362
362pub fn switchBuffer(self: *Editor) !void { 363pub fn switchBuffer(self: *Editor) !void {
363 // TODO: completion 364 // TODO: completion
364 const bufname_opt = try self.prompt("Switch to buffer"); 365 const bufname_opt = try self.prompt(self.allocator, "Switch to buffer");
365 if (bufname_opt) |bufname| { 366 if (bufname_opt) |bufname| {
366 defer self.allocator.free(bufname); 367 defer self.allocator.free(bufname);
367 368
diff --git a/src/search.zig b/src/search.zig
index 321f948..6c7dc6a 100644
--- a/src/search.zig
+++ b/src/search.zig
@@ -37,7 +37,14 @@ pub fn search(editor: *Editor, buffer: *Buffer) !void {
37 data.allocator.free(saved_hl.data); 37 data.allocator.free(saved_hl.data);
38 }; 38 };
39 39
40 if (try editor.promptEx(*CallbackData, Error, "Search", searchCallback, &data)) |response| { 40 if (try editor.promptEx(
41 *CallbackData,
42 Error,
43 editor.allocator,
44 "Search",
45 searchCallback,
46 &data,
47 )) |response| {
41 editor.allocator.free(response); 48 editor.allocator.free(response);
42 } else { 49 } else {
43 // Cancelled 50 // Cancelled