Skip to content

Commit

Permalink
drive view options from configuration (closes #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Nov 6, 2018
1 parent a95c45e commit 41e1685
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
23 changes: 18 additions & 5 deletions README.md
Expand Up @@ -122,26 +122,39 @@ log:
path: ./dive.log
level: info

# note: you can specify multiple bindings by separating values with a comma.
# note: UI hinting is derived from the first binding
# Note: you can specify multiple bindings by separating values with a comma.
# Note: UI hinting is derived from the first binding
keybinding:
# global bindings
# Global bindings
quit: ctrl+c
toggle-view: tab, ctrl+space
filter-files: ctrl+f, ctrl+slash

# layer view specific bindings
# Layer view specific bindings
compare-all: ctrl+a
compare-layer: ctrl+l

# file view specific bindings
# File view specific bindings
toggle-collapse-dir: space
toggle-added-files: ctrl+a
toggle-removed-files: ctrl+r
toggle-modified-files: ctrl+m
toggle-unmodified-files: ctrl+u
page-up: pgup
page-down: pgdn

diff:
# You can change the default files show in the filetree (right pane). All diff types are shown by default.
hide:
- added
- removed
- changed
- unchanged

layer:
# Enable showing all changes from this layer and ever previous layer
show-aggregated-changes: false

```

dive will search for configs in the following locations:
Expand Down
9 changes: 6 additions & 3 deletions cmd/root.go
Expand Up @@ -80,14 +80,14 @@ func initConfig() {
viper.SetDefault("log.level", log.InfoLevel.String())
viper.SetDefault("log.path", "./dive.log")
viper.SetDefault("log.enabled", true)
// status view / global
// keybindings: status view / global
viper.SetDefault("keybinding.quit", "ctrl+c")
viper.SetDefault("keybinding.toggle-view", "tab, ctrl+space")
viper.SetDefault("keybinding.filter-files", "ctrl+f, ctrl+slash")
// layer view
// keybindings: layer view
viper.SetDefault("keybinding.compare-all", "ctrl+a")
viper.SetDefault("keybinding.compare-layer", "ctrl+l")
// filetree view
// keybindings: filetree view
viper.SetDefault("keybinding.toggle-collapse-dir", "space")
viper.SetDefault("keybinding.toggle-added-files", "ctrl+a")
viper.SetDefault("keybinding.toggle-removed-files", "ctrl+r")
Expand All @@ -96,6 +96,9 @@ func initConfig() {
viper.SetDefault("keybinding.page-up", "pgup")
viper.SetDefault("keybinding.page-down", "pgdn")

viper.SetDefault("diff.hide", "")
viper.SetDefault("layer.show-aggregated-changes", false)

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
Expand Down
17 changes: 17 additions & 0 deletions ui/filetreeview.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/wagoodman/dive/utils"
"regexp"
"strings"

Expand Down Expand Up @@ -55,6 +56,22 @@ func NewFileTreeView(name string, gui *gocui.Gui, tree *filetree.FileTree, refTr
treeView.RefTrees = refTrees
treeView.HiddenDiffTypes = make([]bool, 4)

hiddenTypes := viper.GetStringSlice("diff.hide")
for _, hType := range hiddenTypes {
switch t := strings.ToLower(hType); t {
case "added":
treeView.HiddenDiffTypes[filetree.Added] = true
case "removed":
treeView.HiddenDiffTypes[filetree.Removed] = true
case "changed":
treeView.HiddenDiffTypes[filetree.Changed] = true
case "unchanged":
treeView.HiddenDiffTypes[filetree.Unchanged] = true
default:
utils.PrintAndExit(fmt.Sprintf("unknown diff.hide value: %s", t))
}
}

treeView.keybindingToggleCollapse = getKeybindings(viper.GetString("keybinding.toggle-collapse-dir"))
treeView.keybindingToggleAdded = getKeybindings(viper.GetString("keybinding.toggle-added-files"))
treeView.keybindingToggleRemoved = getKeybindings(viper.GetString("keybinding.toggle-removed-files"))
Expand Down
11 changes: 10 additions & 1 deletion ui/layerview.go
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"fmt"
"github.com/spf13/viper"
"github.com/wagoodman/dive/utils"

"github.com/dustin/go-humanize"
"github.com/jroimartin/gocui"
Expand Down Expand Up @@ -35,7 +36,15 @@ func NewLayerView(name string, gui *gocui.Gui, layers []*image.Layer) (layerView
layerView.Name = name
layerView.gui = gui
layerView.Layers = layers
layerView.CompareMode = CompareLayer

switch mode := viper.GetBool("layer.show-aggregated-changes"); mode {
case true:
layerView.CompareMode = CompareAll
case false:
layerView.CompareMode = CompareLayer
default:
utils.PrintAndExit(fmt.Sprintf("unknown layer.show-aggregated-changes value: %s", mode))
}

layerView.keybindingCompareAll = getKeybindings(viper.GetString("keybinding.compare-all"))
layerView.keybindingCompareLayer = getKeybindings(viper.GetString("keybinding.compare-layer"))
Expand Down
3 changes: 3 additions & 0 deletions ui/ui.go
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/viper"
"github.com/wagoodman/dive/filetree"
"github.com/wagoodman/dive/image"
"github.com/wagoodman/dive/utils"
"log"
)

Expand Down Expand Up @@ -313,6 +314,7 @@ func Run(layers []*image.Layer, refTrees []*filetree.FileTree, efficiency float6
if err != nil {
log.Panicln(err)
}
utils.SetUi(g)
defer g.Close()

Views.lookup = make(map[string]View)
Expand Down Expand Up @@ -347,4 +349,5 @@ func Run(layers []*image.Layer, refTrees []*filetree.FileTree, efficiency float6
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
utils.Exit(0)
}
19 changes: 19 additions & 0 deletions utils/exit.go
@@ -1,16 +1,35 @@
package utils

import (
"fmt"
"github.com/jroimartin/gocui"
"github.com/k0kubun/go-ansi"
"github.com/sirupsen/logrus"
"os"
)

var ui *gocui.Gui

func SetUi(g *gocui.Gui) {
ui = g
}

func PrintAndExit(args ...interface{}) {
logrus.Println(args...)
Cleanup()
fmt.Println(args...)
os.Exit(1)
}

// Note: this should only be used when exiting from non-gocui code
func Exit(rc int) {
Cleanup()
os.Exit(rc)
}

func Cleanup() {
if ui != nil {
ui.Close()
}
ansi.CursorShow()
}

0 comments on commit 41e1685

Please sign in to comment.