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"
"strings"
//"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
//"github.com/inancgumus/screen"
)
var (
@@ -21,20 +21,22 @@ var (
cursorStyle = focusedStyle
noStyle = lipgloss.NewStyle()
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))
focusedButton = focusedStyle.Render("[ Submit ]")
focusedExclude = focusedStyle.Render("[ Exclude ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude"))
focusedButton = focusedStyle.Render("[ Submit ]")
focusedExclude = focusedStyle.Render("[ Exclude ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude"))
)
var removeButton bool
type model_ca struct {
focusIndex int
inputs []textinput.Model
quitting bool
exclude bool
quitting bool
exclude bool
}
func initialModel() model_ca {
func createAuthorModel() model_ca {
m := model_ca{
inputs: make([]textinput.Model, 5),
}
@@ -43,17 +45,17 @@ func initialModel() model_ca {
for i := range m.inputs {
t = textinput.New()
t.Cursor.Style = cursorStyle
t.CharLimit = 32
//t.CharLimit = 32
switch i {
case 0:
t.Placeholder = "shortname (e.g. jo)"
t.Placeholder = "Shortname (e.g. jo)"
t.Focus()
t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle
case 1:
t.Placeholder = "Long name (e.g. JohnDoe)"
case 2:
case 2:
t.Placeholder = "Username (e.g. JohnDoe-gh)"
case 3:
t.Placeholder = "Email (e.g. JohnDoe@domain.do"
@@ -67,6 +69,44 @@ func initialModel() model_ca {
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 {
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?
// If so, exit.
if s == "enter" && m.focusIndex == len(m.inputs)+1 {
m.quitting = true
return m, tea.Quit
} else if s == "enter" && m.focusIndex == len(m.inputs) {
// toggle exclude
m.exclude = !m.exclude
return m, nil
if !removeButton {
if s == "enter" && m.focusIndex == len(m.inputs)+1 {
m.quitting = true
return m, tea.Quit
} else if s == "enter" && m.focusIndex == len(m.inputs) {
// toggle exclude
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
@@ -158,21 +205,31 @@ func (m model_ca) View() string {
}
}
button := &blurredButton
if m.focusIndex == len(m.inputs)+1 {
button = &focusedButton
}
exclude := &excludeButton
if m.focusIndex == len(m.inputs) {
exclude = &focusedExclude
//TODO: add check here for wether this button is needed
var exclude *string
var button *string
if !removeButton {
exclude = &excludeButton
if m.focusIndex == len(m.inputs) {
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)
b.WriteString(cursorModeHelpStyle.Render())
@@ -180,21 +237,20 @@ func (m model_ca) View() string {
return b.String()
}
func Entry_CA() string{
m, err := tea.NewProgram(initialModel()).Run()
func Entry_CA() string {
m, err := tea.NewProgram(initialModel("author")).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() != "" &&
m.(model_ca).inputs[2].Value() != "" &&
m.(model_ca).inputs[3].Value() != "" {
f, err := os.OpenFile("author_file", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
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)
}
@@ -204,11 +260,11 @@ func Entry_CA() string{
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()))
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"))
@@ -227,4 +283,22 @@ func Entry_CA() string{
return m.(model_ca).inputs[0].Value()
}
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 ""
}