Skip to content

Commit 2314b8e

Browse files
committed
feat: display content on main panel from left panels
Signed-off-by: Ayush <mail@ayuch.dev>
1 parent 09dff50 commit 2314b8e

File tree

8 files changed

+182
-51
lines changed

8 files changed

+182
-51
lines changed

internal/git/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (g *GitCommands) ShowCommit(commitHash string) (string, error) {
4242
commitHash = "HEAD"
4343
}
4444

45-
cmd := exec.Command("git", "show", commitHash)
45+
cmd := exec.Command("git", "show", "--color=always", commitHash)
4646
output, err := cmd.CombinedOutput()
4747
if err != nil {
4848
return string(output), fmt.Errorf("failed to show commit: %v", err)

internal/git/diff.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ type DiffOptions struct {
1111
Commit2 string
1212
Cached bool
1313
Stat bool
14+
Color bool
1415
}
1516

1617
// ShowDiff shows changes between commits, commit and working tree, etc.
1718
func (g *GitCommands) ShowDiff(options DiffOptions) (string, error) {
1819
args := []string{"diff"}
1920

21+
if options.Color {
22+
args = append(args, "--color=always")
23+
}
2024
if options.Cached {
2125
args = append(args, "--cached")
2226
}
2327
if options.Stat {
2428
args = append(args, "--stat")
2529
}
30+
31+
if options.Commit1 != "" || options.Commit2 != "" {
32+
args = append(args, "--")
33+
}
34+
2635
if options.Commit1 != "" {
2736
args = append(args, options.Commit1)
2837
}

internal/git/log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type LogOptions struct {
2424
MaxCount int
2525
Format string
2626
Color string
27+
Branch string
2728
}
2829

2930
// GetCommitLogsGraph fetches the git log with a graph format and returns it as a
@@ -67,6 +68,9 @@ func (g *GitCommands) ShowLog(options LogOptions) (string, error) {
6768
if options.Color != "" {
6869
args = append(args, fmt.Sprintf("--color=%s", options.Color))
6970
}
71+
if options.Branch != "" {
72+
args = append(args, options.Branch)
73+
}
7074

7175
cmd := ExecCommand("git", args...)
7276
output, err := cmd.CombinedOutput()

internal/git/repo.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ func (g *GitCommands) GetGitRepoPath() (repoPath string, err error) {
3434
repoPath = strings.TrimSpace(string(repoPathBytes))
3535
return repoPath, nil
3636
}
37+
38+
// GetUserName returns the user's name from the git config.
39+
func (g *GitCommands) GetUserName() (string, error) {
40+
cmd := ExecCommand("git", "config", "user.name")
41+
output, err := cmd.Output()
42+
if err != nil {
43+
return "", err
44+
}
45+
return strings.TrimSpace(string(output)), nil
46+
}

internal/git/stash.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (g *GitCommands) Stash(options StashOptions) (string, error) {
8484
} else if options.List {
8585
args = []string{"stash", "list"}
8686
} else if options.Show {
87-
args = []string{"stash", "show"}
87+
args = []string{"stash", "show", "--color=always"}
8888
if options.StashID != "" {
8989
args = append(args, options.StashID)
9090
}
@@ -98,6 +98,10 @@ func (g *GitCommands) Stash(options StashOptions) (string, error) {
9898
cmd := exec.Command("git", args...)
9999
output, err := cmd.CombinedOutput()
100100
if err != nil {
101+
// The command fails if there's no stash.
102+
if strings.Contains(string(output), "No stash entries found") || strings.Contains(string(output), "No stash found") {
103+
return "No stashes found.", nil
104+
}
101105
return string(output), fmt.Errorf("stash operation failed: %v", err)
102106
}
103107

internal/tui/filetree.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Node struct {
1818

1919
// BuildTree parses the output of `git status --porcelain` to construct a file tree.
2020
func BuildTree(gitStatus string) *Node {
21-
root := &Node{name: repoRootNodeName}
21+
root := &Node{name: repoRootNodeName, path: "."}
2222

2323
lines := strings.Split(gitStatus, "\n")
2424
if len(lines) == 1 && lines[0] == "" {
@@ -30,30 +30,35 @@ func BuildTree(gitStatus string) *Node {
3030
continue
3131
}
3232
status := line[:2]
33-
path := strings.TrimSpace(line[porcelainStatusPrefixLength:])
33+
fullPath := strings.TrimSpace(line[porcelainStatusPrefixLength:])
3434
isRenamed := false
3535

3636
if status[0] == 'R' || status[0] == 'C' {
37-
parts := strings.Split(path, gitRenameDelimiter)
37+
parts := strings.Split(fullPath, gitRenameDelimiter)
3838
if len(parts) == 2 {
39-
path = parts[1]
39+
fullPath = parts[1]
4040
isRenamed = true
4141
}
4242
}
4343

44-
parts := strings.Split(path, string(filepath.Separator))
44+
parts := strings.Split(fullPath, string(filepath.Separator))
4545
currentNode := root
4646
for i, part := range parts {
4747
childNode := currentNode.findChild(part)
4848
if childNode == nil {
49-
childNode = &Node{name: part}
49+
// Construct path for the new node based on its parent
50+
nodePath := filepath.Join(currentNode.path, part)
51+
if currentNode.path == "." {
52+
nodePath = part
53+
}
54+
childNode = &Node{name: part, path: nodePath}
5055
currentNode.children = append(currentNode.children, childNode)
5156
}
5257
currentNode = childNode
5358

54-
if i == len(parts)-1 {
59+
if i == len(parts)-1 { // Leaf node (file)
5560
currentNode.status = status
56-
currentNode.path = path
61+
currentNode.path = fullPath // Overwrite with the full path from git
5762
currentNode.isRenamed = isRenamed
5863
}
5964
}
@@ -119,6 +124,7 @@ func (n *Node) compact() {
119124
for len(n.children) == 1 && len(n.children[0].children) > 0 {
120125
child := n.children[0]
121126
n.name = filepath.Join(n.name, child.name)
127+
n.path = child.path
122128
n.children = child.children
123129
}
124130
}
@@ -132,14 +138,14 @@ func (n *Node) renderRecursive(prefix string, theme Theme) []string {
132138

133139
if len(child.children) > 0 { // It's a directory
134140
displayName := dirExpandedIcon + child.name
135-
lines = append(lines, fmt.Sprintf("%s\t\t%s", prefix, displayName))
141+
lines = append(lines, fmt.Sprintf("%s\t\t%s\t%s", prefix, displayName, child.path))
136142
lines = append(lines, child.renderRecursive(newPrefix, theme)...)
137143
} else { // It's a file.
138144
displayName := child.name
139145
if child.isRenamed {
140146
displayName = child.path
141147
}
142-
lines = append(lines, fmt.Sprintf("%s\t%s\t%s", prefix, child.status, displayName))
148+
lines = append(lines, fmt.Sprintf("%s\t%s\t%s\t%s", prefix, child.status, displayName, child.path))
143149
}
144150
}
145151
return lines

internal/tui/model.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import (
1010

1111
// Model represents the state of the TUI.
1212
type Model struct {
13-
width int
14-
height int
15-
panels []panel
16-
panelHeights []int
17-
focusedPanel Panel
18-
theme Theme
19-
themeNames []string
20-
themeIndex int
21-
help help.Model
22-
helpViewport viewport.Model
23-
helpContent string
24-
showHelp bool
25-
git *git.GitCommands
26-
repoName string
27-
branchName string
13+
width int
14+
height int
15+
panels []panel
16+
panelHeights []int
17+
focusedPanel Panel
18+
activeSourcePanel Panel
19+
theme Theme
20+
themeNames []string
21+
themeIndex int
22+
help help.Model
23+
helpViewport viewport.Model
24+
helpContent string
25+
showHelp bool
26+
git *git.GitCommands
27+
repoName string
28+
branchName string
2829
}
2930

3031
// initialModel creates the initial state of the application.
@@ -46,17 +47,18 @@ func initialModel() Model {
4647
}
4748

4849
return Model{
49-
theme: Themes[themeNames[0]],
50-
themeNames: themeNames,
51-
themeIndex: 0,
52-
focusedPanel: StatusPanel,
53-
help: help.New(),
54-
helpViewport: viewport.New(0, 0),
55-
showHelp: false,
56-
git: gc,
57-
repoName: repoName,
58-
branchName: branchName,
59-
panels: panels,
50+
theme: Themes[themeNames[0]],
51+
themeNames: themeNames,
52+
themeIndex: 0,
53+
focusedPanel: StatusPanel,
54+
activeSourcePanel: StatusPanel,
55+
help: help.New(),
56+
helpViewport: viewport.New(0, 0),
57+
showHelp: false,
58+
git: gc,
59+
repoName: repoName,
60+
branchName: branchName,
61+
panels: panels,
6062
}
6163
}
6264

@@ -69,8 +71,8 @@ func (m Model) Init() tea.Cmd {
6971
m.fetchPanelContent(BranchesPanel),
7072
m.fetchPanelContent(CommitsPanel),
7173
m.fetchPanelContent(StashPanel),
72-
m.fetchPanelContent(MainPanel),
7374
m.fetchPanelContent(SecondaryPanel),
75+
m.updateMainPanel(),
7476
)
7577
}
7678

0 commit comments

Comments
 (0)