diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-07-29 12:05:33 -0230 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-07-29 12:05:33 -0230 |
| commit | 9b889ee09e7e6a4ffc257044aaf5334827759050 (patch) | |
| tree | 8a4c22ae9ef8d62ef69b216de5ac941375b3f798 /ui.go | |
| parent | 39d02975dc58830471ca86702e07533a43fc6424 (diff) | |
| download | pfc-9b889ee09e7e6a4ffc257044aaf5334827759050.zip | |
add padding to ui
Diffstat (limited to 'ui.go')
| -rw-r--r-- | ui.go | 49 |
1 files changed, 40 insertions, 9 deletions
@@ -18,11 +18,12 @@ const ( ) // sigDigs is the number of significant digits when printing a number. -const sigDigs = 64 +const sigDigs = 17 type UI struct { - calc Calculator - windowWidth int // Width of the window measured in characters. + calc Calculator + width int // Width of the window measured in characters. + height int } func (ui UI) Init() tea.Cmd { @@ -32,7 +33,8 @@ func (ui UI) Init() tea.Cmd { func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: - ui.windowWidth = msg.Width + ui.width = msg.Width + ui.height = msg.Height case tea.KeyMsg: switch msg.String() { case "ctrl+c", "Q": @@ -73,16 +75,45 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (ui UI) View() string { - var s string + s := padding(ui) + + // Angle mode. + s += fmt.Sprintf("%*s\n", ui.width-1, ui.calc.anglem) + + // Stack. + top := boxTop(ui.width) + bottom := boxBottom(ui.width) + s += top + "\n" for _, f := range ui.calc.stack { - s += printNum(f) + "\n" + s += fmt.Sprintf("%[1]c%*s%[1]c\n", boxVertical, ui.width-2, printNum(f)) } - s += boxTop(ui.windowWidth) + "\n" - s += fmt.Sprintf("%[1]c%-*s%[1]c\n", boxVertical, ui.windowWidth-2, ui.calc.buf) - s += boxBottom(ui.windowWidth) + s += bottom + "\n" + + // Buffer. + s += boxTop(ui.width) + "\n" + s += fmt.Sprintf("%[1]c%*s%[1]c\n", boxVertical, ui.width-2, ui.calc.buf) + s += boxBottom(ui.width) return s } +func padding(ui UI) string { + // Number of lines occupied by each ui element. + var ( + anglem = 1 + stack = len(ui.calc.stack) + 2 + buf = 3 + ) + lines := ui.height - anglem - stack - buf + if lines < 1 { + return "" + } + s := make([]byte, lines) + for i := 0; i < lines; i++ { + s[i] = '\n' + } + return string(s) +} + func printNum(v float64) string { return fmt.Sprintf(" %.*g", sigDigs, v) } |