Skip to content

Commit

Permalink
Add width and height to Model.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertClo committed Nov 17, 2024
1 parent b7e1720 commit f87cd02
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import (

type errMsg error

type model struct {
spinner spinner.Model
counter int
quitting bool
err error
type Model struct {
spinner spinner.Model
counter int
message string
quitting bool
err error
windowSize struct {
width int
height int
}
}

var quitKeys = key.NewBinding(
Expand Down Expand Up @@ -44,18 +49,18 @@ var startKeys = key.NewBinding(
key.WithHelp("s", "start"),
)

func initialModel() model {
func initialModel() Model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#c084fc"))
return model{spinner: s, counter: 0}
return Model{spinner: s, counter: 0, message: ""}
}

func (m model) Init() tea.Cmd {
func (m Model) Init() tea.Cmd {
return m.spinner.Tick
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if key.Matches(msg, quitKeys) {
Expand All @@ -77,6 +82,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmd.Run()

Check failure on line 82 in main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `cmd.Run` is not checked (errcheck)
}
return m, nil
case tea.WindowSizeMsg:
m.windowSize.width = msg.Width
m.windowSize.height = msg.Height
m.message = fmt.Sprintf("Window size: %d x %d", msg.Width, msg.Height)
return m, nil
case errMsg:
m.err = msg
return m, nil
Expand All @@ -88,7 +98,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

func (m model) View() string {
func (m Model) View() string {
if m.err != nil {
return m.err.Error()
}
Expand All @@ -100,12 +110,15 @@ func (m model) View() string {
%s
%s
%s
%s
%s`,
m.counter,
incrementKeys.Help(),
decrementKeys.Help(),
startKeys.Help(),
quitKeys.Help())
quitKeys.Help(),
m.message,
)

if m.quitting {
return str + "\n"
Expand Down

0 comments on commit f87cd02

Please sign in to comment.