home grown item select
This commit is contained in:
parent
fd397ac932
commit
90c86234c4
@ -6,6 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/jroimartin/gocui"
|
"github.com/jroimartin/gocui"
|
||||||
"github.com/wagoodman/docker-image-explorer/filetree"
|
"github.com/wagoodman/docker-image-explorer/filetree"
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileTreeView struct {
|
type FileTreeView struct {
|
||||||
@ -38,9 +40,9 @@ func (view *FileTreeView) Setup(v *gocui.View) error {
|
|||||||
view.view = v
|
view.view = v
|
||||||
view.view.Editable = false
|
view.view.Editable = false
|
||||||
view.view.Wrap = false
|
view.view.Wrap = false
|
||||||
view.view.Highlight = true
|
//view.view.Highlight = true
|
||||||
view.view.SelBgColor = gocui.ColorGreen
|
//view.view.SelBgColor = gocui.ColorGreen
|
||||||
view.view.SelFgColor = gocui.ColorBlack
|
//view.view.SelFgColor = gocui.ColorBlack
|
||||||
view.view.Frame = true
|
view.view.Frame = true
|
||||||
|
|
||||||
// set keybindings
|
// set keybindings
|
||||||
@ -108,7 +110,7 @@ func (view *FileTreeView) CursorDown() error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
view.TreeIndex++
|
view.TreeIndex++
|
||||||
}
|
}
|
||||||
return nil
|
return view.Render()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (view *FileTreeView) CursorUp() error {
|
func (view *FileTreeView) CursorUp() error {
|
||||||
@ -116,9 +118,7 @@ func (view *FileTreeView) CursorUp() error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
view.TreeIndex--
|
view.TreeIndex--
|
||||||
}
|
}
|
||||||
// tmp tmp tmp
|
return view.Render()
|
||||||
view.getAbsPositionNode()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (view *FileTreeView) getAbsPositionNode() (node *filetree.FileNode) {
|
func (view *FileTreeView) getAbsPositionNode() (node *filetree.FileNode) {
|
||||||
@ -184,13 +184,29 @@ func (view *FileTreeView) updateViewTree() {
|
|||||||
}, nil)
|
}, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (view *FileTreeView) KeyHelp() string {
|
||||||
|
control := color.New(color.Bold).SprintFunc()
|
||||||
|
return control("[Space]") + ": Collapse dir " +
|
||||||
|
control("[^A]") + ": Added files " +
|
||||||
|
control("[^R]") + ": Removed files " +
|
||||||
|
control("[^M]") + ": Modified files " +
|
||||||
|
control("[^U]") + ": Unmodified files"
|
||||||
|
}
|
||||||
|
|
||||||
func (view *FileTreeView) Render() error {
|
func (view *FileTreeView) Render() error {
|
||||||
// print the tree to the view
|
// print the tree to the view
|
||||||
renderString := view.ViewTree.String()
|
lines := strings.Split(view.ViewTree.String(), "\n")
|
||||||
view.gui.Update(func(g *gocui.Gui) error {
|
view.gui.Update(func(g *gocui.Gui) error {
|
||||||
view.view.Clear()
|
view.view.Clear()
|
||||||
_, err := fmt.Fprintln(view.view, renderString)
|
for idx, line := range lines {
|
||||||
return err
|
if idx == view.TreeIndex {
|
||||||
|
fmt.Fprintln(view.view, Formatting.Header(line))
|
||||||
|
} else {
|
||||||
|
fmt.Fprintln(view.view, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// todo: should we check error on the view println?
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,9 @@ func (view *LayerView) Setup(v *gocui.View) error {
|
|||||||
// set view options
|
// set view options
|
||||||
view.view = v
|
view.view = v
|
||||||
view.view.Wrap = false
|
view.view.Wrap = false
|
||||||
view.view.Highlight = true
|
//view.view.Highlight = true
|
||||||
view.view.SelBgColor = gocui.ColorGreen
|
//view.view.SelBgColor = gocui.ColorGreen
|
||||||
view.view.SelFgColor = gocui.ColorBlack
|
//view.view.SelFgColor = gocui.ColorBlack
|
||||||
view.view.Frame = false
|
view.view.Frame = false
|
||||||
|
|
||||||
// set keybindings
|
// set keybindings
|
||||||
@ -52,9 +52,16 @@ func (view *LayerView) Setup(v *gocui.View) error {
|
|||||||
func (view *LayerView) Render() error {
|
func (view *LayerView) Render() error {
|
||||||
view.gui.Update(func(g *gocui.Gui) error {
|
view.gui.Update(func(g *gocui.Gui) error {
|
||||||
view.view.Clear()
|
view.view.Clear()
|
||||||
for idx := len(view.Layers) - 1; idx >= 0; idx-- {
|
for revIdx := len(view.Layers) - 1; revIdx >= 0; revIdx-- {
|
||||||
layer := view.Layers[idx]
|
layer := view.Layers[revIdx]
|
||||||
fmt.Fprintln(view.view, layer.String())
|
idx := (len(view.Layers)-1) - revIdx
|
||||||
|
|
||||||
|
if idx == view.LayerIndex {
|
||||||
|
fmt.Fprintln(view.view, Formatting.Header(layer.String()))
|
||||||
|
} else {
|
||||||
|
fmt.Fprintln(view.view, layer.String())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -85,3 +92,7 @@ func (view *LayerView) CursorUp() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (view *LayerView) KeyHelp() string {
|
||||||
|
return "blerg"
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jroimartin/gocui"
|
"github.com/jroimartin/gocui"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StatusView struct {
|
type StatusView struct {
|
||||||
@ -42,10 +43,26 @@ func (view *StatusView) Setup(v *gocui.View) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (view *StatusView) CursorDown() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (view *StatusView) CursorUp() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (view *StatusView) KeyHelp() string {
|
||||||
|
control := color.New(color.Bold).SprintFunc()
|
||||||
|
return control("[^C]") + ": Quit " +
|
||||||
|
control("[^Space]") + ": Switch View "
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (view *StatusView) Render() error {
|
func (view *StatusView) Render() error {
|
||||||
view.gui.Update(func(g *gocui.Gui) error {
|
view.gui.Update(func(g *gocui.Gui) error {
|
||||||
view.view.Clear()
|
view.view.Clear()
|
||||||
fmt.Fprintln(view.view, "[Ctrl+C]: Quit [Ctrl+Space]: Switch View | [Space]: Toggle dir collapse [A]: Added files [R]: Removed files [M]: Modified files [U]: Unmodified files")
|
fmt.Fprintln(view.view, view.KeyHelp() + " | " + Views.lookup[view.gui.CurrentView().Name()].KeyHelp())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
39
ui/ui.go
39
ui/ui.go
@ -6,14 +6,20 @@ import (
|
|||||||
"github.com/jroimartin/gocui"
|
"github.com/jroimartin/gocui"
|
||||||
"github.com/wagoodman/docker-image-explorer/filetree"
|
"github.com/wagoodman/docker-image-explorer/filetree"
|
||||||
"github.com/wagoodman/docker-image-explorer/image"
|
"github.com/wagoodman/docker-image-explorer/image"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
const debug = true
|
const debug = false
|
||||||
|
|
||||||
|
var Formatting struct {
|
||||||
|
Header func(...interface{})(string)
|
||||||
|
}
|
||||||
|
|
||||||
var Views struct {
|
var Views struct {
|
||||||
Tree *FileTreeView
|
Tree *FileTreeView
|
||||||
Layer *LayerView
|
Layer *LayerView
|
||||||
Status *StatusView
|
Status *StatusView
|
||||||
|
lookup map[string]View
|
||||||
}
|
}
|
||||||
|
|
||||||
type View interface {
|
type View interface {
|
||||||
@ -21,14 +27,17 @@ type View interface {
|
|||||||
CursorDown() error
|
CursorDown() error
|
||||||
CursorUp() error
|
CursorUp() error
|
||||||
Render() error
|
Render() error
|
||||||
|
KeyHelp() string
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextView(g *gocui.Gui, v *gocui.View) error {
|
func toggleView(g *gocui.Gui, v *gocui.View) error {
|
||||||
if v == nil || v.Name() == Views.Layer.Name {
|
if v == nil || v.Name() == Views.Layer.Name {
|
||||||
_, err := g.SetCurrentView(Views.Tree.Name)
|
_, err := g.SetCurrentView(Views.Tree.Name)
|
||||||
|
Render()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err := g.SetCurrentView(Views.Layer.Name)
|
_, err := g.SetCurrentView(Views.Layer.Name)
|
||||||
|
Render()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,10 +83,10 @@ func keybindings(g *gocui.Gui) error {
|
|||||||
//if err := g.SetKeybinding("main", gocui.MouseLeft, gocui.ModNone, toggleCollapse); err != nil {
|
//if err := g.SetKeybinding("main", gocui.MouseLeft, gocui.ModNone, toggleCollapse); err != nil {
|
||||||
// return err
|
// return err
|
||||||
//}
|
//}
|
||||||
if err := g.SetKeybinding("side", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
if err := g.SetKeybinding("side", gocui.KeyCtrlSpace, gocui.ModNone, toggleView); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := g.SetKeybinding("main", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
if err := g.SetKeybinding("main", gocui.KeyCtrlSpace, gocui.ModNone, toggleView); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +108,10 @@ func layout(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
Views.Layer.Setup(view)
|
Views.Layer.Setup(view)
|
||||||
|
|
||||||
|
if _, err := g.SetCurrentView(Views.Layer.Name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if view, err := g.SetView(Views.Tree.Name, splitCols, -1, debugCols, maxY-bottomRows); err != nil {
|
if view, err := g.SetView(Views.Tree.Name, splitCols, -1, debugCols, maxY-bottomRows); err != nil {
|
||||||
if err != gocui.ErrUnknownView {
|
if err != gocui.ErrUnknownView {
|
||||||
@ -106,10 +119,6 @@ func layout(g *gocui.Gui) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Views.Tree.Setup(view)
|
Views.Tree.Setup(view)
|
||||||
|
|
||||||
if _, err := g.SetCurrentView(Views.Tree.Name); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if debug {
|
if debug {
|
||||||
if _, err := g.SetView("debug", debugCols, -1, maxX, maxY-bottomRows); err != nil {
|
if _, err := g.SetView("debug", debugCols, -1, maxX, maxY-bottomRows); err != nil {
|
||||||
@ -129,7 +138,14 @@ func layout(g *gocui.Gui) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Render() {
|
||||||
|
for _, view := range Views.lookup {
|
||||||
|
view.Render()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Run(layers []*image.Layer, refTrees []*filetree.FileTree) {
|
func Run(layers []*image.Layer, refTrees []*filetree.FileTree) {
|
||||||
|
Formatting.Header = color.New(color.ReverseVideo, color.Bold).SprintFunc()
|
||||||
|
|
||||||
g, err := gocui.NewGui(gocui.OutputNormal)
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -137,9 +153,16 @@ func Run(layers []*image.Layer, refTrees []*filetree.FileTree) {
|
|||||||
}
|
}
|
||||||
defer g.Close()
|
defer g.Close()
|
||||||
|
|
||||||
|
Views.lookup = make(map[string]View)
|
||||||
|
|
||||||
Views.Layer = NewLayerView("side", g, layers)
|
Views.Layer = NewLayerView("side", g, layers)
|
||||||
|
Views.lookup[Views.Layer.Name] = Views.Layer
|
||||||
|
|
||||||
Views.Tree = NewFileTreeView("main", g, filetree.StackRange(refTrees, 0), refTrees)
|
Views.Tree = NewFileTreeView("main", g, filetree.StackRange(refTrees, 0), refTrees)
|
||||||
|
Views.lookup[Views.Tree.Name] = Views.Tree
|
||||||
|
|
||||||
Views.Status = NewStatusView("status", g)
|
Views.Status = NewStatusView("status", g)
|
||||||
|
Views.lookup[Views.Status.Name] = Views.Status
|
||||||
|
|
||||||
g.Cursor = false
|
g.Cursor = false
|
||||||
//g.Mouse = true
|
//g.Mouse = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user