refactor: changed all models for the tui to be in a proper tree structure

All tui elemnts should now be children models to a parent model where
applicable. This should eliminate the weird key regristration issues
seen before and now adhere to proper bubbletea standards
This commit is contained in:
Slug-Boi
2024-11-11 12:57:14 +01:00
parent 56cf60e071
commit c8e5d87e3f
2 changed files with 81 additions and 85 deletions
+79 -75
View File
@@ -28,7 +28,7 @@ var (
excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude")) excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude"))
) )
var removeButton bool var tempAuthorToggle bool
type model_ca struct { type model_ca struct {
focusIndex int focusIndex int
@@ -74,7 +74,9 @@ func createAuthorModel(old_m *model) model_ca {
return m return m
} }
func tempAuthorModel() model_ca { func tempAuthorModel(old_m *model) model_ca {
parent_m = old_m
m := model_ca{ m := model_ca{
inputs: make([]textinput.Model, 2), inputs: make([]textinput.Model, 2),
} }
@@ -98,22 +100,11 @@ func tempAuthorModel() model_ca {
m.inputs[i] = t m.inputs[i] = t
} }
removeButton = true tempAuthorToggle = true
return m return m
} }
func initialModel(model string) model_ca {
// if model == "author" {
// return createAuthorModel()
// } else {
// return tempAuthorModel()
// }
return tempAuthorModel()
}
func (m model_ca) Init() tea.Cmd { func (m model_ca) Init() tea.Cmd {
return textinput.Blink return textinput.Blink
} }
@@ -131,7 +122,7 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
s := msg.String() s := msg.String()
// Did the user press enter while the submit button was focused? // Did the user press enter while the submit button was focused?
// If so, exit. // If so, exit.
if !removeButton { if !tempAuthorToggle {
if s == "enter" && m.focusIndex == len(m.inputs)+1 { if s == "enter" && m.focusIndex == len(m.inputs)+1 {
m.quitting = true m.quitting = true
m.AddAuthor() m.AddAuthor()
@@ -144,7 +135,8 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} else { } else {
if s == "enter" && m.focusIndex == len(m.inputs) { if s == "enter" && m.focusIndex == len(m.inputs) {
m.quitting = true m.quitting = true
return nil, nil m.TempAddAuthor()
return model{list: parent_m.list}, tea.ClearScreen
} }
} }
@@ -215,7 +207,7 @@ func (m model_ca) View() string {
//TODO: add check here for wether this button is needed //TODO: add check here for wether this button is needed
var exclude *string var exclude *string
var button *string var button *string
if !removeButton { if !tempAuthorToggle {
exclude = &excludeButton exclude = &excludeButton
if m.focusIndex == len(m.inputs) { if m.focusIndex == len(m.inputs) {
exclude = &focusedExclude exclude = &focusedExclude
@@ -291,70 +283,82 @@ func (m *model_ca) AddAuthor() {
} }
} }
func Entry_CA() string { func (m *model_ca) TempAddAuthor() {
m, err := tea.NewProgram(initialModel("author")).Run() if len(m.inputs) > 1 && m.inputs[0].Value() != "" && m.inputs[1].Value() != "" {
if err != nil { item_str := m.inputs[0].Value() + " - " + m.inputs[1].Value()
fmt.Printf("could not start program: %s\n", err) dupProtect[item_str] = m.inputs[0].Value() + ":" + m.inputs[1].Value()
os.Exit(1) i := item(item_str)
} parent_m.list.InsertItem(len(parent_m.list.Items())+1, item(item_str))
selectToggle(i)
if len(m.(model_ca).inputs) > 0 &&
m.(model_ca).inputs[0].Value() != "" &&
m.(model_ca).inputs[1].Value() != "" &&
m.(model_ca).inputs[2].Value() != "" &&
m.(model_ca).inputs[3].Value() != "" {
author_file := utils.Find_authorfile()
f, err := os.OpenFile(author_file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
} }
defer f.Close()
sb := strings.Builder{}
sb.WriteRune('\n')
sb.WriteString(fmt.Sprintf("%s|%s|%s|%s",
m.(model_ca).inputs[0].Value(),
m.(model_ca).inputs[1].Value(),
m.(model_ca).inputs[2].Value(),
m.(model_ca).inputs[3].Value()))
if m.(model_ca).exclude {
sb.WriteString(fmt.Sprintf("|%s", "ex"))
}
if m.(model_ca).inputs[4].Value() != "" {
sb.WriteString(fmt.Sprintf(";;%s", m.(model_ca).inputs[4].Value()))
}
//sb.WriteRune('\n')
if _, err = f.WriteString(sb.String()); err != nil {
panic(err)
}
utils.Define_users(utils.Find_authorfile())
return m.(model_ca).inputs[0].Value()
}
return ""
} }
func Entry_TA() string { // func Entry_CA() string {
//old_m = old_m // m, err := tea.NewProgram(initialModel("author")).Run()
// if err != nil {
// fmt.Printf("could not start program: %s\n", err)
// os.Exit(1)
// }
m, err := tea.NewProgram(initialModel("temp")).Run() // if len(m.(model_ca).inputs) > 0 &&
if err != nil { // m.(model_ca).inputs[0].Value() != "" &&
fmt.Printf("could not start program: %s\n", err) // m.(model_ca).inputs[1].Value() != "" &&
os.Exit(1) // m.(model_ca).inputs[2].Value() != "" &&
} // m.(model_ca).inputs[3].Value() != "" {
// author_file := utils.Find_authorfile()
// f, err := os.OpenFile(author_file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
// if err != nil {
// panic(err)
// }
if len(m.(model_ca).inputs) > 0 && // defer f.Close()
m.(model_ca).inputs[0].Value() != "" &&
m.(model_ca).inputs[1].Value() != "" {
utils.TempAddUser(m.(model_ca).inputs[0].Value(), m.(model_ca).inputs[1].Value())
return m.(model_ca).inputs[0].Value() + ":" + m.(model_ca).inputs[1].Value()
}
return "" // sb := strings.Builder{}
// sb.WriteRune('\n')
} // sb.WriteString(fmt.Sprintf("%s|%s|%s|%s",
// m.(model_ca).inputs[0].Value(),
// m.(model_ca).inputs[1].Value(),
// m.(model_ca).inputs[2].Value(),
// m.(model_ca).inputs[3].Value()))
// if m.(model_ca).exclude {
// sb.WriteString(fmt.Sprintf("|%s", "ex"))
// }
// if m.(model_ca).inputs[4].Value() != "" {
// sb.WriteString(fmt.Sprintf(";;%s", m.(model_ca).inputs[4].Value()))
// }
// //sb.WriteRune('\n')
// if _, err = f.WriteString(sb.String()); err != nil {
// panic(err)
// }
// utils.Define_users(utils.Find_authorfile())
// return m.(model_ca).inputs[0].Value()
// }
// return ""
// }
// func Entry_TA() string {
// //old_m = old_m
// m, err := tea.NewProgram(initialModel("temp")).Run()
// if err != nil {
// fmt.Printf("could not start program: %s\n", err)
// os.Exit(1)
// }
// if len(m.(model_ca).inputs) > 0 &&
// m.(model_ca).inputs[0].Value() != "" &&
// m.(model_ca).inputs[1].Value() != "" {
// utils.TempAddUser(m.(model_ca).inputs[0].Value(), m.(model_ca).inputs[1].Value())
// return m.(model_ca).inputs[0].Value() + ":" + m.(model_ca).inputs[1].Value()
// }
// return ""
// }
+1 -9
View File
@@ -204,15 +204,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.tempAdd): case key.Matches(msg, m.keys.tempAdd):
screen.Clear() screen.Clear()
screen.MoveTopLeft() screen.MoveTopLeft()
tempAuthr := Entry_TA() sub_model = tempAuthorModel(&m)
if tempAuthr != "" {
split := strings.Split(tempAuthr, ":")
item_str := split[0] + " - " + split[1]
dupProtect[item_str] = tempAuthr
i := item(item_str)
m.list.InsertItem(len(m.list.Items())+1, i)
selectToggle(i)
}
return m, tea.ClearScreen return m, tea.ClearScreen
case key.Matches(msg, m.keys.createAuthor): case key.Matches(msg, m.keys.createAuthor):