| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | tea "charm.land/bubbletea/v2" |
| 8 | |
| 9 | "reasonix/internal/i18n" |
| 10 | ) |
| 11 | |
| 12 | type clearConfirm struct { |
| 13 | confirm int // 0 = clear, 1 = cancel |
| 14 | } |
| 15 | |
| 16 | func (m chatTUI) handleClearConfirmKey(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) { |
| 17 | switch msg.String() { |
| 18 | case "up", "down", "left", "right", "j", "k", "tab", "shift+tab": |
| 19 | if m.clearConfirm.confirm == 0 { |
| 20 | m.clearConfirm.confirm = 1 |
| 21 | } else { |
| 22 | m.clearConfirm.confirm = 0 |
| 23 | } |
| 24 | case "y", "Y": |
| 25 | return m.confirmClearContext() |
| 26 | case "n", "N", "esc", "ctrl+c": |
| 27 | m.clearConfirm = nil |
| 28 | case "enter": |
| 29 | if m.clearConfirm.confirm == 0 { |
| 30 | return m.confirmClearContext() |
| 31 | } |
| 32 | m.clearConfirm = nil |
| 33 | } |
| 34 | return m, nil |
| 35 | } |
| 36 | |
| 37 | func (m chatTUI) confirmClearContext() (tea.Model, tea.Cmd) { |
| 38 | m.clearConfirm = nil |
| 39 | if err := m.ctrl.ClearSession(); err != nil { |
| 40 | m.notice(fmt.Sprintf("%s: %v", i18n.M.SlashClearFailed, err)) |
| 41 | return m, nil |
| 42 | } |
| 43 | m.followSessionLease() |
| 44 | m.resetFreshContextView(true) |
| 45 | m.notice(i18n.M.SlashClearDone) |
| 46 | return m, tea.ClearScreen |
| 47 | } |
| 48 | |
| 49 | func (m *chatTUI) resetFreshContextView(clearTranscript bool) { |
| 50 | m.finalizeStreamed() |
| 51 | m.pending.Reset() |
| 52 | m.reasoning.Reset() |
| 53 | m.todoArgs = "" |
| 54 | m.chooser = nil |
| 55 | m.pendingApproval = nil |
| 56 | m.bubblePending = false |
| 57 | m.turnDiscarded = false |
| 58 | if clearTranscript { |
| 59 | m.clearTranscriptDisplay() |
| 60 | m.sessionSwitch = true |
| 61 | } else { |
| 62 | m.commitLine("") |
| 63 | } |
| 64 | m.commitTranscriptSource(transcriptSource{kind: transcriptSourceBanner}) |
| 65 | m.transcriptDirty = true |
| 66 | m.forceGotoBottom = true |
| 67 | } |
| 68 | |
| 69 | func (m chatTUI) renderClearConfirm() string { |
| 70 | if m.clearConfirm == nil { |
| 71 | return "" |
| 72 | } |
| 73 | w := max(viewWidth(m.width), 40) |
| 74 | var b strings.Builder |
| 75 | b.WriteString(i18n.M.SlashClearPrompt + "\n") |
| 76 | b.WriteString(viewMeta("This deletes the current transcript from local history and keeps only the system prompt.") + "\n\n") |
| 77 | b.WriteString(rowLine(m.clearConfirm.confirm == 0, 1, "", "Clear", false) + "\n") |
| 78 | b.WriteString(rowLine(m.clearConfirm.confirm == 1, 2, "", "Cancel", false)) |
| 79 | return choicePanelStyle.Width(w).Render(b.String()) |
| 80 | } |
| 81 |