aboutsummaryrefslogtreecommitdiffstats
path: root/ui.go
blob: 03286ecf94744b71eb5a3e7e02f7bf0fe08362fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main

import (
	"fmt"

	"github.com/charmbracelet/bubbletea"
)

// Types

type UI struct {
	calc        Calculator
	windowWidth int // Width of the window measured in characters.
}

// Interface Implementations

func (ui UI) Init() tea.Cmd {
	return nil
}

func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		ui.windowWidth = msg.Width
	case tea.KeyMsg:
		switch msg.String() {
		case "ctrl+c", "q":
			return ui, tea.Quit
		}
	}
	return ui, nil
}

func (ui UI) View() string {
	var s string
	for _, f := range ui.calc.stack {
		s += fmt.Sprintf("%f\n", f)
	}
	horizBar := make([]byte, ui.windowWidth)
	for i := range horizBar {
		horizBar[i] = '-'
	}
	s += string(horizBar) + "\n"
	s += fmt.Sprintf("| %*s |\n", ui.windowWidth-4, ui.calc.buffer)
	s += string(horizBar)
	return s
}