Skip to content
Snippets Groups Projects
commandsListView.go 1.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • package views
    
    import (
    
    Malte Bauch's avatar
    Malte Bauch committed
    	"code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/app"
    	commands "code.fbi.h-da.de/cocsn/gosdn/cmd/gosdn-tview/grpc"
    
    	"github.com/rivo/tview"
    
    Malte Bauch's avatar
    Malte Bauch committed
    	"google.golang.org/grpc"
    
    //CommandListView is an application view to display all the goSDN commands
    
    type CommandListView struct {
    
    Malte Bauch's avatar
    Malte Bauch committed
    	commandsList *tview.List
    
    //NewCommandListView creates a new CommandListView
    
    Malte Bauch's avatar
    Malte Bauch committed
    func NewCommandListView() *CommandListView {
    
    Malte Bauch's avatar
    Malte Bauch committed
    	cv := &CommandListView{
    
    Malte Bauch's avatar
    Malte Bauch committed
    		commandsList: tview.NewList(),
    
    Malte Bauch's avatar
    Malte Bauch committed
    	}
    
    Malte Bauch's avatar
    Malte Bauch committed
    	cv.commandsList.
    
    		SetBorder(true).
    
    Malte Bauch's avatar
    Malte Bauch committed
    		SetTitle("Commands")
    
    Malte Bauch's avatar
    Malte Bauch committed
    	return cv
    
    //GetContent returns the tview.Primitive belonging to the CommandListView
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (cv *CommandListView) GetContent() tview.Primitive {
    	return cv.commandsList
    
    //GetCommands gets all goSDN commands from a command list and creates new
    //tview.List items for each one of them. The specific gRPC functions are added
    //as tview.Selected() function
    
    Malte Bauch's avatar
    Malte Bauch committed
    func (cv *CommandListView) GetCommands(app *app.App, rv *ResultAndInputView,
    
    	conn *grpc.ClientConn) {
    
    Malte Bauch's avatar
    Malte Bauch committed
    	//TODO: create own command in grpc -> commands
    
    	cv.commandsList.AddItem("addPND", "closes the application", 'a', func() {
    
    		rv.ChangeContentView("addPND")
    		app.SetFocus(rv.GetContent())
    
    Malte Bauch's avatar
    Malte Bauch committed
    	})
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    	for i, command := range commands.CommandList {
    		f := command.Function
    
    Malte Bauch's avatar
    Malte Bauch committed
    		cv.commandsList.
    
    Malte Bauch's avatar
    Malte Bauch committed
    			AddItem(command.Name, command.Description, rune('b'+i), func() {
    
    				r := f(conn)
    				rv.SetContent(r)
    
    				rv.ChangeContentView("result")
    
    	cv.commandsList.AddItem("log", "shows the log of goSDN", 'l', func() {
    
    Malte Bauch's avatar
    Malte Bauch committed
    		rv.ChangeContentView("log")
    	})
    
    
    Malte Bauch's avatar
    Malte Bauch committed
    	cv.commandsList.AddItem("quit", "closes the application", 'q', func() {
    
    Malte Bauch's avatar
    Malte Bauch committed
    		app.Stop()
    	})