* tui: titles, labels and buttons drawn a column a rune Ten places drew each rune of a title or label at its byte offset, so a string holding runes of more than one byte drew with holes and ran past the width it was measured and truncated to: the Pane title, the Modal title and hint, the Divider label, TabBar titles, Table cells, Form labels, the TextField placeholder, the ConfirmDialog and AlertDialog buttons, and the ProgressOverlay title. Each now draws through Region.Text, which counts columns by rune, within the bound its loop kept; a Form label stays within its label column, as the byte count meant it to. * an unchanged frame writes nothing flush closed every frame with SGR 0, so a frame that changed no cell still sent four bytes, and an app drawing an idle screen kept writing to the terminal. The reset now follows only a frame that wrote cells: the next cell written restates its whole style, and Fini resets on its own. Each widget, given a title such as "● é · ñ", draws it whole where it drew it with holes before; a frame flushed twice writes nothing the second time, where it wrote ESC [ 0 m.
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package tui
|
|
|
|
import (
|
|
"github.com/lixenwraith/color"
|
|
"github.com/lixenwraith/terminal"
|
|
)
|
|
|
|
// ModalOpts configures modal overlay rendering
|
|
type ModalOpts struct {
|
|
Title string
|
|
Hint string // Right-aligned hint text
|
|
Border LineType
|
|
BorderFg color.RGB
|
|
TitleFg color.RGB
|
|
HintFg color.RGB
|
|
Bg color.RGB
|
|
}
|
|
|
|
// Modal fills region with background, draws border with title/hint, returns content region
|
|
func (r Region) Modal(opts ModalOpts) Region {
|
|
if r.W < 5 || r.H < 3 {
|
|
return r.Sub(1, 1, 0, 0)
|
|
}
|
|
|
|
// Fill entire region
|
|
r.Fill(opts.Bg)
|
|
|
|
// Draw border
|
|
r.Box(opts.Border, opts.BorderFg)
|
|
|
|
// Title centered on top edge
|
|
if opts.Title != "" {
|
|
title := " " + opts.Title + " "
|
|
titleLen := RuneLen(title)
|
|
if titleLen > r.W-4 {
|
|
title = Truncate(title, r.W-4)
|
|
titleLen = RuneLen(title)
|
|
}
|
|
r.Text((r.W-titleLen)/2, 0, title, opts.TitleFg, opts.Bg, terminal.AttrBold)
|
|
}
|
|
|
|
// Hint right-aligned on top edge
|
|
if opts.Hint != "" {
|
|
hint := opts.Hint
|
|
hintLen := RuneLen(hint)
|
|
if hintLen > r.W/3 {
|
|
hint = Truncate(hint, r.W/3)
|
|
hintLen = RuneLen(hint)
|
|
}
|
|
x := r.W - hintLen - 2
|
|
if x < r.W/2 {
|
|
x = r.W / 2
|
|
}
|
|
r.Sub(0, 0, r.W-1, 1).Text(x, 0, hint, opts.HintFg, opts.Bg, terminal.AttrNone)
|
|
}
|
|
|
|
// Return content region
|
|
return r.Sub(1, 1, r.W-2, r.H-2)
|
|
}
|
|
|