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