123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- // localizing all of the functions required to construct the user interface
- package itashi
- import (
- "fmt"
- "time"
- tea "github.com/charmbracelet/bubbletea"
- )
- const SPRING_EQUINOX = 81
- const SUMMER_SOLSTICE = 173
- const AUTUMN_EQUINOX = 265
- const WINTER_SOLSTICE = 356
- func getSeason(dayofyear int) string {
- if dayofyear > 365 {
- return "[REDACTED]"
- }
- if dayofyear < 0 {
- return "[REDACTED]"
- }
- if dayofyear > 0 && dayofyear < SPRING_EQUINOX {
- return "Winter"
- }
- if dayofyear > SPRING_EQUINOX && dayofyear < SUMMER_SOLSTICE {
- return "Spring"
- }
- if dayofyear > SUMMER_SOLSTICE && dayofyear < AUTUMN_EQUINOX {
- return "Summer"
- }
- if dayofyear > AUTUMN_EQUINOX && dayofyear < WINTER_SOLSTICE {
- return "Autumn"
- }
- if dayofyear > WINTER_SOLSTICE && dayofyear < 365 {
- return "Winter"
- }
- return "Dont know how you got here...."
- }
- type model struct {
- choices []string
- cursor int
- selected map[int]struct{}
- }
- func InitialModel() model {
- shelf := NewFilesystemShelf(FS_SAVE_LOCATION)
- //shelf.AddTask(Task{Id: 1, Title: "This is a sample task", Desc: "Quick sample task that im testing the storage with",
- // Due: time.Now().AddDate(0, 0, 2), Done: false, Priority: 1})
- return model{
- // Our to-do list is a grocery list
- choices: GetTaskNames(shelf.GetAll()),
- // A map which indicates which choices are selected. We're using
- // the map like a mathematical set. The keys refer to the indexes
- // of the `choices` slice, above.
- selected: make(map[int]struct{}),
- }
- }
- func (m model) Init() tea.Cmd {
- // Just return `nil`, which means "no I/O right now, please."
- return nil
- }
- func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
- switch msg := msg.(type) {
- // Is it a key press?
- case tea.KeyMsg:
- // Cool, what was the actual key pressed?
- switch msg.String() {
- // These keys should exit the program.
- case "ctrl+c", "q":
- return m, tea.Quit
- // The "up" and "k" keys move the cursor up
- case "up", "k":
- if m.cursor > 0 {
- m.cursor--
- }
- // The "down" and "j" keys move the cursor down
- case "down", "j":
- if m.cursor < len(m.choices)-1 {
- m.cursor++
- }
- // The "enter" key and the spacebar (a literal space) toggle
- // the selected state for the item that the cursor is pointing at.
- case "enter", " ":
- _, ok := m.selected[m.cursor]
- if ok {
- delete(m.selected, m.cursor)
- } else {
- m.selected[m.cursor] = struct{}{}
- }
- }
- }
- // Return the updated model to the Bubble Tea runtime for processing.
- // Note that we're not returning a command.
- return m, nil
- }
- func (m model) View() string {
- // The header
- rn := time.Now()
- rnString := fmt.Sprintf("%s %v, %v", rn.Month().String(), rn.Day(), rn.Year())
- season := getSeason(rn.YearDay())
- solsticeDiff := 10
- day := rn.Weekday()
- timenow := fmt.Sprintf("%v:%v %s", rn.Hour(), rn.Minute(), "AM")
- toEod := 5
- toSd := 6
- s := fmt.Sprintf(
- `
- %s %s, %v days until the next solstice.
- %s
- %s (%vH, %vM -> EoD, %vH, %vM -> sunset)
- `, rnString, season, solsticeDiff, day, timenow, toEod, toEod, toSd, toSd)
- // Iterate over our choices
- for i, choice := range m.choices {
- // Is the cursor pointing at this choice?
- cursor := " " // no cursor
- if m.cursor == i {
- cursor = ">" // cursor!
- }
- // Is this choice selected?
- checked := " " // not selected
- if _, ok := m.selected[i]; ok {
- checked = "x" // selected!
- }
- // Render the row
- s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
- }
- // The footer
- s += "\nPress q to quit.\n"
- // Send the UI for rendering
- return s
- }
|