Skip to content

Instantly share code, notes, and snippets.

@meowgorithm
Created June 10, 2024 13:04
Show Gist options
  • Save meowgorithm/39cd4a528b48c7e042bf6642bef55496 to your computer and use it in GitHub Desktop.
Save meowgorithm/39cd4a528b48c7e042bf6642bef55496 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"log/slog"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/davecgh/go-spew/spew"
)
type model struct {
width int
height int
}
// Init implements tea.Model.
func (outer model) Init(ctx tea.Context) (tea.Model, tea.Cmd) {
slog.Debug("teaModel.Init()")
return model{}, nil
}
func (m model) Update(ctx tea.Context, msg tea.Msg) (tea.Model, tea.Cmd) {
prefix := spew.Sprintf("Update(%#v)", msg)
slog.Debug(fmt.Sprintf("%s ENTER", prefix))
defer slog.Debug(fmt.Sprintf("%s LEAVE", prefix))
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case tea.KeyMsg:
return m, tea.Quit
}
return m, nil
}
func (m model) View(ctx tea.Context) string {
slog.Debug("View() ENTER")
defer slog.Debug("View() LEAVE")
out := fmt.Sprint("The screen has ", m.width,
" columns and ", m.height, " rows\n")
if m.width > 0 {
style := lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{
Light: "#DDDADA",
Dark: "#9C9C00",
})
out = style.Render(out)
}
return out
}
func setUpLogging() *os.File {
file, err := tea.LogToFileWith(
"sift.log", "sift", log.Default())
if err != nil {
fmt.Printf("Error logging to file: %v\n", err)
os.Exit(1)
}
log.Default().SetFlags(log.LstdFlags | log.Lmicroseconds | log.Llongfile)
slog.SetLogLoggerLevel(slog.LevelDebug)
return file
}
func main() {
logFile := setUpLogging()
defer func() {
if logFile != nil {
_ = logFile.Close()
}
}()
slog.Debug("program started")
program := tea.NewProgram(model{}, tea.WithAltScreen())
_, err := program.Run()
if err != nil {
slog.Error("Error running program: %v", err)
}
slog.Debug("program exiting")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment