refactor: rename all model to Model

This commit is contained in:
Slug-Boi
2025-04-17 23:08:42 +02:00
committed by Theis
parent 8b3c5ab539
commit 39a5005a39
3 changed files with 76 additions and 83 deletions
+39 -41
View File
@@ -52,13 +52,13 @@ func errorGetMissingFields(m model_ca) {
if len(m.inputs) > 0 { if len(m.inputs) > 0 {
for i := 0; i < inpLen; i++ { for i := 0; i < inpLen; i++ {
if m.inputs[i].Value() == "" { if m.inputs[i].Value() == "" {
m.errorModel.missing = append(m.errorModel.missing, "- "+strings.Split(m.inputs[i].Placeholder," (")[0]) m.errorModel.missing = append(m.errorModel.missing, "- "+strings.Split(m.inputs[i].Placeholder, " (")[0])
} }
} }
} else { } else {
m.errorModel.missing = append(m.errorModel.missing, "GIGA ERROR NO INPUTS") m.errorModel.missing = append(m.errorModel.missing, "GIGA ERROR NO INPUTS")
} }
} }
func (e errorModel) View() string { func (e errorModel) View() string {
@@ -68,38 +68,37 @@ func (e errorModel) View() string {
sb.WriteString("\nMissing fields: \n") sb.WriteString("\nMissing fields: \n")
sb.WriteString(strings.Join(e.missing, "\n")) sb.WriteString(strings.Join(e.missing, "\n"))
} }
// Create centered content // Create centered content
content := lipgloss.JoinVertical( content := lipgloss.JoinVertical(
lipgloss.Left, // Changed from Center to Left for better alignment lipgloss.Left, // Changed from Center to Left for better alignment
sb.String(), sb.String(),
"\n\n[enter/esc]", "\n\n[enter/esc]",
)
)
// Create the error box // Create the error box
errorBox := lipgloss.NewStyle(). errorBox := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()). Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("9")). BorderForeground(lipgloss.Color("9")).
Padding(1, 2). Padding(1, 2).
Width(40). Width(40).
Foreground(lipgloss.Color("9")). Foreground(lipgloss.Color("9")).
Background(lipgloss.Color("0")). Background(lipgloss.Color("0")).
Align(lipgloss.Center). Align(lipgloss.Center).
Render(content) Render(content)
return lipgloss.NewStyle(). return lipgloss.NewStyle().
Padding(1, 0). Padding(1, 0).
Render(errorBox) Render(errorBox)
} }
var parent_m *model var parent_m *Model
func createAuthorModel(old_m *model) model_ca { func createAuthorModel(old_m *Model) model_ca {
parent_m = old_m parent_m = old_m
m := model_ca{ m := model_ca{
inputs: make([]textinput.Model, 5), inputs: make([]textinput.Model, 5),
errorModel: intitialErrorModel(), errorModel: intitialErrorModel(),
} }
@@ -137,10 +136,10 @@ func intitialErrorModel() *errorModel {
} }
} }
func createGHTempAuthorModel(old_m *model, user utils.User) model_ca { func createGHTempAuthorModel(old_m *Model, user utils.User) model_ca {
parent_m = old_m parent_m = old_m
m := model_ca{ m := model_ca{
inputs: make([]textinput.Model, 2), inputs: make([]textinput.Model, 2),
errorModel: intitialErrorModel(), errorModel: intitialErrorModel(),
} }
var t textinput.Model var t textinput.Model
@@ -164,11 +163,11 @@ func createGHTempAuthorModel(old_m *model, user utils.User) model_ca {
return m return m
} }
func createGHAuthorModel(old_m *model, user utils.User) model_ca { func createGHAuthorModel(old_m *Model, user utils.User) model_ca {
parent_m = old_m parent_m = old_m
m := model_ca{ m := model_ca{
inputs: make([]textinput.Model, 5), inputs: make([]textinput.Model, 5),
errorModel: intitialErrorModel(), errorModel: intitialErrorModel(),
} }
@@ -195,7 +194,7 @@ func createGHAuthorModel(old_m *model, user utils.User) model_ca {
t.SetValue("") t.SetValue("")
case 4: case 4:
t.Placeholder = "Group tags (e.g. gr1|gr2)" t.Placeholder = "Group tags (e.g. gr1|gr2)"
t.SetValue(strings.Join(user.Groups, "|")) t.SetValue(strings.Join(user.Groups, "|"))
} }
m.inputs[i] = t m.inputs[i] = t
@@ -205,7 +204,7 @@ func createGHAuthorModel(old_m *model, user utils.User) model_ca {
} }
func EntryGHAuthorModel(user utils.User) { func EntryGHAuthorModel(user utils.User) {
model := createGHAuthorModel(&model{}, user) model := createGHAuthorModel(&Model{}, user)
print(model.inputs[0].Value()) print(model.inputs[0].Value())
@@ -215,12 +214,11 @@ func EntryGHAuthorModel(user utils.User) {
} }
} }
func tempAuthorModel(old_m *Model) model_ca {
func tempAuthorModel(old_m *model) model_ca {
parent_m = old_m parent_m = old_m
m := model_ca{ m := model_ca{
inputs: make([]textinput.Model, 2), inputs: make([]textinput.Model, 2),
errorModel: intitialErrorModel(), errorModel: intitialErrorModel(),
} }
@@ -270,7 +268,7 @@ func updateErrorPopup(m model_ca, msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.errorModel.visible { if m.errorModel.visible {
return updateErrorPopup(m, msg) return updateErrorPopup(m, msg)
} }
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
@@ -297,8 +295,8 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil return m, nil
} }
if parent_m != nil { if parent_m != nil {
return model{list: parent_m.list}, tea.ClearScreen return Model{list: parent_m.list}, tea.ClearScreen
} else { } else {
m.quitting = true m.quitting = true
return m, tea.Quit return m, tea.Quit
} }
@@ -317,7 +315,7 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
if parent_m.keys != nil { if parent_m.keys != nil {
tempAuthorToggle = false tempAuthorToggle = false
return model{list: parent_m.list}, tea.ClearScreen return Model{list: parent_m.list}, tea.ClearScreen
} else { } else {
m.quitting = true m.quitting = true
tempAuthorToggle = false tempAuthorToggle = false
@@ -440,7 +438,7 @@ func (m *model_ca) AddAuthor() bool {
m.inputs[1].Value() != "" && m.inputs[1].Value() != "" &&
m.inputs[2].Value() != "" && m.inputs[2].Value() != "" &&
m.inputs[3].Value() != "" { m.inputs[3].Value() != "" {
var groups []string var groups []string
if m.inputs[4].Value() == "" { if m.inputs[4].Value() == "" {
groups = []string{} groups = []string{}
@@ -455,7 +453,7 @@ func (m *model_ca) AddAuthor() bool {
Username: m.inputs[2].Value(), Username: m.inputs[2].Value(),
Email: m.inputs[3].Value(), Email: m.inputs[3].Value(),
Ex: m.exclude, Ex: m.exclude,
Groups: groups, Groups: groups,
} }
utils.CreateAuthor(usr) utils.CreateAuthor(usr)
@@ -468,7 +466,7 @@ func (m *model_ca) AddAuthor() bool {
parent_m.list.InsertItem(len(parent_m.list.Items())+1, item(item_str)) parent_m.list.InsertItem(len(parent_m.list.Items())+1, item(item_str))
} }
return false return false
} }
return true return true
} }
@@ -480,7 +478,7 @@ func (m *model_ca) TempAddAuthor() bool {
parent_m.list.InsertItem(len(parent_m.list.Items())+1, item(item_str)) parent_m.list.InsertItem(len(parent_m.list.Items())+1, item(item_str))
selectToggle(i) selectToggle(i)
return false return false
} }
return true return true
} }
+23 -25
View File
@@ -12,30 +12,29 @@ import (
// Styles // Styles
var ( var (
errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
toggleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("99")) toggleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("99"))
activeToggleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) activeToggleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
) )
type GitHubUserModel struct { type GitHubUserModel struct {
inputs []textinput.Model inputs []textinput.Model
focusIndex int focusIndex int
submitted bool submitted bool
showError bool showError bool
errorMsg string errorMsg string
tempAuthShow bool tempAuthShow bool
tempAuth bool tempAuth bool
} }
func NewGitHubUserForm(old_m *model) GitHubUserModel { func NewGitHubUserForm(old_m *Model) GitHubUserModel {
parent_m = old_m parent_m = old_m
m := GitHubUserModel{ m := GitHubUserModel{
inputs: make([]textinput.Model, 2), inputs: make([]textinput.Model, 2),
tempAuthShow: func() bool { tempAuthShow: func() bool {
return old_m != nil return old_m != nil
}(), }(),
} }
// GitHub Username (required) // GitHub Username (required)
@@ -70,7 +69,7 @@ func (m GitHubUserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return nil, nil return nil, nil
} }
return m, tea.Quit return m, tea.Quit
case "ctrl+t": // Toggle temp mode case "ctrl+t": // Toggle temp mode
if m.tempAuthShow { if m.tempAuthShow {
m.tempAuth = !m.tempAuth m.tempAuth = !m.tempAuth
return m, nil return m, nil
@@ -91,22 +90,21 @@ func (m GitHubUserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
user.Email = m.inputs[1].Value() user.Email = m.inputs[1].Value()
} }
if m.tempAuth { if m.tempAuth {
return createGHTempAuthorModel(parent_m,user), tea.ClearScreen return createGHTempAuthorModel(parent_m, user), tea.ClearScreen
} }
return createGHAuthorModel(parent_m,user), tea.ClearScreen return createGHAuthorModel(parent_m, user), tea.ClearScreen
} else if s == "enter" && m.focusIndex == len(m.inputs) && m.tempAuthShow { } else if s == "enter" && m.focusIndex == len(m.inputs) && m.tempAuthShow {
//toggle temp mode //toggle temp mode
m.tempAuth = !m.tempAuth m.tempAuth = !m.tempAuth
return m, nil return m, nil
} }
// Cycle through inputs // Cycle through inputs
if s == "up" || s == "shift+tab" { if s == "up" || s == "shift+tab" {
m.focusIndex-- m.focusIndex--
} else { } else {
m.focusIndex++ m.focusIndex++
} }
inpNum := len(m.inputs) inpNum := len(m.inputs)
@@ -178,14 +176,14 @@ func (m GitHubUserModel) View() string {
if m.tempAuth { if m.tempAuth {
toggleText = "[X]" toggleText = "[X]"
} }
toggleBtn := fmt.Sprintf("[ TempAuthor ] %s ", toggleText) toggleBtn := fmt.Sprintf("[ TempAuthor ] %s ", toggleText)
if m.focusIndex == len(m.inputs) { // When toggle is focused if m.focusIndex == len(m.inputs) { // When toggle is focused
b.WriteString("\n" + focusedStyle.Render(toggleBtn)) b.WriteString("\n" + focusedStyle.Render(toggleBtn))
} else { } else {
b.WriteString("\n" + blurredStyle.Render(toggleBtn)) b.WriteString("\n" + blurredStyle.Render(toggleBtn))
} }
} }
// Submit button // Submit button
@@ -223,4 +221,4 @@ func RunForm() (string, string, error) {
} }
return "", "", nil return "", "", nil
} }
+14 -17
View File
@@ -148,7 +148,7 @@ func TestEntryTA(t *testing.T) {
keyPress(tm, "esc") keyPress(tm, "esc")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model_ca, got %T", fm) t.Errorf("Expected model_ca, got %T", fm)
} }
@@ -242,7 +242,7 @@ func Test_EntryCA_TriggerError(t *testing.T) {
keyPress(tm, "esc") keyPress(tm, "esc")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
mm, ok := fm.(model) mm, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model_ca, got %T", fm) t.Errorf("Expected model_ca, got %T", fm)
} }
@@ -285,7 +285,7 @@ func Test_EntryCA(t *testing.T) {
keyPress(tm, "esc") keyPress(tm, "esc")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -397,7 +397,6 @@ func TestSubmitWithRequiredField(t *testing.T) {
setup() setup()
defer teardown() defer teardown()
m := NewGitHubUserForm(nil) m := NewGitHubUserForm(nil)
tm := teatest.NewTestModel( tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300), t, m, teatest.WithInitialTermSize(300, 300),
@@ -405,10 +404,10 @@ func TestSubmitWithRequiredField(t *testing.T) {
// Simulate filling in the required field // Simulate filling in the required field
tm.Type("Slug-Boi") tm.Type("Slug-Boi")
tm.Send(tea.KeyMsg{Type: tea.KeyTab}) // Move to next field tm.Send(tea.KeyMsg{Type: tea.KeyTab}) // Move to next field
tm.Send(tea.KeyMsg{Type: tea.KeyTab}) // Move to submit button tm.Send(tea.KeyMsg{Type: tea.KeyTab}) // Move to submit button
tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) // Submit tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) // Submit
tm.Send(tea.KeyMsg{Type: tea.KeyTab}) tm.Send(tea.KeyMsg{Type: tea.KeyTab})
tm.Send(tea.KeyMsg{Type: tea.KeyTab}) tm.Send(tea.KeyMsg{Type: tea.KeyTab})
tm.Send(tea.KeyMsg{Type: tea.KeyTab}) tm.Send(tea.KeyMsg{Type: tea.KeyTab})
tm.Type("input@mail") tm.Type("input@mail")
@@ -421,8 +420,6 @@ func TestSubmitWithRequiredField(t *testing.T) {
// Check if the form was submitted // Check if the form was submitted
updated, _ := tm.FinalModel(t).(model_ca) updated, _ := tm.FinalModel(t).(model_ca)
if updated.inputs[0].Value() != "th" { if updated.inputs[0].Value() != "th" {
t.Errorf("Expected 'Slug-Boi', got '%s'", updated.inputs[0].Value()) t.Errorf("Expected 'Slug-Boi', got '%s'", updated.inputs[0].Value())
} }
@@ -440,7 +437,7 @@ func TestSubmitWithRequiredField(t *testing.T) {
// Test temp auth toggle visibility // Test temp auth toggle visibility
func TestTempAuthToggleVisibility(t *testing.T) { func TestTempAuthToggleVisibility(t *testing.T) {
// With parent model (should show toggle) // With parent model (should show toggle)
m1 := NewGitHubUserForm(&model{}) m1 := NewGitHubUserForm(&Model{})
if !m1.tempAuthShow { if !m1.tempAuthShow {
t.Error("tempAuthShow should be true with parent model") t.Error("tempAuthShow should be true with parent model")
} }
@@ -454,7 +451,7 @@ func TestTempAuthToggleVisibility(t *testing.T) {
// Test temp auth toggle functionality // Test temp auth toggle functionality
func TestTempAuthToggle(t *testing.T) { func TestTempAuthToggle(t *testing.T) {
m := NewGitHubUserForm(&model{}) m := NewGitHubUserForm(&Model{})
// Initial state // Initial state
if m.tempAuth { if m.tempAuth {
@@ -610,7 +607,7 @@ func Test_EntrySelectUsers(t *testing.T) {
keyPress(tm, "enter") keyPress(tm, "enter")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -640,7 +637,7 @@ func Test_EntrySelectAll(t *testing.T) {
keyPress(tm, "enter") keyPress(tm, "enter")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -669,7 +666,7 @@ func Test_EntryNegation(t *testing.T) {
keyPress(tm, "enter") keyPress(tm, "enter")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -700,7 +697,7 @@ func Test_EntryDeleteAuthor(t *testing.T) {
keyPress(tm, "enter") keyPress(tm, "enter")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -733,7 +730,7 @@ func Test_GroupSelection(t *testing.T) {
keyPress(tm, "enter") keyPress(tm, "enter")
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
_, ok := fm.(model) _, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }
@@ -769,7 +766,7 @@ func Test_pagination(t *testing.T) {
tm.Quit() tm.Quit()
fm := tm.FinalModel(t) fm := tm.FinalModel(t)
m, ok := fm.(model) m, ok := fm.(Model)
if !ok { if !ok {
t.Errorf("Expected model, got %T", fm) t.Errorf("Expected model, got %T", fm)
} }