refactor: change name of author and combine with temp add command

This commit is contained in:
Slug-Boi
2024-10-24 20:45:28 +02:00
parent c6fe91d24c
commit 7521fd5f26
@@ -9,10 +9,10 @@ import (
"os" "os"
"strings" "strings"
//"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
//"github.com/inancgumus/screen"
) )
var ( var (
@@ -21,20 +21,22 @@ var (
cursorStyle = focusedStyle cursorStyle = focusedStyle
noStyle = lipgloss.NewStyle() noStyle = lipgloss.NewStyle()
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244")) cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))
focusedButton = focusedStyle.Render("[ Submit ]") focusedButton = focusedStyle.Render("[ Submit ]")
focusedExclude = focusedStyle.Render("[ Exclude ]") focusedExclude = focusedStyle.Render("[ Exclude ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit")) blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude")) excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude"))
) )
var removeButton bool
type model_ca struct { type model_ca struct {
focusIndex int focusIndex int
inputs []textinput.Model inputs []textinput.Model
quitting bool quitting bool
exclude bool exclude bool
} }
func initialModel() model_ca { func createAuthorModel() model_ca {
m := model_ca{ m := model_ca{
inputs: make([]textinput.Model, 5), inputs: make([]textinput.Model, 5),
} }
@@ -43,17 +45,17 @@ func initialModel() model_ca {
for i := range m.inputs { for i := range m.inputs {
t = textinput.New() t = textinput.New()
t.Cursor.Style = cursorStyle t.Cursor.Style = cursorStyle
t.CharLimit = 32 //t.CharLimit = 32
switch i { switch i {
case 0: case 0:
t.Placeholder = "shortname (e.g. jo)" t.Placeholder = "Shortname (e.g. jo)"
t.Focus() t.Focus()
t.PromptStyle = focusedStyle t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle t.TextStyle = focusedStyle
case 1: case 1:
t.Placeholder = "Long name (e.g. JohnDoe)" t.Placeholder = "Long name (e.g. JohnDoe)"
case 2: case 2:
t.Placeholder = "Username (e.g. JohnDoe-gh)" t.Placeholder = "Username (e.g. JohnDoe-gh)"
case 3: case 3:
t.Placeholder = "Email (e.g. JohnDoe@domain.do" t.Placeholder = "Email (e.g. JohnDoe@domain.do"
@@ -67,6 +69,44 @@ func initialModel() model_ca {
return m return m
} }
func tempAuthorModel() model_ca {
m := model_ca{
inputs: make([]textinput.Model, 2),
}
var t textinput.Model
for i := range m.inputs {
t = textinput.New()
t.Cursor.Style = cursorStyle
//t.CharLimit = 32
switch i {
case 0:
t.Placeholder = "Username (e.g. JohnDoe-gh)"
t.Focus()
t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle
case 1:
t.Placeholder = "Email (e.g. JohnDoe@JohnDoe.io)"
}
m.inputs[i] = t
}
removeButton = true
return m
}
func initialModel(model string) model_ca {
if model == "author" {
return createAuthorModel()
} else {
return tempAuthorModel()
}
}
func (m model_ca) Init() tea.Cmd { func (m model_ca) Init() tea.Cmd {
return textinput.Blink return textinput.Blink
} }
@@ -85,13 +125,20 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// 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 s == "enter" && m.focusIndex == len(m.inputs)+1 { if !removeButton {
m.quitting = true if s == "enter" && m.focusIndex == len(m.inputs)+1 {
return m, tea.Quit m.quitting = true
} else if s == "enter" && m.focusIndex == len(m.inputs) { return m, tea.Quit
// toggle exclude } else if s == "enter" && m.focusIndex == len(m.inputs) {
m.exclude = !m.exclude // toggle exclude
return m, nil m.exclude = !m.exclude
return m, nil
}
} else {
if s == "enter" && m.focusIndex == len(m.inputs) {
m.quitting = true
return m, tea.Quit
}
} }
// Cycle indexes // Cycle indexes
@@ -158,21 +205,31 @@ func (m model_ca) View() string {
} }
} }
button := &blurredButton //TODO: add check here for wether this button is needed
if m.focusIndex == len(m.inputs)+1 { var exclude *string
button = &focusedButton var button *string
} if !removeButton {
exclude := &excludeButton exclude = &excludeButton
if m.focusIndex == len(m.inputs) { if m.focusIndex == len(m.inputs) {
exclude = &focusedExclude exclude = &focusedExclude
}
button = &blurredButton
if m.focusIndex == len(m.inputs)+1 {
button = &focusedButton
}
if m.exclude {
fmt.Fprintf(&b, "\n\n%s: [X]\n\n", *exclude)
} else {
fmt.Fprintf(&b, "\n\n%s: [ ]\n\n", *exclude)
}
} else {
button = &blurredButton
if m.focusIndex == len(m.inputs) {
button = &focusedButton
}
} }
if m.exclude {
fmt.Fprintf(&b, "\n\n%s: [X]\n\n", *exclude)
} else {
fmt.Fprintf(&b, "\n\n%s: [ ]\n\n", *exclude)
}
fmt.Fprintf(&b, "\n\n%s\n\n", *button) fmt.Fprintf(&b, "\n\n%s\n\n", *button)
b.WriteString(cursorModeHelpStyle.Render()) b.WriteString(cursorModeHelpStyle.Render())
@@ -180,21 +237,20 @@ func (m model_ca) View() string {
return b.String() return b.String()
} }
func Entry_CA() string{ func Entry_CA() string {
m, err := tea.NewProgram(initialModel()).Run() m, err := tea.NewProgram(initialModel("author")).Run()
if err != nil { if err != nil {
fmt.Printf("could not start program: %s\n", err) fmt.Printf("could not start program: %s\n", err)
os.Exit(1) os.Exit(1)
} }
if len(m.(model_ca).inputs) > 0 &&
m.(model_ca).inputs[0].Value() != "" &&
if len(m.(model_ca).inputs) > 0 && m.(model_ca).inputs[1].Value() != "" &&
m.(model_ca).inputs[0].Value() != "" && m.(model_ca).inputs[2].Value() != "" &&
m.(model_ca).inputs[1].Value() != "" && m.(model_ca).inputs[3].Value() != "" {
m.(model_ca).inputs[2].Value() != "" && author_file := utils.Find_authorfile()
m.(model_ca).inputs[3].Value() != "" { f, err := os.OpenFile(author_file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
f, err := os.OpenFile("author_file", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -204,11 +260,11 @@ func Entry_CA() string{
sb := strings.Builder{} sb := strings.Builder{}
sb.WriteRune('\n') sb.WriteRune('\n')
sb.WriteString(fmt.Sprintf("%s|%s|%s|%s", sb.WriteString(fmt.Sprintf("%s|%s|%s|%s",
m.(model_ca).inputs[0].Value(), m.(model_ca).inputs[0].Value(),
m.(model_ca).inputs[1].Value(), m.(model_ca).inputs[1].Value(),
m.(model_ca).inputs[2].Value(), m.(model_ca).inputs[2].Value(),
m.(model_ca).inputs[3].Value())) m.(model_ca).inputs[3].Value()))
if m.(model_ca).exclude { if m.(model_ca).exclude {
sb.WriteString(fmt.Sprintf("|%s", "ex")) sb.WriteString(fmt.Sprintf("|%s", "ex"))
@@ -227,4 +283,22 @@ func Entry_CA() string{
return m.(model_ca).inputs[0].Value() return m.(model_ca).inputs[0].Value()
} }
return "" return ""
} }
func Entry_TA(ch chan bool) string {
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 ""
}