10 tea "github.com/charmbracelet/bubbletea/v2"
11 "github.com/charmbracelet/crush/internal/app"
12 "github.com/charmbracelet/crush/internal/config"
13 "github.com/charmbracelet/crush/internal/db"
14 "github.com/charmbracelet/crush/internal/tui"
15 "github.com/charmbracelet/crush/internal/version"
16 "github.com/charmbracelet/fang"
17 "github.com/charmbracelet/x/term"
18 "github.com/spf13/cobra"
21var rootCmd = &cobra.Command{
23 Short: "Terminal-based AI assistant for software development",
24 Long: `Crush is a powerful terminal-based AI assistant that helps with software development tasks.
25It provides an interactive chat interface with AI capabilities, code analysis, and LSP integration
26to assist developers in writing, debugging, and understanding code directly from the terminal.`,
28 # Run in interactive mode
31 # Run with debug logging
34 # Run with debug slog.in a specific directory
35 crush -d -c /path/to/project
40 # Run a single non-interactive prompt
41 crush -p "Explain the use of context in Go"
43 # Run a single non-interactive prompt with JSON output format
44 crush -p "Explain the use of context in Go" -f json
46 # Run in dangerous mode (auto-accept all permissions)
49 RunE: func(cmd *cobra.Command, args []string) error {
51 debug, _ := cmd.Flags().GetBool("debug")
52 cwd, _ := cmd.Flags().GetString("cwd")
53 prompt, _ := cmd.Flags().GetString("prompt")
54 quiet, _ := cmd.Flags().GetBool("quiet")
55 yolo, _ := cmd.Flags().GetBool("yolo")
60 return fmt.Errorf("failed to change directory: %v", err)
66 return fmt.Errorf("failed to get current working directory: %v", err)
71 cfg, err := config.Init(cwd, debug)
75 cfg.Options.SkipPermissionsRequests = yolo
79 // Connect DB, this will also run migrations
80 conn, err := db.Connect(ctx, cfg.Options.DataDirectory)
85 app, err := app.New(ctx, conn, cfg)
87 slog.Error(fmt.Sprintf("Failed to create app instance: %v", err))
92 prompt, err = maybePrependStdin(prompt)
94 slog.Error(fmt.Sprintf("Failed to read from stdin: %v", err))
98 // Non-interactive mode
100 // Run non-interactive flow using the App method
101 return app.RunNonInteractive(ctx, prompt, quiet)
105 program := tea.NewProgram(
108 tea.WithContext(ctx),
111 go app.Subscribe(program)
113 if _, err := program.Run(); err != nil {
114 slog.Error(fmt.Sprintf("TUI run error: %v", err))
115 return fmt.Errorf("TUI error: %v", err)
122 if err := fang.Execute(
123 context.Background(),
125 fang.WithVersion(version.Version),
126 fang.WithNotifySignal(os.Interrupt),
133 rootCmd.PersistentFlags().StringP("cwd", "c", "", "Current working directory")
135 rootCmd.Flags().BoolP("help", "h", false, "Help")
136 rootCmd.Flags().BoolP("debug", "d", false, "Debug")
137 rootCmd.Flags().StringP("prompt", "p", "", "Prompt to run in non-interactive mode")
138 rootCmd.Flags().BoolP("yolo", "y", false, "Automatically accept all permissions (dangerous mode)")
140 // Add quiet flag to hide spinner in non-interactive mode
141 rootCmd.Flags().BoolP("quiet", "q", false, "Hide spinner in non-interactive mode")
144func maybePrependStdin(prompt string) (string, error) {
145 if term.IsTerminal(os.Stdin.Fd()) {
148 fi, err := os.Stdin.Stat()
152 if fi.Mode()&os.ModeNamedPipe == 0 {
155 bts, err := io.ReadAll(os.Stdin)
159 return string(bts) + "\n\n" + prompt, nil
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list