Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ ui:

# Use side-by-side diff view (default: true, set false for unified)
sideBySide: true

# How many levels of folders to open on start (-1 = all, 0 = none, 1 = first level, etc.)
startFoldersOpenDepth: 1
```

| Option | Type | Default | Description |
Expand All @@ -139,7 +142,8 @@ ui:
| `ui.icons` | string | `nerd-fonts-status` | Icon style (see below for details) |
| `ui.colorFileNames` | bool | `true` | Color filenames by git status |
| `ui.showDiffStats` | bool | `true` | Show the amount of lines added / removed next to the file |
| `ui.sideBySide` | bool | `true` | Use side-by-side diff view (false for unified) |
| `ui.sideBySide` | bool | `true` | Use side-by-side diff view (false for unified) |
| `ui.startFoldersOpenDepth` | int | `-1` | Folder open depth on start (-1 = all, 0 = none) |

### Icon Styles

Expand Down
38 changes: 20 additions & 18 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
)

type UIConfig struct {
HideHeader bool `yaml:"hideHeader"`
HideFooter bool `yaml:"hideFooter"`
ShowFileTree bool `yaml:"showFileTree"`
FileTreeWidth int `yaml:"fileTreeWidth"`
SearchTreeWidth int `yaml:"searchTreeWidth"`
Icons string `yaml:"icons"` // "nerd-fonts-status" (default), "nerd-fonts-simple", "nerd-fonts-filetype", "nerd-fonts-full", "unicode", "ascii"
ColorFileNames bool `yaml:"colorFileNames"` // Color filenames by git status (default: true)
ShowDiffStats bool `yaml:"showDiffStats"` // Show the amount of lines added / removed next to the file
SideBySide bool `yaml:"sideBySide"` // Side-by-side diff view (default: true)
HideHeader bool `yaml:"hideHeader"`
HideFooter bool `yaml:"hideFooter"`
ShowFileTree bool `yaml:"showFileTree"`
FileTreeWidth int `yaml:"fileTreeWidth"`
SearchTreeWidth int `yaml:"searchTreeWidth"`
Icons string `yaml:"icons"` // "nerd-fonts-status" (default), "nerd-fonts-simple", "nerd-fonts-filetype", "nerd-fonts-full", "unicode", "ascii"
ColorFileNames bool `yaml:"colorFileNames"` // Color filenames by git status (default: true)
ShowDiffStats bool `yaml:"showDiffStats"` // Show the amount of lines added / removed next to the file
SideBySide bool `yaml:"sideBySide"` // Side-by-side diff view (default: true)
StartFoldersOpenDepth int `yaml:"startFoldersOpenDepth"` // How many levels of folders to open on start (-1 = all, 0 = none)
}

type WatchConfig struct {
Expand All @@ -35,15 +36,16 @@ type Config struct {
func DefaultConfig() Config {
return Config{
UI: UIConfig{
HideHeader: false,
HideFooter: false,
ShowFileTree: true,
FileTreeWidth: 30,
SearchTreeWidth: 50,
Icons: "nerd-fonts-status",
ColorFileNames: true,
SideBySide: true,
ShowDiffStats: true,
HideHeader: false,
HideFooter: false,
ShowFileTree: true,
FileTreeWidth: 30,
SearchTreeWidth: 50,
Icons: "nerd-fonts-status",
ColorFileNames: true,
SideBySide: true,
ShowDiffStats: true,
StartFoldersOpenDepth: -1,
},
}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/ui/panes/filetree/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,24 @@ func (m *Model) rebuildTree() {
t = collapseTree(t)
t, _ = truncateTree(t, 0, 0, 0, m.cfg, m.t.Width())
m.t.SetNodes(t)
if m.cfg.UI.StartFoldersOpenDepth >= 0 {
Comment thread
dlvhdr marked this conversation as resolved.
closeDirsBelow(m.t.Root(), m.cfg.UI.StartFoldersOpenDepth)
}
m.t.SetWidth(m.t.Width())
m.updateStyles()
}

func closeDirsBelow(node *tree.Node, maxOpenDepth int) {
for _, child := range node.ChildNodes() {
closeDirsBelow(child, maxOpenDepth)
}
if _, ok := node.GivenValue().(*dirnode.DirNode); ok {
if node.Depth() > maxOpenDepth {
node.Close()
}
}
}

func buildFullFileTree(files []*gitdiff.File, cfg config.Config) *tree.Node {
t := tree.Root(&dirnode.DirNode{FullPath: "/", Name: constants.RootName})
for _, file := range files {
Expand Down
85 changes: 85 additions & 0 deletions pkg/ui/panes/filetree/filetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"charm.land/bubbles/v2/tree"
"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/dlvhdr/diffnav/pkg/config"
"github.com/dlvhdr/diffnav/pkg/constants"
Expand Down Expand Up @@ -185,3 +186,87 @@ func TestUncollapsableTree(t *testing.T) {
t.Fatalf("expected 13 nodes, but got %d", len(allNodes))
}
}

func TestCloseDirsBelowDepthZero(t *testing.T) {
f, err := os.Open("testdata/multiple_files.diff")
if err != nil {
t.Fatal(err)
}
files, _, err := gitdiff.Parse(f)
if err != nil {
t.Fatal(err)
}

tr := buildFullFileTree(files, config.Config{})
tr = collapseTree(tr)

treeModel := tree.New(nil, 80, 40)
treeModel.SetNodes(tr)

root := treeModel.Root()

allNodesBefore := root.AllNodes()
if len(allNodesBefore) != 4 {
t.Fatalf("expected 4 nodes before closing, but got %d", len(allNodesBefore))
}

closeDirsBelow(root, 0)

if !root.IsOpen() {
t.Fatal("expected root node to remain open")
}

allNodesAfter := root.AllNodes()
if len(allNodesAfter) >= len(allNodesBefore) {
t.Fatalf("expected fewer visible nodes after closing dirs, got %d (before: %d)",
len(allNodesAfter), len(allNodesBefore))
}

for _, node := range allNodesAfter {
if _, ok := node.GivenValue().(*dirnode.DirNode); ok {
if node.Depth() > 0 && node.IsOpen() {
t.Fatalf("expected directory at depth %d to be closed", node.Depth())
}
}
}
}

func TestCloseDirsBelowDepthOne(t *testing.T) {
f, err := os.Open("testdata/gh_dash_pr.diff")
if err != nil {
t.Fatal(err)
}
files, _, err := gitdiff.Parse(f)
if err != nil {
t.Fatal(err)
}

tr := buildFullFileTree(files, config.Config{})
tr = collapseTree(tr)

treeModel := tree.New(nil, 80, 40)
treeModel.SetNodes(tr)

root := treeModel.Root()

closeDirsBelow(root, 1)

if !root.IsOpen() {
t.Fatal("expected root node to remain open")
}

for _, node := range root.ChildNodes() {
if dir, ok := node.GivenValue().(*dirnode.DirNode); ok {
if !node.IsOpen() {
t.Fatalf("expected depth-1 directory %q to be open", dir.Name)
}
for _, child := range node.ChildNodes() {
if _, ok := child.GivenValue().(*dirnode.DirNode); ok {
if child.IsOpen() {
t.Fatalf("expected depth-2 directory to be closed")
}
}
}
}
}
}
Loading