diff --git a/tui/forms.go b/tui/forms.go new file mode 100644 index 0000000..ad45099 --- /dev/null +++ b/tui/forms.go @@ -0,0 +1,116 @@ +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 (0–23)").Value(&startHour), + huh.NewInput().Title("End Hour (0–23)").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 (0–23)").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 +} + diff --git a/tui/newEventForm.go b/tui/newEventForm.go deleted file mode 100644 index 82cbc16..0000000 --- a/tui/newEventForm.go +++ /dev/null @@ -1,50 +0,0 @@ -package tui - -import ( - "strconv" - - "git.skdevstudios.com/SK-Development-Studios/go-cal-tui/internal/storage" - - - "github.com/charmbracelet/huh" -) - -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 (0–23)").Value(&startHour), - huh.NewInput().Title("End Hour (0–23)").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 -} diff --git a/tui/update.go b/tui/update.go index cb5cfdf..62a2f3d 100644 --- a/tui/update.go +++ b/tui/update.go @@ -88,6 +88,48 @@ func (m model) updateHourlyView(msg tea.Msg) (tea.Model, tea.Cmd) { showEventForm(func(e storage.EventInput) { _ = storage.SaveEvent(e) }) + case "e": + event, err := storage.GetEvent(int32(m.year), int32(m.monthIndex+1), int32(m.selectedDay), int32(m.hourCursor)) + if err != nil || event == nil { + break // No event to edit at this hour + } + + // Create editable input struct + input := storage.EventInput{ + Title: event.Title, + Description: event.Description, + Year: event.Year, + Month: event.Month, + Day: event.Day, + StartHour: event.StartHour, + EndHour: event.EndHour, + Color: event.Color, + } + + showEditForm(&input, func(updated storage.EventInput) { + _ = storage.EditEvent(event.Year, event.Month, event.Day, event.StartHour, updated) + }) + +case "x": // or "delete" + event, err := storage.GetEvent(int32(m.year), int32(m.monthIndex+1), int32(m.selectedDay), int32(m.hourCursor)) + if err != nil || event == nil { + break // No event to delete + } + + input := storage.EventInput{ + Title: event.Title, + Description: event.Description, + Year: event.Year, + Month: event.Month, + Day: event.Day, + StartHour: event.StartHour, + EndHour: event.EndHour, + Color: event.Color, + } + + showDeleteConfirm(&input, func() { + _ = storage.DeleteEvent(event.Year, event.Month, event.Day, event.StartHour) + }) case "esc": m.mode = monthView return m, nil diff --git a/tui/view.go b/tui/view.go index 940fd40..a585f2c 100644 --- a/tui/view.go +++ b/tui/view.go @@ -134,7 +134,7 @@ func (m model) viewHourly() string { b.WriteString(label + "\n") } - b.WriteString("[n] to add event, [up]/[down] keys to move, [Esc] to go back to month view, [q] to quit.") + b.WriteString("[n] to add event,[e] to edit event,[x] to delete an event, [up]/[down] keys to move, [Esc] to go back to month view, [q] to quit.") return b.String() }