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 (
@@ -27,6 +27,8 @@ var (
excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude"))
)
var removeButton bool
type model_ca struct {
focusIndex int
inputs []textinput.Model
@@ -34,7 +36,7 @@ type model_ca struct {
exclude bool
}
func initialModel() model_ca {
func createAuthorModel() model_ca {
m := model_ca{
inputs: make([]textinput.Model, 5),
}
@@ -43,11 +45,11 @@ 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
@@ -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,6 +125,7 @@ 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 !removeButton {
if s == "enter" && m.focusIndex == len(m.inputs)+1 {
m.quitting = true
return m, tea.Quit
@@ -93,6 +134,12 @@ func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
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
if s == "up" || s == "shift+tab" {
@@ -158,20 +205,30 @@ func (m model_ca) View() string {
}
}
button := &blurredButton
if m.focusIndex == len(m.inputs)+1 {
button = &focusedButton
}
exclude := &excludeButton
//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
}
}
fmt.Fprintf(&b, "\n\n%s\n\n", *button)
@@ -181,20 +238,19 @@ func (m model_ca) View() string {
}
func Entry_CA() string {
m, err := tea.NewProgram(initialModel()).Run()
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)
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)
}
@@ -228,3 +284,21 @@ func Entry_CA() string{
}
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 ""
}