Commit 2ec300dd authored by elfgzp's avatar elfgzp
Browse files

feat: add render options

parent 5bd35cbe
Showing with 150 additions and 9 deletions
+150 -9
......@@ -4,5 +4,6 @@ import "github.com/TNK-Studio/lazykube/pkg/app"
func main() {
lazykube := app.NewApp()
defer lazykube.Stop()
lazykube.Run()
}
......@@ -3,6 +3,7 @@ module github.com/TNK-Studio/lazykube
go 1.14
require (
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/jroimartin/gocui v0.4.0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 // indirect
......
......@@ -4,6 +4,7 @@ import (
"github.com/TNK-Studio/lazykube/pkg/app/panel"
"github.com/TNK-Studio/lazykube/pkg/config"
"github.com/TNK-Studio/lazykube/pkg/gui"
"github.com/TNK-Studio/lazykube/pkg/utils"
"github.com/jroimartin/gocui"
)
......@@ -14,6 +15,7 @@ type App struct {
Deployment *gui.View
Pod *gui.View
Detail *gui.View
Option *gui.View
Gui *gui.Gui
}
......@@ -25,6 +27,7 @@ func NewApp() *App {
Deployment: panel.Deployment,
Pod: panel.Pod,
Detail: panel.Detail,
Option: panel.Option,
}
//Todo: add app config
......@@ -42,7 +45,10 @@ func NewApp() *App {
app.Deployment,
app.Pod,
app.Detail,
app.Option,
)
app.Gui.Render = app.Render
app.Gui.RenderOptions = app.RenderOptions
return app
}
......@@ -50,6 +56,33 @@ func (app *App) Run() {
app.Gui.Run()
}
func (app *App) Stop() {
app.Gui.Close()
}
func (app *App) Render(gui *gui.Gui) error {
if gui.CurrentView() == nil {
_, err := gui.GetView(app.Namespace.Name)
if err != nil {
return nil
}
if err:= gui.SwitchFocus("", app.Namespace.Name, false); err != nil {
return err
}
}
return nil
}
func (app *App) RenderOptions(gui *gui.Gui) error {
return gui.RenderString(
app.Option.Name,
utils.OptionsMapToString(
map[string]string{
"PgUp/PgDn": "scroll",
"← → ↑ ↓": "navigate",
"esc/q": "close",
}),
)
}
package panel
import (
"github.com/TNK-Studio/lazykube/pkg/gui"
"github.com/jroimartin/gocui"
)
var (
Option *gui.View
)
func init() {
Option = &gui.View{
Name: "option",
DimensionFunc: func(gui *gui.Gui, view *gui.View) (int, int, int, int) {
maxWidth, maxHeight := gui.Size()
return 0, maxHeight - 2, maxWidth / 3, maxHeight
},
NoFrame: true,
FgColor: gocui.ColorBlue,
}
}
\ No newline at end of file
......@@ -25,7 +25,7 @@ func reactiveHeight(gui *gui.Gui, view *gui.View) int {
"service": space / tallPanels,
"deployment": space / tallPanels,
"pod": space / tallPanels,
"options": 1,
"option": 1,
}
//if maxHeight < 28 {
// defaultHeight := 3
......@@ -35,9 +35,10 @@ func reactiveHeight(gui *gui.Gui, view *gui.View) int {
// vHeights = map[string]int{
// "clusterInfo": defaultHeight,
// "namespace": defaultHeight,
// "images": defaultHeight,
// "volumes": defaultHeight,
// "options": defaultHeight,
// "service": defaultHeight,
// "deployment": defaultHeight,
// "pod": defaultHeight,
// "option": defaultHeight,
// }
// //if gui.DockerCommand.InDockerComposeProject {
// // vHeights["services"] = defaultHeight
......
......@@ -21,3 +21,4 @@ type Action struct {
Handler func(gui *Gui) func(*gocui.Gui, *gocui.View) error
Mod gocui.Modifier
}
......@@ -2,6 +2,7 @@ package gui
import (
"github.com/TNK-Studio/lazykube/pkg/config"
"github.com/golang-collections/collections/stack"
"github.com/jroimartin/gocui"
"log"
"time"
......@@ -10,6 +11,10 @@ import (
type Gui struct {
State State
Render func(gui *Gui) error
RenderOptions func(gui *Gui) error
// History of focused views name.
previousViews *stack.Stack
g *gocui.Gui
views []*View
......@@ -19,7 +24,8 @@ type Gui struct {
func NewGui(config config.GuiConfig, views ...*View) *Gui {
gui := &Gui{
State: &StateMap{state: make(map[string]interface{}, 0)},
State: &StateMap{state: make(map[string]interface{}, 0)},
previousViews: stack.New(),
}
gui.views = make([]*View, 0)
g, err := gocui.NewGui(gocui.OutputNormal)
......@@ -117,6 +123,10 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}
if err := gui.renderOptions(); err != nil {
return err
}
return nil
}
......@@ -291,3 +301,70 @@ func (gui *Gui) getView(name string) *View {
}
return nil
}
func (gui *Gui) popPreviousView() string {
if gui.previousViews.Len() > 0 {
return gui.previousViews.Pop().(string)
}
return ""
}
func (gui *Gui) peekPreviousView() string {
if gui.previousViews.Len() > 0 {
return gui.previousViews.Peek().(string)
}
return ""
}
func (gui *Gui) pushPreviousView(name string) {
gui.previousViews.Push(name)
}
func (gui *Gui) FocusView(name string) error {
if _, err := gui.g.SetCurrentView(name); err != nil {
return err
}
if _, err := gui.g.SetViewOnTop(name); err != nil {
return err
}
return nil
}
// pass in oldView = nil if you don't want to be able to return to your old view
// TODO: move some of this logic into our onFocusLost and onFocus hooks
func (gui *Gui) SwitchFocus(oldViewName, newViewName string, returning bool) error {
// we assume we'll never want to return focus to a popup panel i.e.
// we should never stack popup panels
//if oldView != nil && !gui.isPopupPanel(oldView.Name()) && !returning {
// gui.pushPreviousView(oldView.Name())
//}
if oldViewName != "" && !returning {
gui.pushPreviousView(oldViewName)
}
//gui.Log.Info("setting highlight to true for view " + newView.Name())
//gui.Log.Info("new focused view is " + newView.Name())
if err := gui.FocusView(newViewName); err != nil {
return err
}
//g.Cursor = newView.Editable
//return gui.newLineFocused(newView)
return nil
}
func (gui *Gui) renderOptions() error {
currentView := gui.CurrentView()
if currentView != nil && currentView.RenderOptions != nil {
return currentView.RenderOptions(gui, currentView)
}
if gui.RenderOptions != nil {
return gui.RenderOptions(gui)
}
return nil
}
\ No newline at end of file
......@@ -13,8 +13,8 @@ var (
func init() {
NotEnoughSpace = &View{
Name: "notEnoughSpace",
Title: "Not enough space to render.",
Name: "notEnoughSpace",
Title: "Not enough space to render.",
DimensionFunc: func(gui *Gui, view *View) (int, int, int, int) {
maxWidth, maxHeight := gui.Size()
return 0, 0, maxWidth - 1, maxHeight - 1
......@@ -31,9 +31,11 @@ type View struct {
Autoscroll bool
IgnoreCarriageReturns bool
Highlight bool
NoFrame bool
FgColor gocui.Attribute
Render func(gui *Gui, view *View) error
Render func(gui *Gui, view *View) error
RenderOptions func(gui *Gui, view *View) error
DimensionFunc DimensionFunc
......@@ -55,11 +57,12 @@ func (view *View) InitView() {
view.v.Editable = view.Editable
view.v.Autoscroll = view.Autoscroll
view.v.Highlight = view.Highlight
view.v.Frame = !view.NoFrame
view.v.FgColor = view.FgColor
}
}
func (view *View) BindGui (gui *Gui) {
func (view *View) BindGui(gui *Gui) {
view.gui = gui
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment