79113
Ma
mini-coding-agent / files
mini-coding-agent / src / tui / commands.zig
2.0 KB
1const std = @import("std"); 2const platform = @import("../platform.zig"); 3const complete = @import("complete.zig"); 4const styles = @import("styles.zig"); 5 6pub const Command = enum { 7 help, 8 new, 9 provider, 10 model, 11 thinking, 12 13 pub fn wire(self: Command) []const u8 { 14 return @tagName(self); 15 } 16 17 pub fn summary(self: Command) []const u8 { 18 return switch (self) { 19 .help => "list commands and keybindings", 20 .new => "start a new session", 21 .provider => "choose the provider and model", 22 .model => "choose a model for the current provider", 23 .thinking => "set the thinking level", 24 }; 25 } 26 27 pub const all = [_]Command{ .help, .new, .provider, .model, .thinking }; 28}; 29 30pub fn findCommand(text: []const u8) ?Command { 31 if (text.len == 0 or text[0] != '/') return null; 32 var i: usize = 1; 33 while (i < text.len and text[i] != ' ' and text[i] != '\t' and text[i] != '\n') i += 1; 34 return std.meta.stringToEnum(Command, text[1..i]); 35} 36 37pub fn completeCommand(draft: []const u8) ?[]const u8 { 38 if (draft.len == 0 or draft[0] != '/') return null; 39 if (std.mem.indexOfAny(u8, draft, " \t\n") != null) return null; 40 const typed = draft[1..]; 41 var matched: ?[]const u8 = null; 42 for (Command.all) |c| { 43 if (!std.mem.startsWith(u8, c.wire(), typed)) continue; 44 matched = if (matched) |have| complete.commonPrefix(have, c.wire()) else c.wire(); 45 } 46 const name = matched orelse return null; 47 if (name.len <= typed.len) return null; 48 return std.fmt.allocPrint(platform.gpa, "/{s}", .{name}) catch null; 49} 50 51pub fn helpRow(a: std.mem.Allocator, key: []const u8, description: []const u8, width: usize) []const u8 { 52 var padded: std.ArrayList(u8) = .empty; 53 padded.appendSlice(a, key) catch {}; 54 var i = key.len; 55 while (i < width) : (i += 1) padded.append(a, ' ') catch {}; 56 return std.fmt.allocPrint(a, " {s} {s}", .{ styles.comment(a, padded.items), description }) catch key; 57}