9 "charm.land/bubbles/v2/help"
10 "charm.land/bubbles/v2/key"
11 tea "charm.land/bubbletea/v2"
12 "charm.land/lipgloss/v2"
13 "github.com/charmbracelet/catwalk/pkg/catwalk"
15 "github.com/charmbracelet/crush/internal/agent"
16 "github.com/charmbracelet/crush/internal/agent/tools/mcp"
17 "github.com/charmbracelet/crush/internal/config"
18 "github.com/charmbracelet/crush/internal/csync"
19 "github.com/charmbracelet/crush/internal/pubsub"
20 "github.com/charmbracelet/crush/internal/tui/components/chat"
21 "github.com/charmbracelet/crush/internal/tui/components/core"
22 "github.com/charmbracelet/crush/internal/tui/components/dialogs"
23 "github.com/charmbracelet/crush/internal/tui/exp/list"
24 "github.com/charmbracelet/crush/internal/tui/styles"
25 "github.com/charmbracelet/crush/internal/tui/util"
29 CommandsDialogID dialogs.DialogID = "commands"
36func (c commandType) String() string { return []string{"System", "User", "MCP"}[c] }
39 SystemCommands commandType = iota
44type listModel = list.FilterableList[list.CompletionItem[Command]]
46// Command represents a command that can be executed
51 Shortcut string // Optional shortcut for the command
52 Handler func(cmd Command) tea.Cmd
55// CommandsDialog represents the commands dialog.
56type CommandsDialog interface {
60type commandDialogCmp struct {
62 wWidth int // Width of the terminal window
63 wHeight int // Height of the terminal window
66 keyMap CommandsDialogKeyMap
68 selected commandType // Selected SystemCommands, UserCommands, or MCPPrompts
69 userCommands []Command // User-defined commands
70 mcpPrompts *csync.Slice[Command] // MCP prompts
71 sessionID string // Current session ID
75 SwitchSessionsMsg struct{}
76 NewSessionsMsg struct{}
77 SwitchModelMsg struct{}
79 OpenFilePickerMsg struct{}
80 ToggleHelpMsg struct{}
81 ToggleCompactModeMsg struct{}
82 ToggleThinkingMsg struct{}
83 OpenReasoningDialogMsg struct{}
84 OpenExternalEditorMsg struct{}
85 ToggleYoloModeMsg struct{}
91func NewCommandDialog(sessionID string) CommandsDialog {
92 keyMap := DefaultCommandsDialogKeyMap()
93 listKeyMap := list.DefaultKeyMap()
94 listKeyMap.Down.SetEnabled(false)
95 listKeyMap.Up.SetEnabled(false)
96 listKeyMap.DownOneItem = keyMap.Next
97 listKeyMap.UpOneItem = keyMap.Previous
99 t := styles.CurrentTheme()
100 inputStyle := t.S().Base.PaddingLeft(1).PaddingBottom(1)
101 commandList := list.NewFilterableList(
102 []list.CompletionItem[Command]{},
103 list.WithFilterInputStyle(inputStyle),
104 list.WithFilterListOptions(
105 list.WithKeyMap(listKeyMap),
106 list.WithWrapNavigation(),
107 list.WithResizeByList(),
111 help.Styles = t.S().Help
112 return &commandDialogCmp{
113 commandList: commandList,
115 keyMap: DefaultCommandsDialogKeyMap(),
117 selected: SystemCommands,
118 sessionID: sessionID,
119 mcpPrompts: csync.NewSlice[Command](),
123func (c *commandDialogCmp) Init() tea.Cmd {
124 commands, err := LoadCustomCommands()
126 return util.ReportError(err)
128 c.userCommands = commands
129 c.mcpPrompts.SetSlice(loadMCPPrompts())
130 return c.setCommandType(c.selected)
133func (c *commandDialogCmp) Update(msg tea.Msg) (util.Model, tea.Cmd) {
134 switch msg := msg.(type) {
135 case tea.WindowSizeMsg:
137 c.wHeight = msg.Height
139 c.setCommandType(c.selected),
140 c.commandList.SetSize(c.listWidth(), c.listHeight()),
142 case pubsub.Event[mcp.Event]:
143 // Reload MCP prompts when MCP state changes
144 if msg.Type == pubsub.UpdatedEvent {
145 c.mcpPrompts.SetSlice(loadMCPPrompts())
146 // If we're currently viewing MCP prompts, refresh the list
147 if c.selected == MCPPrompts {
148 return c, c.setCommandType(MCPPrompts)
152 case tea.KeyPressMsg:
154 case key.Matches(msg, c.keyMap.Select):
155 selectedItem := c.commandList.SelectedItem()
156 if selectedItem == nil {
157 return c, nil // No item selected, do nothing
159 command := (*selectedItem).Value()
160 return c, tea.Sequence(
161 util.CmdHandler(dialogs.CloseDialogMsg{}),
162 command.Handler(command),
164 case key.Matches(msg, c.keyMap.Tab):
165 if len(c.userCommands) == 0 && c.mcpPrompts.Len() == 0 {
168 return c, c.setCommandType(c.next())
169 case key.Matches(msg, c.keyMap.Close):
170 return c, util.CmdHandler(dialogs.CloseDialogMsg{})
172 u, cmd := c.commandList.Update(msg)
173 c.commandList = u.(listModel)
180func (c *commandDialogCmp) next() commandType {
183 if len(c.userCommands) > 0 {
186 if c.mcpPrompts.Len() > 0 {
191 if c.mcpPrompts.Len() > 0 {
196 return SystemCommands
198 return SystemCommands
202func (c *commandDialogCmp) View() string {
203 t := styles.CurrentTheme()
204 listView := c.commandList
205 radio := c.commandTypeRadio()
207 header := t.S().Base.Padding(0, 1, 1, 1).Render(core.Title("Commands", c.width-lipgloss.Width(radio)-5) + " " + radio)
208 if len(c.userCommands) == 0 && c.mcpPrompts.Len() == 0 {
209 header = t.S().Base.Padding(0, 1, 1, 1).Render(core.Title("Commands", c.width-4))
211 content := lipgloss.JoinVertical(
216 t.S().Base.Width(c.width-2).PaddingLeft(1).AlignHorizontal(lipgloss.Left).Render(c.help.View(c.keyMap)),
218 return c.style().Render(content)
221func (c *commandDialogCmp) Cursor() *tea.Cursor {
222 if cursor, ok := c.commandList.(util.Cursor); ok {
223 cursor := cursor.Cursor()
225 cursor = c.moveCursor(cursor)
232func (c *commandDialogCmp) commandTypeRadio() string {
233 t := styles.CurrentTheme()
235 fn := func(i commandType) string {
237 return "◉ " + i.String()
239 return "○ " + i.String()
245 if len(c.userCommands) > 0 {
246 parts = append(parts, fn(UserCommands))
248 if c.mcpPrompts.Len() > 0 {
249 parts = append(parts, fn(MCPPrompts))
251 return t.S().Base.Foreground(t.FgHalfMuted).Render(strings.Join(parts, " "))
254func (c *commandDialogCmp) listWidth() int {
255 return defaultWidth - 2 // 4 for padding
258func (c *commandDialogCmp) setCommandType(commandType commandType) tea.Cmd {
259 c.selected = commandType
261 var commands []Command
264 commands = c.defaultCommands()
266 commands = c.userCommands
268 commands = slices.Collect(c.mcpPrompts.Seq())
271 commandItems := []list.CompletionItem[Command]{}
272 for _, cmd := range commands {
273 opts := []list.CompletionItemOption{
274 list.WithCompletionID(cmd.ID),
276 if cmd.Shortcut != "" {
279 list.WithCompletionShortcut(cmd.Shortcut),
282 commandItems = append(commandItems, list.NewCompletionItem(cmd.Title, cmd, opts...))
284 return c.commandList.SetItems(commandItems)
287func (c *commandDialogCmp) listHeight() int {
288 listHeigh := len(c.commandList.Items()) + 2 + 4 // height based on items + 2 for the input + 4 for the sections
289 return min(listHeigh, c.wHeight/2)
292func (c *commandDialogCmp) moveCursor(cursor *tea.Cursor) *tea.Cursor {
293 row, col := c.Position()
296 cursor.X = cursor.X + col + 2
300func (c *commandDialogCmp) style() lipgloss.Style {
301 t := styles.CurrentTheme()
304 Border(lipgloss.RoundedBorder()).
305 BorderForeground(t.BorderFocus)
308func (c *commandDialogCmp) Position() (int, int) {
309 row := c.wHeight/4 - 2 // just a bit above the center
315func (c *commandDialogCmp) defaultCommands() []Command {
316 commands := []Command{
319 Title: "New Session",
320 Description: "start a new session",
322 Handler: func(cmd Command) tea.Cmd {
323 return util.CmdHandler(NewSessionsMsg{})
327 ID: "switch_session",
328 Title: "Switch Session",
329 Description: "Switch to a different session",
331 Handler: func(cmd Command) tea.Cmd {
332 return util.CmdHandler(SwitchSessionsMsg{})
337 Title: "Switch Model",
338 Description: "Switch to a different model",
340 Handler: func(cmd Command) tea.Cmd {
341 return util.CmdHandler(SwitchModelMsg{})
346 // Only show compact command if there's an active session
347 if c.sessionID != "" {
348 commands = append(commands, Command{
350 Title: "Summarize Session",
351 Description: "Summarize the current session and create a new one with the summary",
352 Handler: func(cmd Command) tea.Cmd {
353 return util.CmdHandler(CompactMsg{
354 SessionID: c.sessionID,
360 // Add reasoning toggle for models that support it
362 if agentCfg, ok := cfg.Agents[config.AgentCoder]; ok {
363 providerCfg := cfg.GetProviderForModel(agentCfg.Model)
364 model := cfg.GetModelByType(agentCfg.Model)
365 if providerCfg != nil && model != nil && model.CanReason {
366 selectedModel := cfg.Models[agentCfg.Model]
368 // Anthropic models: thinking toggle
369 if providerCfg.Type == catwalk.TypeAnthropic {
371 if selectedModel.Think {
374 commands = append(commands, Command{
375 ID: "toggle_thinking",
376 Title: status + " Thinking Mode",
377 Description: "Toggle model thinking for reasoning-capable models",
378 Handler: func(cmd Command) tea.Cmd {
379 return util.CmdHandler(ToggleThinkingMsg{})
384 // OpenAI models: reasoning effort dialog
385 if len(model.ReasoningLevels) > 0 {
386 commands = append(commands, Command{
387 ID: "select_reasoning_effort",
388 Title: "Select Reasoning Effort",
389 Description: "Choose reasoning effort level (low/medium/high)",
390 Handler: func(cmd Command) tea.Cmd {
391 return util.CmdHandler(OpenReasoningDialogMsg{})
397 // Only show toggle compact mode command if window width is larger than compact breakpoint (90)
398 if c.wWidth > 120 && c.sessionID != "" {
399 commands = append(commands, Command{
400 ID: "toggle_sidebar",
401 Title: "Toggle Sidebar",
402 Description: "Toggle between compact and normal layout",
403 Handler: func(cmd Command) tea.Cmd {
404 return util.CmdHandler(ToggleCompactModeMsg{})
408 if c.sessionID != "" {
409 agentCfg := config.Get().Agents[config.AgentCoder]
410 model := config.Get().GetModelByType(agentCfg.Model)
411 if model.SupportsImages {
412 commands = append(commands, Command{
414 Title: "Open File Picker",
416 Description: "Open file picker",
417 Handler: func(cmd Command) tea.Cmd {
418 return util.CmdHandler(OpenFilePickerMsg{})
424 // Add external editor command if $EDITOR is available
425 if os.Getenv("EDITOR") != "" {
426 commands = append(commands, Command{
427 ID: "open_external_editor",
428 Title: "Open External Editor",
430 Description: "Open external editor to compose message",
431 Handler: func(cmd Command) tea.Cmd {
432 return util.CmdHandler(OpenExternalEditorMsg{})
437 return append(commands, []Command{
440 Title: "Toggle Yolo Mode",
441 Description: "Toggle yolo mode",
442 Handler: func(cmd Command) tea.Cmd {
443 return util.CmdHandler(ToggleYoloModeMsg{})
448 Title: "Toggle Help",
450 Description: "Toggle help",
451 Handler: func(cmd Command) tea.Cmd {
452 return util.CmdHandler(ToggleHelpMsg{})
457 Title: "Initialize Project",
458 Description: fmt.Sprintf("Create/Update the %s memory file", config.Get().Options.InitializeAs),
459 Handler: func(cmd Command) tea.Cmd {
460 initPrompt, err := agent.InitializePrompt(*config.Get())
462 return util.ReportError(err)
464 return util.CmdHandler(chat.SendMsg{
474 Handler: func(cmd Command) tea.Cmd {
475 return util.CmdHandler(QuitMsg{})
481func (c *commandDialogCmp) ID() dialogs.DialogID {
482 return CommandsDialogID
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