From e01bf10273a9aa2d9ceaa13d2e19f14acaeb7f75 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 25 Jun 2018 08:45:55 -0400 Subject: [PATCH] added multilayer select with mode toggle --- ui/filetreeview.go | 43 ++++++++++++++++------------------- ui/layerview.go | 56 ++++++++++++++++++++++++++++++++++++++++------ ui/ui.go | 4 ++++ 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/ui/filetreeview.go b/ui/filetreeview.go index 5f955929..98103f70 100644 --- a/ui/filetreeview.go +++ b/ui/filetreeview.go @@ -1,7 +1,6 @@ package ui import ( - "errors" "fmt" "github.com/jroimartin/gocui" @@ -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) { @@ -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 } @@ -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 { @@ -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() @@ -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() @@ -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++ @@ -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() } @@ -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) diff --git a/ui/layerview.go b/ui/layerview.go index f4eefa95..b5ba5613 100644 --- a/ui/layerview.go +++ b/ui/layerview.go @@ -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) { @@ -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 } @@ -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 { @@ -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) } } @@ -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 @@ -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 diff --git a/ui/ui.go b/ui/ui.go index 8b58eb34..33493332 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -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 { @@ -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 {