From 02da9b2bb39b7e9c2f5aa988cd4f9709521ae7b0 Mon Sep 17 00:00:00 2001 From: Vitaliy Turov Date: Sat, 15 Feb 2025 12:12:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20bubbletea=20tui=20=D0=B1=D0=B8?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/lesson3/cmd/main.go | 77 ++++++++++++++++++++++++++++++++++++++++++ go/lesson3/go.mod | 25 ++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 go/lesson3/cmd/main.go diff --git a/go/lesson3/cmd/main.go b/go/lesson3/cmd/main.go new file mode 100644 index 0000000..926cabf --- /dev/null +++ b/go/lesson3/cmd/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "os" + "strings" + "time" + + "github.com/charmbracelet/bubbles/progress" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +const ( + padding = 2 + maxWidth = 80 +) + +var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#626262")).Render + +func main() { + prog := progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")) + + if _, err := tea.NewProgram(model{progress: prog}).Run(); err != nil { + fmt.Println("Oh no!", err) + os.Exit(1) + } +} + +type tickMsg time.Time + +type model struct { + percent float64 + progress progress.Model +} + +func (m model) Init() tea.Cmd { + return tickCmd() +} + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + return m, tea.Quit + + case tea.WindowSizeMsg: + m.progress.Width = msg.Width - padding*2 - 4 + if m.progress.Width > maxWidth { + m.progress.Width = maxWidth + } + return m, nil + + case tickMsg: + m.percent += 0.25 + if m.percent > 1.0 { + m.percent = 1.0 + return m, tea.Quit + } + return m, tickCmd() + + default: + return m, nil + } +} + +func (m model) View() string { + pad := strings.Repeat(" ", padding) + return "\n" + + pad + m.progress.ViewAs(m.percent) + "\n\n" + + pad + helpStyle("Press any key to quit") +} + +func tickCmd() tea.Cmd { + return tea.Tick(time.Second, func(t time.Time) tea.Msg { + return tickMsg(t) + }) +} diff --git a/go/lesson3/go.mod b/go/lesson3/go.mod index b628e93..be11943 100644 --- a/go/lesson3/go.mod +++ b/go/lesson3/go.mod @@ -1,3 +1,28 @@ module lesson3 go 1.24.0 + +require ( + github.com/charmbracelet/bubbles v0.20.0 + github.com/charmbracelet/bubbletea v1.3.3 + github.com/charmbracelet/lipgloss v1.0.0 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/harmonica v0.2.0 // indirect + github.com/charmbracelet/x/ansi v0.8.0 // indirect + github.com/charmbracelet/x/term v0.2.1 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + golang.org/x/sync v0.11.0 // indirect + golang.org/x/sys v0.30.0 // indirect + golang.org/x/text v0.3.8 // indirect +)