v0.1.0 initial commit
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"github.com/lixenwraith/terminal"
|
||||
)
|
||||
|
||||
// PaneOpts configures pane rendering
|
||||
type PaneOpts struct {
|
||||
Title string
|
||||
Border LineType
|
||||
BorderFg terminal.RGB
|
||||
Bg terminal.RGB
|
||||
TitleFg terminal.RGB
|
||||
}
|
||||
|
||||
// Pane draws bordered pane with optional title, returns content region
|
||||
// Content region is inside border, below title row if present
|
||||
func (r Region) Pane(opts PaneOpts) Region {
|
||||
if r.W < 3 || r.H < 3 {
|
||||
return r.Sub(1, 1, 0, 0)
|
||||
}
|
||||
|
||||
// Fill background
|
||||
r.Fill(opts.Bg)
|
||||
|
||||
// Draw border
|
||||
r.Box(opts.Border, opts.BorderFg)
|
||||
|
||||
// Title on top edge
|
||||
headerH := 0
|
||||
if opts.Title != "" {
|
||||
headerH = 1
|
||||
title := " " + opts.Title + " "
|
||||
if RuneLen(title) > r.W-4 {
|
||||
title = Truncate(title, r.W-4)
|
||||
}
|
||||
x := 2
|
||||
for i, ch := range title {
|
||||
if x+i >= r.W-1 {
|
||||
break
|
||||
}
|
||||
r.Cell(x+i, 0, ch, opts.TitleFg, opts.Bg, terminal.AttrBold)
|
||||
}
|
||||
}
|
||||
|
||||
// Return content region (inside border, below title)
|
||||
return r.Sub(1, 1+headerH, r.W-2, r.H-2-headerH)
|
||||
}
|
||||
|
||||
// TitledPane fills region with background, draws centered title at top, returns content region
|
||||
// Content region starts at row 1 with full width
|
||||
func (r Region) TitledPane(title string, titleFg, bg terminal.RGB) Region {
|
||||
r.Fill(bg)
|
||||
if title != "" && r.H > 0 {
|
||||
r.TextCenter(0, title, titleFg, bg, terminal.AttrBold)
|
||||
}
|
||||
if r.H <= 1 {
|
||||
return r.Sub(0, 0, r.W, 0)
|
||||
}
|
||||
return r.Sub(0, 1, r.W, r.H-1)
|
||||
}
|
||||
|
||||
// TitledPaneFocused is TitledPane with focus-dependent background
|
||||
func (r Region) TitledPaneFocused(title string, titleFg, bg, focusBg terminal.RGB, focused bool) Region {
|
||||
if focused {
|
||||
bg = focusBg
|
||||
}
|
||||
return r.TitledPane(title, titleFg, bg)
|
||||
}
|
||||
Reference in New Issue
Block a user