got month grid going
This commit is contained in:
parent
16e807b9f2
commit
af03d06bf5
2 changed files with 58 additions and 41 deletions
95
main.go
95
main.go
|
|
@ -1,73 +1,86 @@
|
|||
// main.go
|
||||
package main
|
||||
|
||||
// A simple program demonstrating the spinner component from the Bubbles
|
||||
// component library.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/charmbracelet/bubbles/spinner"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"os"
|
||||
)
|
||||
|
||||
type errMsg error
|
||||
const (
|
||||
numRows = 5
|
||||
numCols = 7
|
||||
cellW = 10
|
||||
cellH = 1
|
||||
)
|
||||
|
||||
var (
|
||||
cellStyle = lipgloss.NewStyle().Width(cellW).Height(cellH).Align(lipgloss.Center)
|
||||
selectedStyle = cellStyle.Copy().Bold(true).Background(lipgloss.Color("12")).Foreground(lipgloss.Color("15"))
|
||||
unselectedStyle = cellStyle.Copy().Background(lipgloss.Color("236")).Foreground(lipgloss.Color("250"))
|
||||
)
|
||||
|
||||
type model struct {
|
||||
spinner spinner.Model
|
||||
quitting bool
|
||||
err error
|
||||
}
|
||||
|
||||
func initialModel() model {
|
||||
s := spinner.New()
|
||||
s.Spinner = spinner.Dot
|
||||
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
||||
return model{spinner: s}
|
||||
cursorRow int
|
||||
cursorCol int
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd {
|
||||
return m.spinner.Tick
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "q", "esc", "ctrl+c":
|
||||
m.quitting = true
|
||||
case "ctrl+c", "q":
|
||||
return m, tea.Quit
|
||||
default:
|
||||
return m, nil
|
||||
case "up":
|
||||
if m.cursorRow > 0 {
|
||||
m.cursorRow--
|
||||
}
|
||||
case "down":
|
||||
if m.cursorRow < numRows-1 {
|
||||
m.cursorRow++
|
||||
}
|
||||
case "left":
|
||||
if m.cursorCol > 0 {
|
||||
m.cursorCol--
|
||||
}
|
||||
case "right":
|
||||
if m.cursorCol < numCols-1 {
|
||||
m.cursorCol++
|
||||
}
|
||||
}
|
||||
|
||||
case errMsg:
|
||||
m.err = msg
|
||||
return m, nil
|
||||
|
||||
default:
|
||||
var cmd tea.Cmd
|
||||
m.spinner, cmd = m.spinner.Update(msg)
|
||||
return m, cmd
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m model) View() string {
|
||||
if m.err != nil {
|
||||
return m.err.Error()
|
||||
var out string
|
||||
count := 1
|
||||
for r := 0; r < numRows; r++ {
|
||||
for c := 0; c < numCols; c++ {
|
||||
label := fmt.Sprintf("Cell %02d", count)
|
||||
if r == m.cursorRow && c == m.cursorCol {
|
||||
out += selectedStyle.Render(label)
|
||||
} else {
|
||||
out += unselectedStyle.Render(label)
|
||||
}
|
||||
count++
|
||||
}
|
||||
out += "\n"
|
||||
}
|
||||
str := fmt.Sprintf("\n\n %s Loading forever...press q to quit\n\n", m.spinner.View())
|
||||
if m.quitting {
|
||||
return str + "\n"
|
||||
}
|
||||
return str
|
||||
out += "\nUse arrow keys to move, q to quit."
|
||||
return out
|
||||
}
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(initialModel())
|
||||
if _, err := p.Run(); err != nil {
|
||||
fmt.Println(err)
|
||||
p := tea.NewProgram(model{})
|
||||
if err := p.Start(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue