diff --git a/examples/cellbuffer/main.go b/examples/cellbuffer/main.go index 00de39c..456089e 100644 --- a/examples/cellbuffer/main.go +++ b/examples/cellbuffer/main.go @@ -18,14 +18,14 @@ const ( fps = 60 frequency = 7.5 damping = 0.15 + asterisk = "*" ) func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) { - const c = "*" var ( dx, dy, d1, d2 float64 - x float64 = 0 - y = ry + x float64 + y = ry ) d1 = ry*ry - rx*rx*ry + 0.25*rx*rx @@ -33,10 +33,10 @@ func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) { dy = 2 * rx * rx * y for dx < dy { - cb.set(c, int(x+xc), int(y+yc)) - cb.set(c, int(-x+xc), int(y+yc)) - cb.set(c, int(x+xc), int(-y+yc)) - cb.set(c, int(-x+xc), int(-y+yc)) + cb.set(int(x+xc), int(y+yc)) + cb.set(int(-x+xc), int(y+yc)) + cb.set(int(x+xc), int(-y+yc)) + cb.set(int(-x+xc), int(-y+yc)) if d1 < 0 { x++ dx = dx + (2 * ry * ry) @@ -53,10 +53,10 @@ func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) { d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry) for y >= 0 { - cb.set(c, int(x+xc), int(y+yc)) - cb.set(c, int(-x+xc), int(y+yc)) - cb.set(c, int(x+xc), int(-y+yc)) - cb.set(c, int(-x+xc), int(-y+yc)) + cb.set(int(x+xc), int(y+yc)) + cb.set(int(-x+xc), int(y+yc)) + cb.set(int(x+xc), int(-y+yc)) + cb.set(int(-x+xc), int(-y+yc)) if d2 > 0 { y-- dy = dy - (2 * rx * rx) @@ -85,16 +85,12 @@ func (c *cellbuffer) init(w, h int) { c.wipe() } -func (c cellbuffer) set(v string, x, y int) { +func (c cellbuffer) set(x, y int) { i := y*c.stride + x if i > len(c.cells)-1 || x < 0 || y < 0 || x >= c.width() || y >= c.height() { return } - c.cells[i] = v -} - -func (c *cellbuffer) clear(x, y int) { - c.set(" ", x, y) + c.cells[i] = asterisk } func (c *cellbuffer) wipe() { diff --git a/examples/composable-views/main.go b/examples/composable-views/main.go index f81ce6a..4352a7c 100644 --- a/examples/composable-views/main.go +++ b/examples/composable-views/main.go @@ -98,7 +98,7 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } else { m.Next() m.resetSpinner() - cmds = append(cmds, spinner.Tick) + cmds = append(cmds, m.spinner.Tick) } } switch m.state { diff --git a/examples/help/main.go b/examples/help/main.go index fb382ce..67e5a4f 100644 --- a/examples/help/main.go +++ b/examples/help/main.go @@ -132,12 +132,12 @@ func (m model) View() string { func main() { if os.Getenv("HELP_DEBUG") != "" { - if f, err := tea.LogToFile("debug.log", "help"); err != nil { + f, err := tea.LogToFile("debug.log", "help") + if err != nil { fmt.Println("Couldn't open a file for logging:", err) os.Exit(1) - } else { - defer f.Close() } + defer f.Close() // nolint:errcheck } if _, err := tea.NewProgram(newModel()).Run(); err != nil { diff --git a/examples/http/main.go b/examples/http/main.go index 1b6a1e6..dc2842d 100644 --- a/examples/http/main.go +++ b/examples/http/main.go @@ -76,7 +76,7 @@ func checkServer() tea.Msg { if err != nil { return errMsg{err} } - defer res.Body.Close() + defer res.Body.Close() // nolint:errcheck return statusMsg(res.StatusCode) } diff --git a/examples/list-simple/main.go b/examples/list-simple/main.go index 8cc4290..f8f85ed 100644 --- a/examples/list-simple/main.go +++ b/examples/list-simple/main.go @@ -28,9 +28,9 @@ func (i item) FilterValue() string { return "" } type itemDelegate struct{} -func (d itemDelegate) Height() int { return 1 } -func (d itemDelegate) Spacing() int { return 0 } -func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil } +func (d itemDelegate) Height() int { return 1 } +func (d itemDelegate) Spacing() int { return 0 } +func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { i, ok := listItem.(item) if !ok { diff --git a/examples/package-manager/main.go b/examples/package-manager/main.go index ced7c77..eb6f69a 100644 --- a/examples/package-manager/main.go +++ b/examples/package-manager/main.go @@ -115,7 +115,7 @@ type installedPkgMsg string func downloadAndInstall(pkg string) tea.Cmd { // This is where you'd do i/o stuff to download and install packages. In // our case we're just pausing for a moment to simulate the process. - d := time.Millisecond * time.Duration(rand.Intn(500)) + d := time.Millisecond * time.Duration(rand.Intn(500)) //nolint:gosec return tea.Tick(d, func(t time.Time) tea.Msg { return installedPkgMsg(pkg) }) @@ -129,8 +129,6 @@ func max(a, b int) int { } func main() { - rand.Seed(time.Now().Unix()) - if _, err := tea.NewProgram(newModel()).Run(); err != nil { fmt.Println("Error running program:", err) os.Exit(1) diff --git a/examples/package-manager/packages.go b/examples/package-manager/packages.go index 7ed8478..40425fc 100644 --- a/examples/package-manager/packages.go +++ b/examples/package-manager/packages.go @@ -46,7 +46,7 @@ func getPackages() []string { }) for k := range pkgs { - pkgs[k] += fmt.Sprintf("-%d.%d.%d", rand.Intn(10), rand.Intn(10), rand.Intn(10)) + pkgs[k] += fmt.Sprintf("-%d.%d.%d", rand.Intn(10), rand.Intn(10), rand.Intn(10)) //nolint:gosec } return pkgs } diff --git a/examples/pipe/main.go b/examples/pipe/main.go index 1fce2fc..a309566 100644 --- a/examples/pipe/main.go +++ b/examples/pipe/main.go @@ -58,7 +58,7 @@ type model struct { func newModel(initialValue string) (m model) { i := textinput.New() i.Prompt = "" - i.CursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("63")) + i.Cursor.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63")) i.Width = 48 i.SetValue(initialValue) i.CursorEnd() diff --git a/examples/prevent-quit/main.go b/examples/prevent-quit/main.go index 1339393..7399d30 100644 --- a/examples/prevent-quit/main.go +++ b/examples/prevent-quit/main.go @@ -61,7 +61,7 @@ func initialModel() model { return model{ textarea: ti, - help: help.NewModel(), + help: help.New(), keymap: keymap{ save: key.NewBinding( key.WithKeys("ctrl+s"), diff --git a/examples/progress-download/main.go b/examples/progress-download/main.go index 4639aa1..018b29f 100644 --- a/examples/progress-download/main.go +++ b/examples/progress-download/main.go @@ -40,7 +40,7 @@ func (pw *progressWriter) Write(p []byte) (int, error) { } func getResponse(url string) (*http.Response, error) { - resp, err := http.Get(url) + resp, err := http.Get(url) // nolint:gosec if err != nil { log.Fatal(err) } @@ -64,7 +64,7 @@ func main() { fmt.Println("could not get response", err) os.Exit(1) } - defer resp.Body.Close() + defer resp.Body.Close() // nolint:errcheck // Don't add TUI if the header doesn't include content size // it's impossible see progress without total @@ -79,7 +79,7 @@ func main() { fmt.Println("could not create file:", err) os.Exit(1) } - defer file.Close() + defer file.Close() // nolint:errcheck pw := &progressWriter{ total: int(resp.ContentLength), diff --git a/examples/realtime/main.go b/examples/realtime/main.go index 516fcd6..4abddd3 100644 --- a/examples/realtime/main.go +++ b/examples/realtime/main.go @@ -24,7 +24,7 @@ type responseMsg struct{} func listenForActivity(sub chan struct{}) tea.Cmd { return func() tea.Msg { for { - time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)+100)) + time.Sleep(time.Millisecond * time.Duration(rand.Int63n(900)+100)) // nolint:gosec sub <- struct{}{} } } @@ -46,7 +46,7 @@ type model struct { func (m model) Init() tea.Cmd { return tea.Batch( - spinner.Tick, + m.spinner.Tick, listenForActivity(m.sub), // generate activity waitForActivity(m.sub), // wait for activity ) @@ -78,8 +78,6 @@ func (m model) View() string { } func main() { - rand.Seed(time.Now().UTC().UnixNano()) - p := tea.NewProgram(model{ sub: make(chan struct{}), spinner: spinner.New(), diff --git a/examples/send-msg/main.go b/examples/send-msg/main.go index 7badf6a..5ace43b 100644 --- a/examples/send-msg/main.go +++ b/examples/send-msg/main.go @@ -53,7 +53,7 @@ func newModel() model { } func (m model) Init() tea.Cmd { - return spinner.Tick + return m.spinner.Tick } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -100,14 +100,12 @@ func (m model) View() string { } func main() { - rand.Seed(time.Now().UTC().UnixNano()) - p := tea.NewProgram(newModel()) // Simulate activity go func() { for { - pause := time.Duration(rand.Int63n(899)+100) * time.Millisecond + pause := time.Duration(rand.Int63n(899)+100) * time.Millisecond // nolint:gosec time.Sleep(pause) // Send the Bubble Tea program a message from outside the @@ -129,5 +127,5 @@ func randomFood() string { "a kohlrabi", "some spaghetti", "tacos", "a currywurst", "some curry", "a sandwich", "some peanut butter", "some cashews", "some ramen", } - return string(food[rand.Intn(len(food))]) + return food[rand.Intn(len(food))] // nolint:gosec } diff --git a/examples/stopwatch/main.go b/examples/stopwatch/main.go index 69884b1..2b1a4d4 100644 --- a/examples/stopwatch/main.go +++ b/examples/stopwatch/main.go @@ -91,7 +91,7 @@ func main() { key.WithHelp("q", "quit"), ), }, - help: help.NewModel(), + help: help.New(), } m.keymap.start.SetEnabled(false) diff --git a/examples/textinputs/main.go b/examples/textinputs/main.go index 2620bfc..5882858 100644 --- a/examples/textinputs/main.go +++ b/examples/textinputs/main.go @@ -8,6 +8,7 @@ import ( "os" "strings" + "github.com/charmbracelet/bubbles/cursor" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -28,7 +29,7 @@ var ( type model struct { focusIndex int inputs []textinput.Model - cursorMode textinput.CursorMode + cursorMode cursor.Mode } func initialModel() model { @@ -39,7 +40,7 @@ func initialModel() model { var t textinput.Model for i := range m.inputs { t = textinput.New() - t.CursorStyle = cursorStyle + t.Cursor.Style = cursorStyle t.CharLimit = 32 switch i { @@ -77,12 +78,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // Change cursor mode case "ctrl+r": m.cursorMode++ - if m.cursorMode > textinput.CursorHide { - m.cursorMode = textinput.CursorBlink + if m.cursorMode > cursor.CursorHide { + m.cursorMode = cursor.CursorBlink } cmds := make([]tea.Cmd, len(m.inputs)) for i := range m.inputs { - cmds[i] = m.inputs[i].SetCursorMode(m.cursorMode) + cmds[i] = m.inputs[i].Cursor.SetMode(m.cursorMode) } return m, tea.Batch(cmds...) diff --git a/examples/timer/main.go b/examples/timer/main.go index 7ee4f7d..42d980c 100644 --- a/examples/timer/main.go +++ b/examples/timer/main.go @@ -111,7 +111,7 @@ func main() { key.WithHelp("q", "quit"), ), }, - help: help.NewModel(), + help: help.New(), } m.keymap.start.SetEnabled(false) diff --git a/examples/tui-daemon-combo/main.go b/examples/tui-daemon-combo/main.go index 962da81..0736406 100644 --- a/examples/tui-daemon-combo/main.go +++ b/examples/tui-daemon-combo/main.go @@ -19,8 +19,6 @@ import ( var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render func main() { - rand.Seed(time.Now().UTC().UnixNano()) - var ( daemonMode bool showHelp bool @@ -77,7 +75,7 @@ func newModel() model { func (m model) Init() tea.Cmd { log.Println("Starting work...") return tea.Batch( - spinner.Tick, + m.spinner.Tick, runPretendProcess, ) } @@ -128,12 +126,12 @@ type processFinishedMsg time.Duration // pretendProcess simulates a long-running process. func runPretendProcess() tea.Msg { - pause := time.Duration(rand.Int63n(899)+100) * time.Millisecond + pause := time.Duration(rand.Int63n(899)+100) * time.Millisecond // nolint:gosec time.Sleep(pause) return processFinishedMsg(pause) } func randomEmoji() string { emojis := []rune("🍦🧋🍡🤠👾😭🦊🐯🦆🥨🎏🍔🍒🍥🎮📦🦁🐶🐸🍕🥐🧲🚒🥇🏆🌽") - return string(emojis[rand.Intn(len(emojis))]) + return string(emojis[rand.Intn(len(emojis))]) // nolint:gosec } diff --git a/examples/views/main.go b/examples/views/main.go index a06b592..da3c4ec 100644 --- a/examples/views/main.go +++ b/examples/views/main.go @@ -216,7 +216,7 @@ func chosenView(m model) string { label = fmt.Sprintf("Downloaded. Exiting in %s seconds...", colorFg(strconv.Itoa(m.Ticks), "79")) } - return msg + "\n\n" + label + "\n" + progressbar(80, m.Progress) + "%" + return msg + "\n\n" + label + "\n" + progressbar(m.Progress) + "%" } func checkbox(label string, checked bool) string { @@ -226,7 +226,7 @@ func checkbox(label string, checked bool) string { return fmt.Sprintf("[ ] %s", label) } -func progressbar(width int, percent float64) string { +func progressbar(percent float64) string { w := float64(progressBarWidth) fullSize := int(math.Round(w * percent)) @@ -253,14 +253,6 @@ func makeFgStyle(color string) func(string) string { return termenv.Style{}.Foreground(term.Color(color)).Styled } -// Color a string's foreground and background with the given value. -func makeFgBgStyle(fg, bg string) func(string) string { - return termenv.Style{}. - Foreground(term.Color(fg)). - Background(term.Color(bg)). - Styled -} - // Generate a blend of colors. func makeRamp(colorA, colorB string, steps float64) (s []string) { cA, _ := colorful.Hex(colorA) diff --git a/tutorials/commands/main.go b/tutorials/commands/main.go index 8ed68b7..37a62f0 100644 --- a/tutorials/commands/main.go +++ b/tutorials/commands/main.go @@ -22,7 +22,7 @@ func checkServer() tea.Msg { if err != nil { return errMsg{err} } - defer res.Body.Close() + defer res.Body.Close() // nolint:errcheck return statusMsg(res.StatusCode) }