GoCalTui/tui/forms.go

116 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tui
import (
"fmt"
"strconv"
"git.skdevstudios.com/SK-Development-Studios/go-cal-tui/internal/storage"
"github.com/charmbracelet/huh"
)
// showEventForm prompts user for new event data and calls onSubmit with the result.
func showEventForm(onSubmit func(storage.EventInput)) error {
var title, description, year, month, day, startHour, endHour, color string
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().Title("Title").Value(&title),
huh.NewInput().Title("Description").Value(&description),
huh.NewInput().Title("Year").Value(&year),
huh.NewInput().Title("Month").Value(&month),
huh.NewInput().Title("Day").Value(&day),
huh.NewInput().Title("Start Hour (023)").Value(&startHour),
huh.NewInput().Title("End Hour (023)").Value(&endHour),
huh.NewInput().Title("Color (e.g. 99 or #ff5733)").Value(&color),
),
)
if err := form.Run(); err != nil {
return err
}
yr, _ := strconv.Atoi(year)
mo, _ := strconv.Atoi(month)
da, _ := strconv.Atoi(day)
sh, _ := strconv.Atoi(startHour)
eh, _ := strconv.Atoi(endHour)
onSubmit(storage.EventInput{
Title: title,
Description: description,
Year: int32(yr),
Month: int32(mo),
Day: int32(da),
StartHour: int32(sh),
EndHour: int32(eh),
Color: color,
})
return nil
}
// showEditForm allows editing of mutable fields in an existing event.
func showEditForm(evt *storage.EventInput, onSubmit func(storage.EventInput)) error {
// Convert to string for input
endHourStr := strconv.Itoa(int(evt.EndHour))
colorStr := evt.Color
titleStr := evt.Title
descStr := evt.Description
form := huh.NewForm(
huh.NewGroup(
huh.NewInput().Title("Title").Value(&titleStr),
huh.NewInput().Title("Description").Value(&descStr),
huh.NewInput().Title("End Hour (023)").Value(&endHourStr),
huh.NewInput().Title("Color (e.g. 99 or #ff5733)").Value(&colorStr),
),
)
if err := form.Run(); err != nil {
return err
}
endHour, err := strconv.Atoi(endHourStr)
if err != nil {
return err
}
onSubmit(storage.EventInput{
Title: titleStr,
Description: descStr,
Year: evt.Year,
Month: evt.Month,
Day: evt.Day,
StartHour: evt.StartHour,
EndHour: int32(endHour),
Color: colorStr,
})
return nil
}
// showDeleteConfirm prompts for confirmation before deleting an event.
func showDeleteConfirm(evt *storage.EventInput, onConfirm func()) error {
var confirm bool
form := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Title(fmt.Sprintf("Delete event '%s' on %04d-%02d-%02d at %02d:00?", evt.Title, evt.Year, evt.Month, evt.Day, evt.StartHour)).
Affirmative("Yes").
Negative("No").
Value(&confirm),
),
)
if err := form.Run(); err != nil {
return err
}
if confirm {
onConfirm()
}
return nil
}