| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | tea "charm.land/bubbletea/v2" |
| 12 | "github.com/charmbracelet/x/ansi" |
| 13 | |
| 14 | "reasonix/internal/gitcmd" |
| 15 | ) |
| 16 | |
| 17 | const gitStatusTimeout = 700 * time.Millisecond |
| 18 | |
| 19 | type gitStatus struct { |
| 20 | Repo string |
| 21 | Branch string |
| 22 | Detached bool |
| 23 | Added int |
| 24 | Removed int |
| 25 | Untracked int |
| 26 | } |
| 27 | |
| 28 | // fetchGitStatus reads the status line through repo, the identity the session |
| 29 | // resolved when it opened. |
| 30 | func fetchGitStatus(repo gitcmd.Repo) tea.Cmd { |
| 31 | return func() tea.Msg { |
| 32 | ctx, cancel := context.WithTimeout(context.Background(), gitStatusTimeout) |
| 33 | defer cancel() |
| 34 | status, err := loadGitStatus(ctx, repo) |
| 35 | if err != nil { |
| 36 | return gitStatusMsg{} |
| 37 | } |
| 38 | return gitStatusMsg{status: status} |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func loadGitStatus(ctx context.Context, repo gitcmd.Repo) (gitStatus, error) { |
| 43 | return loadGitStatusWithRunner(ctx, repo, runGit) |
| 44 | } |
| 45 | |
| 46 | func loadGitStatusWithRunner(ctx context.Context, repo gitcmd.Repo, run func(context.Context, gitcmd.Repo, ...string) (string, error)) (gitStatus, error) { |
| 47 | if !repo.Valid() { |
| 48 | return gitStatus{}, gitcmd.ErrNotRepository |
| 49 | } |
| 50 | root := repo.Top() |
| 51 | |
| 52 | status := gitStatus{Repo: filepath.Base(root.WorkTree)} |
| 53 | if branch, err := run(ctx, root, "symbolic-ref", "--quiet", "--short", "HEAD"); err == nil && strings.TrimSpace(branch) != "" { |
| 54 | status.Branch = strings.TrimSpace(branch) |
| 55 | } else if sha, err := run(ctx, root, "rev-parse", "--short", "HEAD"); err == nil && strings.TrimSpace(sha) != "" { |
| 56 | status.Branch = strings.TrimSpace(sha) |
| 57 | status.Detached = true |
| 58 | } else if ref, err := run(ctx, root, "symbolic-ref", "--short", "HEAD"); err == nil && strings.TrimSpace(ref) != "" { |
| 59 | status.Branch = strings.TrimSpace(ref) |
| 60 | } |
| 61 | if status.Branch == "" { |
| 62 | status.Branch = "HEAD" |
| 63 | status.Detached = true |
| 64 | } |
| 65 | |
| 66 | if out, err := run(ctx, root, "diff", "--numstat", "HEAD", "--"); err == nil { |
| 67 | status.Added, status.Removed = parseGitNumstat(out) |
| 68 | } |
| 69 | if out, err := run(ctx, root, "status", "--porcelain=v1", "--untracked-files=normal"); err == nil { |
| 70 | status.Untracked = countUntracked(out) |
| 71 | } |
| 72 | if err := ctx.Err(); err != nil { |
| 73 | return gitStatus{}, err |
| 74 | } |
| 75 | return status, nil |
| 76 | } |
| 77 | |
| 78 | func runGit(ctx context.Context, repo gitcmd.Repo, args ...string) (string, error) { |
| 79 | cmd := repo.Command(ctx, args...) |
| 80 | out, err := cmd.Output() |
| 81 | if err != nil { |
| 82 | return "", err |
| 83 | } |
| 84 | return string(out), nil |
| 85 | } |
| 86 | |
| 87 | func parseGitNumstat(out string) (added int, removed int) { |
| 88 | for line := range strings.SplitSeq(strings.TrimSpace(out), "\n") { |
| 89 | if line == "" { |
| 90 | continue |
| 91 | } |
| 92 | fields := strings.Fields(line) |
| 93 | if len(fields) < 2 { |
| 94 | continue |
| 95 | } |
| 96 | if fields[0] != "-" { |
| 97 | if n, err := strconv.Atoi(fields[0]); err == nil { |
| 98 | added += n |
| 99 | } |
| 100 | } |
| 101 | if fields[1] != "-" { |
| 102 | if n, err := strconv.Atoi(fields[1]); err == nil { |
| 103 | removed += n |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return added, removed |
| 108 | } |
| 109 | |
| 110 | func countUntracked(out string) int { |
| 111 | n := 0 |
| 112 | for line := range strings.SplitSeq(strings.TrimRight(out, "\n"), "\n") { |
| 113 | if strings.HasPrefix(line, "?? ") { |
| 114 | n++ |
| 115 | } |
| 116 | } |
| 117 | return n |
| 118 | } |
| 119 | |
| 120 | func (m chatTUI) gitTag() string { |
| 121 | if strings.TrimSpace(m.gitStatus.Repo) == "" || strings.TrimSpace(m.gitStatus.Branch) == "" { |
| 122 | return "" |
| 123 | } |
| 124 | return m.gitStatus.render(themeFg(m.statusModeColor(), m.gitStatus.Repo), m.gitStatus.Branch) |
| 125 | } |
| 126 | |
| 127 | var ( |
| 128 | statusAutoColor = cliColor{"#f59e0b", 214} |
| 129 | statusPlanColor = cliColor{"#2563eb", 27} |
| 130 | statusYoloColor = cliColor{"#e5484d", 167} |
| 131 | statusShellColor = cliColor{"#16a34a", 71} |
| 132 | modeTagLight = cliColor{"#ffffff", 231} |
| 133 | modeTagDark = cliColor{"#111827", 234} |
| 134 | ) |
| 135 | |
| 136 | func (m chatTUI) statusModeColor() cliColor { |
| 137 | switch { |
| 138 | case m.ctrl != nil && m.ctrl.AutoApproveTools(): |
| 139 | return statusYoloColor |
| 140 | case m.planMode: |
| 141 | return statusPlanColor |
| 142 | default: |
| 143 | return statusAutoColor |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func (s gitStatus) Render() string { |
| 148 | return s.RenderRepo(accent(s.Repo)) |
| 149 | } |
| 150 | |
| 151 | func (s gitStatus) RenderRepo(repo string) string { |
| 152 | if strings.TrimSpace(s.Repo) == "" || strings.TrimSpace(s.Branch) == "" { |
| 153 | return "" |
| 154 | } |
| 155 | return s.render(repo, s.Branch) |
| 156 | } |
| 157 | |
| 158 | func (s gitStatus) RenderWithin(maxWidth int, repoColor cliColor) string { |
| 159 | if strings.TrimSpace(s.Repo) == "" || strings.TrimSpace(s.Branch) == "" { |
| 160 | return "" |
| 161 | } |
| 162 | repo, branch := s.compactIdentity(maxWidth) |
| 163 | out := s.render(themeFg(repoColor, repo), branch) |
| 164 | if maxWidth > 0 && visibleWidth(out) > maxWidth { |
| 165 | return ansi.Truncate(out, maxWidth, "…") |
| 166 | } |
| 167 | return out |
| 168 | } |
| 169 | |
| 170 | func (s gitStatus) compactIdentity(maxWidth int) (repo, branch string) { |
| 171 | repo = strings.TrimSpace(s.Repo) |
| 172 | branch = strings.TrimSpace(s.Branch) |
| 173 | if maxWidth <= 0 { |
| 174 | return repo, branch |
| 175 | } |
| 176 | dirtyWidth := visibleWidth(s.dirtyPlain()) |
| 177 | nameBudget := maxWidth - dirtyWidth - visibleWidth("@") |
| 178 | if nameBudget <= 2 { |
| 179 | return compactEnd(repo, max(1, nameBudget)), "" |
| 180 | } |
| 181 | repoWidth := visibleWidth(repo) |
| 182 | branchWidth := visibleWidth(branch) |
| 183 | if repoWidth+branchWidth <= nameBudget { |
| 184 | return repo, branch |
| 185 | } |
| 186 | |
| 187 | minRepo := min(repoWidth, 8) |
| 188 | if repoBudget := nameBudget - branchWidth; repoBudget >= minRepo { |
| 189 | return compactMiddle(repo, repoBudget), branch |
| 190 | } |
| 191 | |
| 192 | repoBudget := min(repoWidth, max(4, min(10, nameBudget/3))) |
| 193 | if nameBudget-repoBudget < 8 { |
| 194 | repoBudget = max(1, nameBudget-8) |
| 195 | } |
| 196 | branchBudget := max(1, nameBudget-repoBudget) |
| 197 | return compactMiddle(repo, repoBudget), compactMiddle(branch, branchBudget) |
| 198 | } |
| 199 | |
| 200 | func (s gitStatus) dirtyPlain() string { |
| 201 | var parts []string |
| 202 | if s.Added > 0 || s.Removed > 0 { |
| 203 | parts = append(parts, fmt.Sprintf("+%d", s.Added), fmt.Sprintf("-%d", s.Removed)) |
| 204 | } |
| 205 | if s.Untracked > 0 { |
| 206 | parts = append(parts, fmt.Sprintf("?%d", s.Untracked)) |
| 207 | } |
| 208 | if len(parts) == 0 { |
| 209 | return "" |
| 210 | } |
| 211 | return " " + strings.Join(parts, " ") |
| 212 | } |
| 213 | |
| 214 | func (s gitStatus) render(repo, branch string) string { |
| 215 | var b strings.Builder |
| 216 | b.WriteString(repo) |
| 217 | b.WriteString(dim("@")) |
| 218 | if s.Detached { |
| 219 | b.WriteString(yellow(branch)) |
| 220 | } else { |
| 221 | // A branch name is identity, not a success condition. Keep semantic green |
| 222 | // for additions and use the theme's readable neutral value colour here. |
| 223 | b.WriteString(footerValue(branch)) |
| 224 | } |
| 225 | |
| 226 | var parts []string |
| 227 | if s.Added > 0 || s.Removed > 0 { |
| 228 | parts = append(parts, green(fmt.Sprintf("+%d", s.Added)), red(fmt.Sprintf("-%d", s.Removed))) |
| 229 | } |
| 230 | if s.Untracked > 0 { |
| 231 | parts = append(parts, yellow(fmt.Sprintf("?%d", s.Untracked))) |
| 232 | } |
| 233 | if len(parts) > 0 { |
| 234 | b.WriteString(" ") |
| 235 | b.WriteString(strings.Join(parts, " ")) |
| 236 | } |
| 237 | return b.String() |
| 238 | } |
| 239 |