-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (89 loc) · 2.1 KB
/
main.go
File metadata and controls
103 lines (89 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)
func spinner() *time.Ticker {
ticker := time.NewTicker(400 * time.Millisecond)
fmt.Fprintf(os.Stderr, "thinking")
go func() {
frames := []string{"thinking ", "thinking. ", "thinking.. ", "thinking..."}
i := 1
for range ticker.C {
fmt.Fprintf(os.Stderr, "\r%s", frames[i%len(frames)])
i++
}
}()
return ticker
}
func clearSpinner() {
fmt.Fprint(os.Stderr, "\r \r")
}
const systemPrompt = `You are a terse CLI assistant. Output plain text only, no markdown formatting. Be concise and direct. Give the shortest useful answer.
Question: `
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Usage: how <question>")
os.Exit(1)
}
claudePath, err := exec.LookPath("claude")
if err != nil {
fmt.Fprintln(os.Stderr, "Error: claude-code not installed. Install from: https://docs.anthropic.com/claude-code")
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
checkCmd := exec.CommandContext(ctx, claudePath, "--version")
if err := checkCmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, "Error: run 'claude' first to authenticate")
os.Exit(1)
}
query := systemPrompt + "how " + strings.Join(os.Args[1:], " ")
cmd := exec.Command(claudePath, "--print", query)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
cmd.Stderr = os.Stderr
t := spinner()
if err := cmd.Start(); err != nil {
t.Stop()
clearSpinner()
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
reader := bufio.NewReader(stdout)
firstByte := true
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if n > 0 {
if firstByte {
t.Stop()
clearSpinner()
firstByte = false
}
os.Stdout.Write(buf[:n])
}
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading output: %v\n", err)
break
}
}
if err := cmd.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
os.Exit(exitErr.ExitCode())
}
os.Exit(1)
}
}