|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// Node represents a file or directory within the file tree structure. |
| 11 | +type Node struct { |
| 12 | + name string |
| 13 | + status string // Git status prefix (e.g., "M", "??"), only for file nodes. |
| 14 | + children []*Node |
| 15 | +} |
| 16 | + |
| 17 | +// BuildTree parses the output of `git status --porcelain` to construct a file |
| 18 | +// tree. It processes each line, builds a hierarchical structure of nodes, |
| 19 | +// sorts them, and compacts single-child directories for a cleaner display. |
| 20 | +func BuildTree(gitStatus string) *Node { |
| 21 | + root := &Node{name: "."} |
| 22 | + |
| 23 | + lines := strings.Split(strings.TrimSpace(gitStatus), "\n") |
| 24 | + if len(lines) == 1 && lines[0] == "" { |
| 25 | + return root // No changes, return the root. |
| 26 | + } |
| 27 | + |
| 28 | + for _, line := range lines { |
| 29 | + if len(line) < 4 { |
| 30 | + continue |
| 31 | + } |
| 32 | + status := strings.TrimSpace(line[:2]) |
| 33 | + path := line[3:] |
| 34 | + |
| 35 | + parts := strings.Split(path, string(filepath.Separator)) |
| 36 | + currentNode := root |
| 37 | + for i, part := range parts { |
| 38 | + // Traverse the tree, creating nodes as necessary. |
| 39 | + childNode := currentNode.findChild(part) |
| 40 | + if childNode == nil { |
| 41 | + childNode = &Node{name: part} |
| 42 | + currentNode.children = append(currentNode.children, childNode) |
| 43 | + } |
| 44 | + currentNode = childNode |
| 45 | + |
| 46 | + // The last part of the path is the file, so set its status. |
| 47 | + if i == len(parts)-1 { |
| 48 | + currentNode.status = status |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + root.sort() |
| 54 | + root.compact() |
| 55 | + |
| 56 | + return root |
| 57 | +} |
| 58 | + |
| 59 | +// findChild searches for an immediate child node by name. |
| 60 | +func (n *Node) findChild(name string) *Node { |
| 61 | + for _, child := range n.children { |
| 62 | + if child.name == name { |
| 63 | + return child |
| 64 | + } |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// sort recursively sorts the children of a node. Directories are listed first, |
| 70 | +// then files, with both groups sorted alphabetically. |
| 71 | +func (n *Node) sort() { |
| 72 | + if n.children == nil { |
| 73 | + return |
| 74 | + } |
| 75 | + sort.SliceStable(n.children, func(i, j int) bool { |
| 76 | + isDirI := len(n.children[i].children) > 0 |
| 77 | + isDirJ := len(n.children[j].children) > 0 |
| 78 | + if isDirI != isDirJ { |
| 79 | + return isDirI // Directories first. |
| 80 | + } |
| 81 | + return n.children[i].name < n.children[j].name // Then sort alphabetically. |
| 82 | + }) |
| 83 | + |
| 84 | + for _, child := range n.children { |
| 85 | + child.sort() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// compact recursively merges directories that contain only a single sub-directory. |
| 90 | +// For example, a path like "src/main/go" becomes a single node. |
| 91 | +func (n *Node) compact() { |
| 92 | + if n.children == nil { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + // First, compact all children in a post-order traversal. |
| 97 | + for _, child := range n.children { |
| 98 | + child.compact() |
| 99 | + } |
| 100 | + |
| 101 | + // Merge this node with its child if it's a single-directory container. |
| 102 | + for len(n.children) == 1 && len(n.children[0].children) > 0 { |
| 103 | + child := n.children[0] |
| 104 | + n.name = filepath.Join(n.name, child.name) |
| 105 | + n.children = child.children |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Render traverses the tree and returns a slice of strings for display. |
| 110 | +func (n *Node) Render() []string { |
| 111 | + return n.renderRecursive("") |
| 112 | +} |
| 113 | + |
| 114 | +// renderRecursive performs a depth-first traversal to generate the visual |
| 115 | +// representation of the tree, using box-drawing characters to show hierarchy. |
| 116 | +func (n *Node) renderRecursive(prefix string) []string { |
| 117 | + var lines []string |
| 118 | + for i, child := range n.children { |
| 119 | + // Use different connectors for the last child in a list. |
| 120 | + connector := "├─" |
| 121 | + newPrefix := "│ " |
| 122 | + if i == len(n.children)-1 { |
| 123 | + connector = "└─" |
| 124 | + newPrefix = " " |
| 125 | + } |
| 126 | + |
| 127 | + if len(child.children) > 0 { |
| 128 | + // It's a directory. |
| 129 | + lines = append(lines, fmt.Sprintf("%s%s▼ %s", prefix, connector, child.name)) |
| 130 | + lines = append(lines, child.renderRecursive(prefix+newPrefix)...) |
| 131 | + } else { |
| 132 | + // It's a file. |
| 133 | + lines = append(lines, fmt.Sprintf("%s%s %s %s", prefix, connector, child.status, child.name)) |
| 134 | + } |
| 135 | + } |
| 136 | + return lines |
| 137 | +} |
0 commit comments