1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-01-18 21:59:30 +00:00

tui, renamed slide fields

This commit is contained in:
postmannen 2022-01-17 14:20:03 +01:00
parent 5346221b49
commit 5a52003e5a

94
tui.go
View file

@ -152,7 +152,7 @@ func (t *tui) infoSlide(app *tview.Application) tview.Primitive {
// for all the fields in the struct. If we have forgot'en one // for all the fields in the struct. If we have forgot'en one
// it will create a "no case" field in the console, to easily // it will create a "no case" field in the console, to easily
// detect that a struct field are missing a defenition below. // detect that a struct field are missing a defenition below.
func drawMessageInputFields(p pageMessage, m tuiMessage) { func drawMessageInputFields(p slideMessageEdit, m tuiMessage) {
fmt.Fprintf(p.logForm, " * drawing message input fields\n") fmt.Fprintf(p.logForm, " * drawing message input fields\n")
fieldWidth := 0 fieldWidth := 0
@ -178,11 +178,11 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
values = tmp values = tmp
} }
p.msgInputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1) p.inputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1)
//c.msgForm.AddDropDown(mRefVal.Type().Field(i).Name, values, 0, nil).SetItemPadding(1) //c.msgForm.AddDropDown(mRefVal.Type().Field(i).Name, values, 0, nil).SetItemPadding(1)
case "ToNodes": case "ToNodes":
if m.ToNodes == nil { if m.ToNodes == nil {
p.msgInputForm.AddInputField(fieldName, "", fieldWidth, nil, nil) p.inputForm.AddInputField(fieldName, "", fieldWidth, nil, nil)
continue continue
} }
@ -198,7 +198,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
val2 = fmt.Sprintf("%v,\"%v\"", val2, v) val2 = fmt.Sprintf("%v,\"%v\"", val2, v)
} }
p.msgInputForm.AddInputField(fieldName, val2, fieldWidth, nil, nil) p.inputForm.AddInputField(fieldName, val2, fieldWidth, nil, nil)
} }
case "Method": case "Method":
@ -215,10 +215,10 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
values = tmp values = tmp
} }
p.msgInputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1) p.inputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1)
case "MethodArgs": case "MethodArgs":
if m.MethodArgs == nil { if m.MethodArgs == nil {
p.msgInputForm.AddInputField(fieldName, "", 0, nil, nil) p.inputForm.AddInputField(fieldName, "", 0, nil, nil)
continue continue
} }
@ -234,7 +234,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
val2 = fmt.Sprintf("%v,\"%v\"", val2, v) val2 = fmt.Sprintf("%v,\"%v\"", val2, v)
} }
p.msgInputForm.AddInputField(fieldName, val2, 0, nil, nil) p.inputForm.AddInputField(fieldName, val2, 0, nil, nil)
} }
case "ReplyMethod": case "ReplyMethod":
@ -251,10 +251,10 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
values = tmp values = tmp
} }
p.msgInputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1) p.inputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1)
case "ReplyMethodArgs": case "ReplyMethodArgs":
if m.ReplyMethodArgs == nil { if m.ReplyMethodArgs == nil {
p.msgInputForm.AddInputField(fieldName, "", fieldWidth, nil, nil) p.inputForm.AddInputField(fieldName, "", fieldWidth, nil, nil)
continue continue
} }
@ -270,7 +270,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
val2 = fmt.Sprintf("%v,\"%v\"", val2, v) val2 = fmt.Sprintf("%v,\"%v\"", val2, v)
} }
p.msgInputForm.AddInputField(fieldName, val2, fieldWidth, nil, nil) p.inputForm.AddInputField(fieldName, val2, fieldWidth, nil, nil)
} }
case "ACKTimeout": case "ACKTimeout":
value := 30 value := 30
@ -279,7 +279,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.ACKTimeout value = *m.ACKTimeout
} }
p.msgInputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil) p.inputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil)
case "Retries": case "Retries":
value := 1 value := 1
@ -287,7 +287,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.Retries value = *m.Retries
} }
p.msgInputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil) p.inputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil)
case "ReplyACKTimeout": case "ReplyACKTimeout":
value := 30 value := 30
@ -295,7 +295,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.ReplyACKTimeout value = *m.ReplyACKTimeout
} }
p.msgInputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil) p.inputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil)
case "ReplyRetries": case "ReplyRetries":
value := 1 value := 1
@ -303,7 +303,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.ReplyRetries value = *m.ReplyRetries
} }
p.msgInputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil) p.inputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil)
case "MethodTimeout": case "MethodTimeout":
value := 120 value := 120
@ -311,7 +311,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.MethodTimeout value = *m.MethodTimeout
} }
p.msgInputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil) p.inputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil)
case "ReplyMethodTimeout": case "ReplyMethodTimeout":
value := 120 value := 120
@ -319,7 +319,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.ReplyMethodTimeout value = *m.ReplyMethodTimeout
} }
p.msgInputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil) p.inputForm.AddInputField(fieldName, fmt.Sprintf("%d", value), fieldWidth, validateInteger, nil)
case "Directory": case "Directory":
value := "/some-dir/" value := "/some-dir/"
@ -327,7 +327,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.Directory value = *m.Directory
} }
p.msgInputForm.AddInputField(fieldName, value, fieldWidth, nil, nil) p.inputForm.AddInputField(fieldName, value, fieldWidth, nil, nil)
case "FileName": case "FileName":
value := ".log" value := ".log"
@ -335,7 +335,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
value = *m.FileName value = *m.FileName
} }
p.msgInputForm.AddInputField(fieldName, value, fieldWidth, nil, nil) p.inputForm.AddInputField(fieldName, value, fieldWidth, nil, nil)
case "RelayViaNode": case "RelayViaNode":
// Get nodes from file. // Get nodes from file.
values, err := getNodeNames("nodeslist.cfg") values, err := getNodeNames("nodeslist.cfg")
@ -350,7 +350,7 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
values = tmp values = tmp
} }
p.msgInputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1) p.inputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1)
//c.msgForm.AddDropDown(mRefVal.Type().Field(i).Name, values, 0, nil).SetItemPadding(1) //c.msgForm.AddDropDown(mRefVal.Type().Field(i).Name, values, 0, nil).SetItemPadding(1)
case "RelayReplyMethod": case "RelayReplyMethod":
var v Method var v Method
@ -366,13 +366,13 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
values = tmp values = tmp
} }
p.msgInputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1) p.inputForm.AddDropDown(fieldName, values, 0, nil).SetItemPadding(1)
default: default:
// Add a no definition fields to the form if a a field within the // Add a no definition fields to the form if a a field within the
// struct were missing an action above, so we can easily detect // struct were missing an action above, so we can easily detect
// if there is missing a case action for one of the struct fields. // if there is missing a case action for one of the struct fields.
p.msgInputForm.AddDropDown("error: no case for: "+fieldName, []string{"1", "2"}, 0, nil).SetItemPadding(1) p.inputForm.AddDropDown("error: no case for: "+fieldName, []string{"1", "2"}, 0, nil).SetItemPadding(1)
} }
} }
@ -382,25 +382,25 @@ func drawMessageInputFields(p pageMessage, m tuiMessage) {
// pageMessage is a struct for holding all the main forms and // pageMessage is a struct for holding all the main forms and
// views used in the message slide, so we can easily reference // views used in the message slide, so we can easily reference
// them later in the code. // them later in the code.
type pageMessage struct { type slideMessageEdit struct {
flex *tview.Flex flex *tview.Flex
msgInputForm *tview.Form inputForm *tview.Form
msgOutputForm *tview.TextView outputForm *tview.TextView
logForm *tview.TextView logForm *tview.TextView
saveForm *tview.Form saveForm *tview.Form
} }
// messageSlide is the main function for setting up the slides. // messageSlide is the main function for setting up the slides.
func (t *tui) messageSlide(app *tview.Application) tview.Primitive { func (t *tui) messageSlide(app *tview.Application) tview.Primitive {
p := pageMessage{} p := slideMessageEdit{}
p.msgInputForm = tview.NewForm() p.inputForm = tview.NewForm()
p.msgInputForm.SetBorder(true).SetTitle("Message input").SetTitleAlign(tview.AlignLeft) p.inputForm.SetBorder(true).SetTitle("Message input").SetTitleAlign(tview.AlignLeft)
p.msgOutputForm = tview.NewTextView() p.outputForm = tview.NewTextView()
p.msgOutputForm.SetBorder(true).SetTitle("Message output").SetTitleAlign(tview.AlignLeft) p.outputForm.SetBorder(true).SetTitle("Message output").SetTitleAlign(tview.AlignLeft)
p.msgOutputForm.SetChangedFunc(func() { p.outputForm.SetChangedFunc(func() {
// Will cause the log window to be redrawn as soon as // Will cause the log window to be redrawn as soon as
// new output are detected. // new output are detected.
app.Draw() app.Draw()
@ -423,11 +423,11 @@ func (t *tui) messageSlide(app *tview.Application) tview.Primitive {
p.flex = tview.NewFlex().SetDirection(tview.FlexRow). p.flex = tview.NewFlex().SetDirection(tview.FlexRow).
// Add a flex for the top windows with columns. // Add a flex for the top windows with columns.
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn). AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(p.msgInputForm, 0, 10, false). AddItem(p.inputForm, 0, 10, false).
// Add a new flex for splitting output form horizontally. // Add a new flex for splitting output form horizontally.
AddItem(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
// Add the message output form. // Add the message output form.
AddItem(p.msgOutputForm, 0, 10, false). AddItem(p.outputForm, 0, 10, false).
// Add the save message form. // Add the save message form.
AddItem(p.saveForm, 0, 2, false), AddItem(p.saveForm, 0, 2, false),
0, 10, false), 0, 10, false),
@ -477,16 +477,16 @@ func (t *tui) messageSlide(app *tview.Application) tview.Primitive {
fmt.Fprintf(p.logForm, " * DEBUG: %v\n", m.MethodArgs) fmt.Fprintf(p.logForm, " * DEBUG: %v\n", m.MethodArgs)
// Clear the form. // Clear the form.
p.msgInputForm.Clear(false) p.inputForm.Clear(false)
// Add a self dropdown when selected since it is not a // Add a self dropdown when selected since it is not a
// part of drawing the input fields function. // part of drawing the input fields function.
p.msgInputForm.AddFormItem(messageDropdown) p.inputForm.AddFormItem(messageDropdown)
fmt.Fprintf(p.logForm, " * message read: %v\n", m) fmt.Fprintf(p.logForm, " * message read: %v\n", m)
drawMessageInputFields(p, m) drawMessageInputFields(p, m)
}) })
p.msgInputForm.AddFormItem(messageDropdown) p.inputForm.AddFormItem(messageDropdown)
p.msgInputForm.AddButton("update message dropdown menu", func() { p.inputForm.AddButton("update message dropdown menu", func() {
// TODO: for message message dropdown // TODO: for message message dropdown
fmt.Fprintf(p.logForm, " * Update button pushed\n") fmt.Fprintf(p.logForm, " * Update button pushed\n")
messageMessageValues := getMessageNames(p.logForm) messageMessageValues := getMessageNames(p.logForm)
@ -504,18 +504,18 @@ func (t *tui) messageSlide(app *tview.Application) tview.Primitive {
var saveFileName string var saveFileName string
// Add Buttons below the message fields. Like Generate and Exit. // Add Buttons below the message fields. Like Generate and Exit.
p.msgInputForm. p.inputForm.
// Add a generate button, which when pressed will loop through all the // Add a generate button, which when pressed will loop through all the
// message form items, and if found fill the value into a msg struct, // message form items, and if found fill the value into a msg struct,
// and at last write it to a file. // and at last write it to a file.
AddButton("generate to console", func() { AddButton("generate to console", func() {
p.msgOutputForm.Clear() p.outputForm.Clear()
m := tuiMessage{} m := tuiMessage{}
// Loop trough all the form fields, check the value of each // Loop trough all the form fields, check the value of each
// form field, and add the value to m. // form field, and add the value to m.
for i := 0; i < p.msgInputForm.GetFormItemCount(); i++ { for i := 0; i < p.inputForm.GetFormItemCount(); i++ {
fi := p.msgInputForm.GetFormItem(i) fi := p.inputForm.GetFormItem(i)
label, value := getLabelAndValue(fi) label, value := getLabelAndValue(fi)
switch label { switch label {
@ -606,7 +606,7 @@ func (t *tui) messageSlide(app *tview.Application) tview.Primitive {
// the content for example if we want to save the message to file. // the content for example if we want to save the message to file.
lastGeneratedMessage = msgsIndented lastGeneratedMessage = msgsIndented
_, err = p.msgOutputForm.Write(msgsIndented) _, err = p.outputForm.Write(msgsIndented)
if err != nil { if err != nil {
fmt.Fprintf(p.logForm, "%v : error: write to fh failed: %v\n", time.Now().Format("Mon Jan _2 15:04:05 2006"), err) fmt.Fprintf(p.logForm, "%v : error: write to fh failed: %v\n", time.Now().Format("Mon Jan _2 15:04:05 2006"), err)
} }
@ -617,7 +617,7 @@ func (t *tui) messageSlide(app *tview.Application) tview.Primitive {
app.Stop() app.Stop()
}) })
app.SetFocus(p.msgInputForm) app.SetFocus(p.inputForm)
p.saveForm. p.saveForm.
AddInputField("FileName", "", 40, nil, func(text string) { AddInputField("FileName", "", 40, nil, func(text string) {
@ -669,13 +669,13 @@ func (t *tui) console(app *tview.Application) tview.Primitive {
// pageMessage is a struct for holding all the main forms and // pageMessage is a struct for holding all the main forms and
// views used in the message slide, so we can easily reference // views used in the message slide, so we can easily reference
// them later in the code. // them later in the code.
type pageMessage struct { type slideConsole struct {
flex *tview.Flex flex *tview.Flex
selectForm *tview.Form selectForm *tview.Form
outputForm *tview.TextView outputForm *tview.TextView
} }
p := pageMessage{} p := slideConsole{}
p.selectForm = tview.NewForm() p.selectForm = tview.NewForm()
p.selectForm.SetBorder(true).SetTitle("select").SetTitleAlign(tview.AlignLeft) p.selectForm.SetBorder(true).SetTitle("select").SetTitleAlign(tview.AlignLeft)