diff options
| author | 2022-01-02 09:29:45 +0200 | |
|---|---|---|
| committer | 2022-01-02 09:29:45 +0200 | |
| commit | 0bde08a141b8de9b9d9354b532a84333e02724dc (patch) | |
| tree | f9c8912766eb62e1a97dc41c91cb9bbb0b8eff9d /src/Editor.zig | |
| parent | Support ~/paths (diff) | |
| download | es-0bde08a141b8de9b9d9354b532a84333e02724dc.tar.gz es-0bde08a141b8de9b9d9354b532a84333e02724dc.tar.xz es-0bde08a141b8de9b9d9354b532a84333e02724dc.zip | |
Added Allocator to Editor.prompt & Editor.promptEx
Diffstat (limited to 'src/Editor.zig')
| -rw-r--r-- | src/Editor.zig | 15 |
1 files changed, 8 insertions, 7 deletions
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 | ||
| 190 | pub fn openFile(self: *Editor) !void { | 190 | pub 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 | ||
| 203 | pub fn prompt(self: *Editor, prompt_str: []const u8) !?[]u8 { | 203 | pub 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 | ||
| 207 | pub fn promptEx( | 207 | pub 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 | ||
| 362 | pub fn switchBuffer(self: *Editor) !void { | 363 | pub 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 | ||