From ee4d23765da97c892d02e7334900837c6d2aecad Mon Sep 17 00:00:00 2001 From: Noah Date: Sun, 17 Mar 2024 22:17:19 -0500 Subject: [PATCH] refactor([times]): Move Indicator to its own file (fix #6) --- indicator.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ times.go | 57 --------------------------------------------- 2 files changed, 65 insertions(+), 57 deletions(-) create mode 100644 indicator.go diff --git a/indicator.go b/indicator.go new file mode 100644 index 0000000..1cef0b5 --- /dev/null +++ b/indicator.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "github.com/charmbracelet/bubbles/spinner" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "strings" + "time" +) + +type Indicator struct { + spinner spinner.Model + quitting bool + err error + info IndicatorInfo +} + +type IndicatorInfo struct { + duration time.Duration + quitting bool + info string +} + +func (i IndicatorInfo) String() string { + if i.duration == 0 { + return textStyle.Render(strings.Repeat(".", 30)) + } + return fmt.Sprintf("%s %s", textStyle.Render(i.duration.String()), i.info) +} + +func initialIndicator() Indicator { + s := spinner.New() + s.Spinner = spinner.Dot + s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#a6da95")) + return Indicator{spinner: s} +} +func (m Indicator) Init() tea.Cmd { + return m.spinner.Tick +} +func (m Indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case error: + m.err = msg + return m, nil + case IndicatorInfo: + m.info = msg + m.quitting = msg.quitting + return m, nil + default: + var cmd tea.Cmd + m.spinner, cmd = m.spinner.Update(msg) + return m, cmd + } +} +func (m Indicator) View() string { + if m.err != nil { + return m.err.Error() + } + str := "" + if !m.quitting { + str += fmt.Sprintf("\n %s Loading time logs...\n %s\n", m.spinner.View(), m.info.String()) + } + return str +} diff --git a/times.go b/times.go index 2475b48..aa8efd8 100644 --- a/times.go +++ b/times.go @@ -3,10 +3,8 @@ package main import ( "fmt" "os" - "strings" "time" - "github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/table" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -99,61 +97,6 @@ func (m timesviewer) View() string { totalTextStyle.Render(fmt.Sprintf("\nTotal - %s\n", m.total_time.String())) } -type Indicator struct { - spinner spinner.Model - quitting bool - err error - info IndicatorInfo -} - -type IndicatorInfo struct { - duration time.Duration - quitting bool - info string -} - -func (i IndicatorInfo) String() string { - if i.duration == 0 { - return textStyle.Render(strings.Repeat(".", 30)) - } - return fmt.Sprintf("%s %s", textStyle.Render(i.duration.String()), i.info) -} - -func initialIndicator() Indicator { - s := spinner.New() - s.Spinner = spinner.Dot - s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#a6da95")) - return Indicator{spinner: s} -} -func (m Indicator) Init() tea.Cmd { - return m.spinner.Tick -} -func (m Indicator) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - case error: - m.err = msg - return m, nil - case IndicatorInfo: - m.info = msg - m.quitting = msg.quitting - return m, nil - default: - var cmd tea.Cmd - m.spinner, cmd = m.spinner.Update(msg) - return m, cmd - } -} -func (m Indicator) View() string { - if m.err != nil { - return m.err.Error() - } - str := "" - if !m.quitting { - str += fmt.Sprintf("\n %s Loading time logs...\n %s\n", m.spinner.View(), m.info.String()) - } - return str -} - func times() { p := tea.NewProgram(initialIndicator()) go func() {