refactor([times]): Move Indicator to its own file (fix #6)

This commit is contained in:
Noah 2024-03-17 22:17:19 -05:00
parent 6b5ad657b6
commit ee4d23765d
2 changed files with 65 additions and 57 deletions

65
indicator.go Normal file
View File

@ -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
}

View File

@ -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() {