Update something

This commit is contained in:
tiff 2025-03-20 22:04:20 -04:00
parent e886740265
commit c31be0e16d
3 changed files with 141 additions and 46 deletions

View file

@ -1,63 +1,104 @@
/*
Copyright © 2024 tiff github@tifflabs.org
*/
package cmd
import (
"os"
"encoding/csv"
"strconv"
"text/tabwriter"
"github.com/mergestat/timediff"
"github.com/spf13/cobra"
"errors"
"fmt"
tea "github.com/charmbracelet/bubbletea"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: `goto-do`,
Short: `CLI todo CRUD app`,
Long: `CLI todo app where you can create, update, and delete tasks all
from the command line. Written in Go`
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
type model struct {
todos []string // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected map[int]struct{} // which to-do items are selected
}
// f, err := os.Create("", "tasks.csv")
// if err != nil {
// log.Fatal(error)
// }
// defer os.Remove(f.name())
func initialModel() model {
return model{
// Our to-do list is a grocery list
todos: []string{""},
f, err := os.
func csvCreate() {
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
// 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 init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.goto-do.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
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.todos)-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
s := "What do you want to do today?\n\n"
// Iterate over our choices
for i, todo := range m.todos {
// 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, todo)
}
// The footer
s += "\nPress q to quit.\n"
// Send the UI for rendering
return s
}