Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
commandsListView.go 1.05 KiB
package views

import (
	"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"
	"google.golang.org/grpc"
)

type CommandListView struct {
	commandsList *tview.List
}

func NewCommandListView() *CommandListView {
	cv := &CommandListView{
		commandsList: tview.NewList(),
	}
	cv.commandsList.
		SetBorder(true).
		SetTitle("Commands")

	return cv
}

func (cv *CommandListView) GetContent() tview.Primitive {
	return cv.commandsList
}

func (cv *CommandListView) GetCommands(app *app.App, rv *ResultAndInputView,
	conn *grpc.ClientConn) {
	//TODO: create own command in grpc -> commands
	cv.commandsList.AddItem("AddPND", "closes the application", '!', func() {
		app.SwitchPage("PND")
	})

	for i, command := range commands.CommandList {
		f := command.Function
		cv.commandsList.
			AddItem(command.Name, command.Description, rune('a'+i), func() {
				r := f(conn)
				rv.SetContent(r)
			})
	}

	cv.commandsList.AddItem("quit", "closes the application", 'q', func() {
		app.Stop()
	})
}