From 12a2e78e37bdfc7dd05b0c936201ab036b5b6867 Mon Sep 17 00:00:00 2001 From: Tommy Guo Date: Wed, 28 Jan 2026 11:47:31 -0500 Subject: [PATCH] inital commit --- go.mod | 31 +++++ go.sum | 55 +++++++++ main.go | 346 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 432 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1776d45 --- /dev/null +++ b/go.mod @@ -0,0 +1,31 @@ +module github.com/oug-t/stagi + +go 1.25.6 + +require ( + github.com/charmbracelet/bubbles v0.21.0 + github.com/charmbracelet/bubbletea v1.3.10 + github.com/charmbracelet/lipgloss v1.1.0 +) + +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect + github.com/charmbracelet/x/ansi v0.10.1 // indirect + github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect + github.com/charmbracelet/x/term v0.2.1 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.16.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/sahilm/fuzzy v0.1.1 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sys v0.36.0 // indirect + golang.org/x/text v0.3.8 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a5aaf28 --- /dev/null +++ b/go.sum @@ -0,0 +1,55 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= +github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= +github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs= +github.com/charmbracelet/bubbles v0.21.0/go.mod h1:HF+v6QUR4HkEpz62dx7ym2xc71/KBHg+zKwJtMw+qtg= +github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw= +github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk= +github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= +github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= +github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ= +github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs= +github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ= +github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= +github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= +github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= +github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= +github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= +golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..619cb16 --- /dev/null +++ b/main.go @@ -0,0 +1,346 @@ +package main + +import ( + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "sort" + "strings" + + "github.com/charmbracelet/bubbles/list" + "github.com/charmbracelet/bubbles/viewport" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +const targetBranch = "main" + +// --- STYLES --- + +var ( + // Modern, Clean Theme + colorBorder = lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#383838"} + colorSelected = lipgloss.AdaptiveColor{Light: "#1F1F1F", Dark: "#F8F8F2"} + colorInactive = lipgloss.AdaptiveColor{Light: "#A8A8A8", Dark: "#626262"} + colorAccent = lipgloss.AdaptiveColor{Light: "#00BCF0", Dark: "#00BCF0"} // Dash-like Cyan + + // Panes + treeStyle = lipgloss.NewStyle(). + Border(lipgloss.NormalBorder(), false, true, false, false). // Right border only + BorderForeground(colorBorder). + MarginRight(1) + + diffStyle = lipgloss.NewStyle(). + Padding(0, 1) + + // Tree Items + itemStyle = lipgloss.NewStyle().PaddingLeft(1) + selectedItemStyle = lipgloss.NewStyle(). + PaddingLeft(0). + Foreground(colorSelected). + Bold(true) +) + +// --- DATA STRUCTURES --- + +type node struct { + name string + fullPath string + children map[string]*node + isDir bool +} + +type treeItem struct { + path string + fullPath string + isDir bool + depth int +} + +func (i treeItem) FilterValue() string { return i.fullPath } +func (i treeItem) Description() string { return "" } + +// Title renders the Nerd Font icon + filename +func (i treeItem) Title() string { + indent := strings.Repeat(" ", i.depth) + icon := getIcon(i.path, i.isDir) + return fmt.Sprintf("%s%s %s", indent, icon, i.path) +} + +// --- NERD FONT ICON MAPPING --- + +func getIcon(name string, isDir bool) string { + if isDir { + return "" // Folder icon + } + + ext := filepath.Ext(name) + switch strings.ToLower(ext) { + case ".go": + return "" // Go Gopher + case ".js", ".ts": + return "" // JS/Node + case ".md": + return "" // Markdown + case ".json": + return "" // JSON + case ".yml", ".yaml": + return "" // Settings/Config + case ".html": + return "" + case ".css": + return "" + case ".git": + return "" // Git + case ".dockerfile", "dockerfile": + return "" // Docker + default: + return "" // Default File + } +} + +// --- DELEGATE (CUSTOM RENDERER) --- + +type delegate struct{} + +func (d delegate) Height() int { return 1 } +func (d delegate) Spacing() int { return 0 } +func (d delegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil } +func (d delegate) Render(w io.Writer, m list.Model, index int, item list.Item) { + i, ok := item.(treeItem) + if !ok { + return + } + + str := i.Title() + + // If selected, add a border or indicator + if index == m.Index() { + fmt.Fprint(w, selectedItemStyle.Render("│ "+str)) + } else { + // Render unselected items with subtle gray + fmt.Fprint(w, itemStyle.Render(str)) + } +} + +// --- MODEL --- + +type model struct { + fileTree list.Model + diffViewport viewport.Model + selectedPath string + width, height int +} + +func initialModel() model { + // 1. Fetch changed files + // Compares HEAD against targetBranch + cmd := exec.Command("git", "diff", "--name-only", targetBranch) + out, _ := cmd.Output() + filePaths := strings.Split(strings.TrimSpace(string(out)), "\n") + + // Handle empty diff case + if len(filePaths) == 1 && filePaths[0] == "" { + filePaths = []string{} + } + + // 2. Build Tree + items := buildTree(filePaths) + + // 3. Configure List + l := list.New(items, delegate{}, 0, 0) + l.SetShowTitle(false) + l.SetShowHelp(false) + l.SetShowStatusBar(false) + l.SetFilteringEnabled(false) + + vp := viewport.New(0, 0) + + m := model{ + fileTree: l, + diffViewport: vp, + } + + // Select first file if available + if len(items) > 0 { + if first, ok := items[0].(treeItem); ok { + m.selectedPath = first.fullPath + } + } + + return m +} + +func (m model) Init() tea.Cmd { + if m.selectedPath != "" { + return fetchDiff(m.selectedPath) + } + return nil +} + +// --- UPDATE --- + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmd tea.Cmd + var cmds []tea.Cmd + + switch msg := msg.(type) { + case tea.WindowSizeMsg: + m.width = msg.Width + m.height = msg.Height + + // Layout: 25% Tree (Fixed Left), Rest Diff + treeWidth := int(float64(m.width) * 0.25) + diffWidth := m.width - treeWidth - 2 + + m.fileTree.SetSize(treeWidth, m.height) + m.diffViewport.Width = diffWidth + m.diffViewport.Height = m.height + + case tea.KeyMsg: + switch msg.String() { + case "q", "ctrl+c": + return m, tea.Quit + + case "up", "k", "down", "j": + m.fileTree, cmd = m.fileTree.Update(msg) + cmds = append(cmds, cmd) + + // Fetch diff only if actual file selected + if item, ok := m.fileTree.SelectedItem().(treeItem); ok && !item.isDir { + if item.fullPath != m.selectedPath { + m.selectedPath = item.fullPath + cmds = append(cmds, fetchDiff(m.selectedPath)) + } + } + + case "e": + // Edit in NVIM + if m.selectedPath != "" { + return m, openEditor(m.selectedPath) + } + } + + case diffMsg: + m.diffViewport.SetContent(string(msg)) + + case editorFinishedMsg: + return m, fetchDiff(m.selectedPath) + } + + return m, tea.Batch(cmds...) +} + +// --- VIEW --- + +func (m model) View() string { + if m.width == 0 { + return "Loading..." + } + + treeView := treeStyle.Copy(). + Width(m.fileTree.Width()). + Height(m.fileTree.Height()). + Render(m.fileTree.View()) + + diffView := diffStyle.Copy(). + Width(m.diffViewport.Width). + Height(m.diffViewport.Height). + Render(m.diffViewport.View()) + + return lipgloss.JoinHorizontal(lipgloss.Top, treeView, diffView) +} + +// --- COMMANDS --- + +type diffMsg string +type editorFinishedMsg struct{ err error } + +func fetchDiff(path string) tea.Cmd { + return func() tea.Msg { + // Use --color=always to let git handle syntax highlighting + out, _ := exec.Command("git", "diff", "--color=always", targetBranch, "--", path).Output() + return diffMsg(out) + } +} + +func openEditor(path string) tea.Cmd { + editor := os.Getenv("EDITOR") + if editor == "" { + editor = "vim" + } + c := exec.Command(editor, path) + c.Stdin, c.Stdout, c.Stderr = os.Stdin, os.Stdout, os.Stderr + return tea.ExecProcess(c, func(err error) tea.Msg { + return editorFinishedMsg{err} + }) +} + +// --- TREE ALGORITHMS --- + +func buildTree(paths []string) []list.Item { + root := &node{children: make(map[string]*node)} + + for _, path := range paths { + parts := strings.Split(path, "/") + current := root + for i, part := range parts { + if _, exists := current.children[part]; !exists { + isDir := i < len(parts)-1 + fullPath := strings.Join(parts[:i+1], "/") + current.children[part] = &node{ + name: part, + fullPath: fullPath, + children: make(map[string]*node), + isDir: isDir, + } + } + current = current.children[part] + } + } + + var items []list.Item + flatten(root, 0, &items) + return items +} + +func flatten(n *node, depth int, items *[]list.Item) { + keys := make([]string, 0, len(n.children)) + for k := range n.children { + keys = append(keys, k) + } + + sort.Slice(keys, func(i, j int) bool { + a, b := n.children[keys[i]], n.children[keys[j]] + if a.isDir && !b.isDir { + return true + } + if !a.isDir && b.isDir { + return false + } + return a.name < b.name + }) + + for _, k := range keys { + child := n.children[k] + *items = append(*items, treeItem{ + path: child.name, + fullPath: child.fullPath, + isDir: child.isDir, + depth: depth, + }) + if child.isDir { + flatten(child, depth+1, items) + } + } +} + +func main() { + p := tea.NewProgram(initialModel(), tea.WithAltScreen()) + if _, err := p.Run(); err != nil { + fmt.Println(err) + os.Exit(1) + } +}