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