Skip to content

Commit bcffe3f

Browse files
committed
added output coloring and help meta command
1 parent 7319f5f commit bcffe3f

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

pkg/aicli/cmd.go

+54-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ const (
1717
maxFileSize = 10000
1818
)
1919

20+
// ANSI color codes
21+
const (
22+
colorReset = "\033[0m"
23+
colorCyan = "\033[36m" // for user input
24+
colorYellow = "\033[33m" // for AI responses
25+
colorRed = "\033[31m" // for errors
26+
colorBlue = "\033[34m" // for system messages
27+
)
28+
2029
type Cmd struct {
2130
AI string `help:"Name of service"`
2231
Model string `help:"Name of model to talk to. Most services have multiple options."`
@@ -79,7 +88,7 @@ func (cmd *Cmd) Run() error {
7988
HistoryLimit: 1000000,
8089

8190
Stdin: cmd.stdin,
82-
Stdout: cmd.stdout,
91+
Stdout: &colorWriter{w: cmd.stdout, color: colorCyan}, // Color the user input
8392
Stderr: cmd.stderr,
8493
})
8594
if err != nil {
@@ -129,7 +138,7 @@ func (cmd *Cmd) sendMessages() error {
129138
Temperature: cmd.Temperature,
130139
Messages: cmd.messagesWithinLimit(),
131140
}
132-
msg, err := cmd.client().GenerateStream(req, cmd.stdout)
141+
msg, err := cmd.client().GenerateStream(req, &colorWriter{w: cmd.stdout, color: colorYellow})
133142
if err != nil {
134143
return err
135144
}
@@ -231,6 +240,19 @@ func (cmd *Cmd) handleMeta(line string) string {
231240
}
232241
case `\quit`, `\exit`, `\q`:
233242
os.Exit(0)
243+
case `\help`, `\?`:
244+
cmd.sysOut("Available commands:")
245+
cmd.sysOut(" \\help, \\? Show this help message")
246+
cmd.sysOut(" \\reset Reset conversation (keeps system message)")
247+
cmd.sysOut(" \\reset-system Remove system message")
248+
cmd.sysOut(" \\messages Show all messages in the conversation")
249+
cmd.sysOut(" \\config Show current configuration")
250+
cmd.sysOut(" \\set Set a configuration parameter (usage: \\set <param> <value>)")
251+
cmd.sysOut(" \\file Add file contents to conversation")
252+
cmd.sysOut(" \\system Set or show system message")
253+
cmd.sysOut(" \\context Set context limit in bytes")
254+
cmd.sysOut(" \\<< Start multiline input (optional custom terminator)")
255+
cmd.sysOut(" \\quit, \\exit, \\q Exit the program")
234256
default:
235257
err = errors.Errorf("Unknown meta command '%s'", line)
236258
}
@@ -326,7 +348,11 @@ func (cmd *Cmd) appendMessage(msg Message) {
326348

327349
func (cmd *Cmd) printMessages() {
328350
for _, msg := range cmd.messages {
329-
cmd.out("%9s: %s", msg.Role(), msg.Content())
351+
if msg.Role() == "user" {
352+
cmd.out(colorCyan+"%9s: %s"+colorReset, msg.Role(), msg.Content())
353+
} else {
354+
cmd.out(colorYellow+"%9s: %s"+colorReset, msg.Role(), msg.Content())
355+
}
330356
}
331357
}
332358

@@ -340,13 +366,17 @@ func (cmd *Cmd) out(format string, a ...any) {
340366
fmt.Fprintf(cmd.stdout, format+"\n", a...)
341367
}
342368

369+
func (cmd *Cmd) sysOut(format string, a ...any) {
370+
fmt.Fprintf(cmd.stdout, colorBlue+format+colorReset+"\n", a...)
371+
}
372+
343373
func (cmd *Cmd) err(err error) {
344-
fmt.Fprintf(cmd.stderr, err.Error()+"\n")
374+
fmt.Fprintf(cmd.stderr, colorRed+"%s"+colorReset+"\n", err.Error())
345375
}
346376

347377
// errOut wraps the error and writes it to the user on stderr.
348378
func (cmd *Cmd) errOut(err error, format string, a ...any) {
349-
fmt.Fprintf(cmd.stderr, "%s: %v", fmt.Sprintf(format, a...), err.Error())
379+
fmt.Fprintf(cmd.stderr, colorRed+"%s: %v"+colorReset+"\n", fmt.Sprintf(format, a...), err.Error())
350380
}
351381

352382
// checkConfig ensures the command configuration is valid before proceeding.
@@ -413,3 +443,22 @@ func (cmd *Cmd) readConfigFile() error {
413443
}
414444
return nil
415445
}
446+
447+
// colorWriter wraps an io.Writer and adds color codes
448+
type colorWriter struct {
449+
w io.Writer
450+
color string
451+
}
452+
453+
func (cw *colorWriter) Write(p []byte) (n int, err error) {
454+
// Write the color code, then the content, then reset
455+
if _, err := cw.w.Write([]byte(cw.color)); err != nil {
456+
return 0, err
457+
}
458+
n, err = cw.w.Write(p)
459+
if err != nil {
460+
return n, err
461+
}
462+
_, err = cw.w.Write([]byte(colorReset))
463+
return n, err
464+
}

0 commit comments

Comments
 (0)