Skip to content

Commit

Permalink
added multilayer select with mode toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
wagoodman committed Jun 25, 2018
1 parent 08fd010 commit e01bf10
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
43 changes: 19 additions & 24 deletions ui/filetreeview.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ui

import (
"errors"
"fmt"

"github.com/jroimartin/gocui"
Expand All @@ -27,9 +26,8 @@ type FileTreeView struct {
ViewTree *filetree.FileTree
RefTrees []*filetree.FileTree
HiddenDiffTypes []bool
CompareMode CompareType
CompareStartIndex int
CompareStopIndex int
TreeIndex int

}

func NewFileTreeView(name string, gui *gocui.Gui, tree *filetree.FileTree, refTrees []*filetree.FileTree) (treeview *FileTreeView) {
Expand All @@ -41,7 +39,6 @@ func NewFileTreeView(name string, gui *gocui.Gui, tree *filetree.FileTree, refTr
treeview.ModelTree = tree
treeview.RefTrees = refTrees
treeview.HiddenDiffTypes = make([]bool, 4)
treeview.CompareMode = CompareLayer

return treeview
}
Expand Down Expand Up @@ -94,13 +91,17 @@ func (view *FileTreeView) Setup(v *gocui.View, header *gocui.View) error {
return nil
}

func (view *FileTreeView) setLayer(layerIndex int) error {
if layerIndex > len(view.RefTrees)-1 {
return errors.New(fmt.Sprintf("Invalid layer index given: %d of %d", layerIndex, len(view.RefTrees)-1))


func (view *FileTreeView) setTreeByLayer(bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int) error {
//if stopIdx > len(view.RefTrees)-1 {
// return errors.New(fmt.Sprintf("Invalid layer index given: %d of %d", stopIdx, len(view.RefTrees)-1))
//}
newTree := filetree.StackRange(view.RefTrees, bottomTreeStart, bottomTreeStop)

for idx := topTreeStart; idx <= topTreeStop; idx++ {
newTree.Compare(view.RefTrees[idx])
}
view.CompareStopIndex = layerIndex
newTree := filetree.StackRange(view.RefTrees, view.CompareStartIndex, view.CompareStopIndex-1)
newTree.Compare(view.RefTrees[view.CompareStopIndex])

// preserve view state on copy
visitor := func(node *filetree.FileNode) error {
Expand All @@ -112,14 +113,8 @@ func (view *FileTreeView) setLayer(layerIndex int) error {
}
view.ModelTree.VisitDepthChildFirst(visitor, nil)

if debug {
v, _ := view.gui.View("debug")
v.Clear()
_, _ = fmt.Fprintln(v, view.RefTrees[view.CompareStopIndex])
}

view.view.SetCursor(0, 0)
view.CompareStopIndex = 0
view.TreeIndex = 0
view.ModelTree = newTree
view.updateViewTree()
return view.Render()
Expand All @@ -130,16 +125,16 @@ func (view *FileTreeView) CursorDown() error {
// to let us know what is a valid bounds (i.e. when it hits an empty line)
err := CursorDown(view.gui, view.view)
if err == nil {
view.CompareStopIndex++
view.TreeIndex++
}
return view.Render()
}

func (view *FileTreeView) CursorUp() error {
if view.CompareStopIndex > 0 {
if view.TreeIndex > 0 {
err := CursorUp(view.gui, view.view)
if err == nil {
view.CompareStopIndex--
view.TreeIndex--
}
}
return view.Render()
Expand All @@ -151,7 +146,7 @@ func (view *FileTreeView) getAbsPositionNode() (node *filetree.FileNode) {
var dfsCounter int

visiter = func(curNode *filetree.FileNode) error {
if dfsCounter == view.CompareStopIndex {
if dfsCounter == view.TreeIndex {
node = curNode
}
dfsCounter++
Expand Down Expand Up @@ -181,7 +176,7 @@ func (view *FileTreeView) toggleShowDiffType(diffType filetree.DiffType) error {
view.HiddenDiffTypes[diffType] = !view.HiddenDiffTypes[diffType]

view.view.SetCursor(0, 0)
view.CompareStopIndex = 0
view.TreeIndex = 0
view.updateViewTree()
return view.Render()
}
Expand Down Expand Up @@ -217,7 +212,7 @@ func (view *FileTreeView) Render() error {
view.gui.Update(func(g *gocui.Gui) error {
view.view.Clear()
for idx, line := range lines {
if idx == view.CompareStopIndex {
if idx == view.TreeIndex {
fmt.Fprintln(view.view, Formatting.StatusBar(vtclean.Clean(line, false)))
} else {
fmt.Fprintln(view.view, line)
Expand Down
56 changes: 49 additions & 7 deletions ui/layerview.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type LayerView struct {
header *gocui.View
LayerIndex int
Layers []*image.Layer
CompareMode CompareType
CompareStartIndex int
}

func NewLayerView(name string, gui *gocui.Gui, layers []*image.Layer) (layerview *LayerView) {
Expand All @@ -24,6 +26,7 @@ func NewLayerView(name string, gui *gocui.Gui, layers []*image.Layer) (layerview
layerview.Name = name
layerview.gui = gui
layerview.Layers = layers
layerview.CompareMode = CompareLayer

return layerview
}
Expand Down Expand Up @@ -59,16 +62,53 @@ func (view *LayerView) Setup(v *gocui.View, header *gocui.View) error {
}


headerStr := fmt.Sprintf(image.LayerFormat, "Image ID", "Size", "Command")
headerStr := fmt.Sprintf("C "+image.LayerFormat, "Image ID", "Size", "Command")
fmt.Fprintln(view.header, Formatting.Header(vtclean.Clean(headerStr, false)))

return view.Render()
}

func (view *LayerView) setCompareMode(compareMode CompareType) error {
Views.Tree.CompareMode = compareMode
view.CompareMode = compareMode
view.Render()
return Views.Tree.setLayer(Views.Tree.CompareStopIndex)
return Views.Tree.setTreeByLayer(view.getCompareIndexes())
}

func (view *LayerView) getCompareIndexes() (bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int) {
bottomTreeStart = view.CompareStartIndex
topTreeStop = view.LayerIndex

if view.LayerIndex == view.CompareStartIndex {
bottomTreeStop = view.LayerIndex
topTreeStart = view.LayerIndex
} else if view.CompareMode == CompareLayer {
bottomTreeStop = view.LayerIndex -1
topTreeStart = view.LayerIndex
} else {
bottomTreeStop = view.CompareStartIndex
topTreeStart = view.CompareStartIndex+1
}

return bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop
}

func (view *LayerView) renderCompareBar(layerIdx int) string {
bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop := view.getCompareIndexes()
result := " "

if debug {
v, _ := view.gui.View("debug")
v.Clear()
_, _ = fmt.Fprintf(v, "bStart: %d bStop: %d tStart: %d tStop: %d", bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop)
}

if layerIdx >= bottomTreeStart && layerIdx <= bottomTreeStop {
result = Formatting.CompareBottom(" ")
}
if layerIdx >= topTreeStart && layerIdx <= topTreeStop {
result = Formatting.CompareTop(" ")
}
return result
}

func (view *LayerView) Render() error {
Expand All @@ -84,10 +124,12 @@ func (view *LayerView) Render() error {
layerStr = fmt.Sprintf(image.LayerFormat, layer.History.ID[0:25], "", "FROM "+layer.Id())
}

compareBar := view.renderCompareBar(idx)

if idx == view.LayerIndex {
fmt.Fprintln(view.view, Formatting.StatusBar(layerStr))
fmt.Fprintln(view.view, compareBar + " " + Formatting.StatusBar(layerStr))
} else {
fmt.Fprintln(view.view, layerStr)
fmt.Fprintln(view.view, compareBar + " " + layerStr)
}

}
Expand All @@ -102,8 +144,8 @@ func (view *LayerView) CursorDown() error {
err := CursorDown(view.gui, view.view)
if err == nil {
view.LayerIndex++
Views.Tree.setTreeByLayer(view.getCompareIndexes())
view.Render()
Views.Tree.setLayer(view.LayerIndex)
}
}
return nil
Expand All @@ -114,8 +156,8 @@ func (view *LayerView) CursorUp() error {
err := CursorUp(view.gui, view.view)
if err == nil {
view.LayerIndex--
Views.Tree.setTreeByLayer(view.getCompareIndexes())
view.Render()
Views.Tree.setLayer(view.LayerIndex)
}
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var Formatting struct {
Header func(...interface{})(string)
StatusBar func(...interface{})(string)
Control func(...interface{})(string)
CompareTop func(...interface{})(string)
CompareBottom func(...interface{})(string)
}

var Views struct {
Expand Down Expand Up @@ -170,6 +172,8 @@ func Run(layers []*image.Layer, refTrees []*filetree.FileTree) {
Formatting.StatusBar = color.New(color.ReverseVideo, color.Bold).SprintFunc()
Formatting.Header = color.New(color.Bold).SprintFunc()
Formatting.Control = color.New(color.Bold).SprintFunc()
Formatting.CompareTop = color.New(color.BgMagenta).SprintFunc()
Formatting.CompareBottom = color.New(color.BgGreen).SprintFunc()

g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
Expand Down

0 comments on commit e01bf10

Please sign in to comment.