Skip to content

Commit

Permalink
added configuration + custom keybinding support
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Nov 1, 2018
1 parent 4b6ff48 commit 6f00572
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 57 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,47 @@ For docker in windows (does not support pulling images yet):
docker run --rm -it -v //var/run/docker.sock:/var/run/docker.sock wagoodman/dive:latest <dive arguments...>
```

## KeyBindings

Key Binding | Description
-------------------------------------------|---------------------------------------------------------
<kbd>Ctrl + C</kbd> | Exit
<kbd>Tab</kbd> or <kbd>Ctrl + Space</kbd> | Switch between the layer and filetree views
<kbd>Ctrl + F</kbd> | Filter files
<kbd>Ctrl + A</kbd> | Layer view: see aggregated image modifications
<kbd>Ctrl + L</kbd> | Layer view: see current layer modifications
<kbd>Space</kbd> | Filetree view: collapse/uncollapse a directory
<kbd>Ctrl + A</kbd> | Filetree view: show/hide added files
<kbd>Ctrl + R</kbd> | Filetree view: show/hide removed files
<kbd>Ctrl + M</kbd> | Filetree view: show/hide modified files
<kbd>Ctrl + U</kbd> | Filetree view: show/hide unmodified files
<kbd>PageUp</kbd> | Filetree view: scroll up a page
<kbd>PageDown</kbd> | Filetree view: scroll down a page

## Configuration

No configuration is necessary, however, you can create a `~/.dive.yaml` file and override values:
```yaml
log:
enabled: true
path: ./dive.log
level: info

# note: you can specify multiple bindings by separating values with a comma. UI hinting shows the first binding.
keybinding:
# global bindings
quit: ctrl+c
toggle-view: tab, ctrl+space
filter-files: ctrl+f, ctrl+slash
# layer view specific bindings
compare-all: ctrl+a
compare-layer: ctrl+l
# 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
```
53 changes: 42 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/wagoodman/dive/utils"
"io/ioutil"
"os"

"github.com/k0kubun/go-ansi"
Expand Down Expand Up @@ -38,8 +39,7 @@ func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(initLogging)

// TODO: add config options
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml)")

rootCmd.PersistentFlags().BoolP("version", "v", false, "display version number")
}
Expand All @@ -62,6 +62,25 @@ func initConfig() {
viper.SetConfigName(".dive")
}

viper.SetDefault("log.level", log.InfoLevel.String())
viper.SetDefault("log.path", "./dive.log")
viper.SetDefault("log.enabled", true)
// 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
viper.SetDefault("keybinding.compare-all", "ctrl+a")
viper.SetDefault("keybinding.compare-layer", "ctrl+l")
// 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")
viper.SetDefault("keybinding.toggle-modified-files", "ctrl+m")
viper.SetDefault("keybinding.toggle-unchanged-files", "ctrl+u")
viper.SetDefault("keybinding.page-up", "pgup")
viper.SetDefault("keybinding.page-down", "pgdn")

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

// If a config file is found, read it in.
Expand All @@ -70,21 +89,33 @@ func initConfig() {
}
}

// initLogging sets up the loggin object with a formatter and location
// initLogging sets up the logging object with a formatter and location
func initLogging() {
// TODO: clean this up and make more configurable
var filename string = "dive.log"
// create the log file if doesn't exist. And append to it if it already exists.
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)

if viper.GetBool("log.enabled") == false {
log.SetOutput(ioutil.Discard)
}

logFileObj, err := os.OpenFile(viper.GetString("log.path"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

Formatter := new(log.TextFormatter)
Formatter.DisableTimestamp = true
log.SetFormatter(Formatter)
log.SetLevel(log.DebugLevel)

level, err := log.ParseLevel(viper.GetString("log.level"))
if err != nil {
// cannot open log file. Logging to stderr
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
}
log.SetLevel(level)

if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
log.SetOutput(f)
log.SetOutput(logFileObj)
}

log.Debug("Starting Dive...")
}
70 changes: 51 additions & 19 deletions ui/filetreeview.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"regexp"
"strings"

Expand Down Expand Up @@ -33,6 +34,14 @@ type FileTreeView struct {
bufferIndex uint
bufferIndexUpperBound uint
bufferIndexLowerBound uint

keybindingToggleCollapse []Key
keybindingToggleAdded []Key
keybindingToggleRemoved []Key
keybindingToggleModified []Key
keybindingToggleUnchanged []Key
keybindingPageDown []Key
keybindingPageUp []Key
}

// NewFileTreeView creates a new view object attached the the global [gocui] screen object.
Expand All @@ -46,6 +55,14 @@ func NewFileTreeView(name string, gui *gocui.Gui, tree *filetree.FileTree, refTr
treeView.RefTrees = refTrees
treeView.HiddenDiffTypes = make([]bool, 4)

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"))
treeView.keybindingToggleModified = getKeybindings(viper.GetString("keybinding.toggle-modified-files"))
treeView.keybindingToggleUnchanged = getKeybindings(viper.GetString("keybinding.toggle-unchanged-files"))
treeView.keybindingPageUp = getKeybindings(viper.GetString("keybinding.page-up"))
treeView.keybindingPageDown = getKeybindings(viper.GetString("keybinding.page-down"))

return treeView
}

Expand Down Expand Up @@ -76,26 +93,41 @@ func (view *FileTreeView) Setup(v *gocui.View, header *gocui.View) error {
if err := view.gui.SetKeybinding(view.Name, gocui.KeyArrowRight, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.CursorRight() }); err != nil {
return err
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeyPgdn, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.PageDown() }); err != nil {
return err

for _, key := range view.keybindingPageUp {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.PageUp() }); err != nil {
return err
}
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeyPgup, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.PageUp() }); err != nil {
return err
for _, key := range view.keybindingPageDown {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.PageDown() }); err != nil {
return err
}
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeySpace, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.toggleCollapse() }); err != nil {
return err
for _, key := range view.keybindingToggleCollapse {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.toggleCollapse() }); err != nil {
return err
}
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeyCtrlA, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Added) }); err != nil {
return err
for _, key := range view.keybindingToggleAdded {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Added) }); err != nil {
return err
}
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeyCtrlR, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Removed) }); err != nil {
return err
for _, key := range view.keybindingToggleRemoved {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Removed) }); err != nil {
return err
}
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeyCtrlM, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Changed) }); err != nil {
return err
for _, key := range view.keybindingToggleModified {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Changed) }); err != nil {
return err
}
}
if err := view.gui.SetKeybinding(view.Name, gocui.KeyCtrlU, gocui.ModNone, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Unchanged) }); err != nil {
return err
for _, key := range view.keybindingToggleUnchanged {
if err := view.gui.SetKeybinding("", key.value, key.modifier, func(*gocui.Gui, *gocui.View) error { return view.toggleShowDiffType(filetree.Unchanged) }); err != nil {
return err
}
}

view.bufferIndexLowerBound = 0
Expand Down Expand Up @@ -494,9 +526,9 @@ func (view *FileTreeView) Render() error {

// KeyHelp indicates all the possible actions a user can take while the current pane is selected.
func (view *FileTreeView) KeyHelp() string {
return renderStatusOption("Space", "Collapse dir", false) +
renderStatusOption("^A", "Added files", !view.HiddenDiffTypes[filetree.Added]) +
renderStatusOption("^R", "Removed files", !view.HiddenDiffTypes[filetree.Removed]) +
renderStatusOption("^M", "Modified files", !view.HiddenDiffTypes[filetree.Changed]) +
renderStatusOption("^U", "Unmodified files", !view.HiddenDiffTypes[filetree.Unchanged])
return renderStatusOption(view.keybindingToggleCollapse[0].String(), "Collapse dir", false) +
renderStatusOption(view.keybindingToggleAdded[0].String(), "Added files", !view.HiddenDiffTypes[filetree.Added]) +
renderStatusOption(view.keybindingToggleRemoved[0].String(), "Removed files", !view.HiddenDiffTypes[filetree.Removed]) +
renderStatusOption(view.keybindingToggleModified[0].String(), "Modified files", !view.HiddenDiffTypes[filetree.Changed]) +
renderStatusOption(view.keybindingToggleUnchanged[0].String(), "Unmodified files", !view.HiddenDiffTypes[filetree.Unchanged])
}
Loading

0 comments on commit 6f00572

Please sign in to comment.