85 lines
1.6 KiB
Go
85 lines
1.6 KiB
Go
package tui
|
|
|
|
import "github.com/charmbracelet/bubbles/table"
|
|
|
|
const (
|
|
sidebarWidth = 18
|
|
footerHeight = 1
|
|
titleHeight = 2
|
|
boxChromeV = 2
|
|
boxChromeH = 4
|
|
)
|
|
|
|
// flexColumns grows the columns at flexIdx to fill totalW, distributing extra
|
|
// space proportionally to each flex column's existing width. Bubble-tea's
|
|
// table widget reserves padding around every cell, accounted for via colPadding.
|
|
func flexColumns(totalW int, cols []table.Column, flexIdx []int) []table.Column {
|
|
out := make([]table.Column, len(cols))
|
|
copy(out, cols)
|
|
if totalW <= 0 || len(flexIdx) == 0 {
|
|
return out
|
|
}
|
|
const colPadding = 2
|
|
used := 0
|
|
for _, c := range out {
|
|
used += c.Width + colPadding
|
|
}
|
|
extra := totalW - used
|
|
if extra <= 0 {
|
|
return out
|
|
}
|
|
totalFlex := 0
|
|
for _, i := range flexIdx {
|
|
if i < 0 || i >= len(out) {
|
|
return out
|
|
}
|
|
totalFlex += out[i].Width
|
|
}
|
|
if totalFlex == 0 {
|
|
return out
|
|
}
|
|
distributed := 0
|
|
for j, i := range flexIdx {
|
|
var add int
|
|
if j == len(flexIdx)-1 {
|
|
add = extra - distributed
|
|
} else {
|
|
add = extra * out[i].Width / totalFlex
|
|
}
|
|
out[i].Width += add
|
|
distributed += add
|
|
}
|
|
return out
|
|
}
|
|
|
|
type layoutDims struct {
|
|
termW, termH int
|
|
contentW int
|
|
contentH int
|
|
sidebarH int
|
|
}
|
|
|
|
func computeLayout(termW, termH int) layoutDims {
|
|
if termW < 60 {
|
|
termW = 60
|
|
}
|
|
if termH < 18 {
|
|
termH = 18
|
|
}
|
|
contentW := termW - sidebarWidth - boxChromeH - 1
|
|
if contentW < 40 {
|
|
contentW = 40
|
|
}
|
|
contentH := termH - titleHeight - footerHeight - boxChromeV
|
|
if contentH < 10 {
|
|
contentH = 10
|
|
}
|
|
return layoutDims{
|
|
termW: termW,
|
|
termH: termH,
|
|
contentW: contentW,
|
|
contentH: contentH,
|
|
sidebarH: contentH,
|
|
}
|
|
}
|