2025-10-16 12:46:23 +03:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/charmbracelet/lipgloss/table"
|
|
|
|
terminal "golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
2025-10-16 14:54:24 +03:00
|
|
|
// ANSI256 256 colors, 8-bit
|
2025-10-16 12:46:23 +03:00
|
|
|
var (
|
|
|
|
Blue = lipgloss.Color("21")
|
|
|
|
Magenta = lipgloss.Color("13")
|
|
|
|
Gray = lipgloss.Color("238")
|
|
|
|
White = lipgloss.Color("15")
|
|
|
|
Red = lipgloss.Color("9")
|
|
|
|
Green = lipgloss.Color("10")
|
|
|
|
Yellow = lipgloss.Color("11")
|
|
|
|
)
|
|
|
|
|
|
|
|
type TableBuilder struct {
|
|
|
|
tableHeader []string
|
|
|
|
rows [][]string
|
|
|
|
Table *table.Table
|
|
|
|
padding int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTableBuilder() *TableBuilder {
|
|
|
|
return &TableBuilder{
|
|
|
|
Table: table.New(),
|
|
|
|
padding: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableBuilder) Reset() {
|
|
|
|
t.tableHeader = nil
|
|
|
|
t.rows = nil
|
|
|
|
t.Table = table.New()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableBuilder) AddTableHeader(strs ...string) {
|
|
|
|
t.tableHeader = append(t.tableHeader, strs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableBuilder) AddRow(strs ...string) {
|
|
|
|
t.rows = append(t.rows, strs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableBuilder) Width(w int) {
|
|
|
|
t.Table.Width(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableBuilder) colWidth(colNumber int) int {
|
|
|
|
var maxWidth int
|
|
|
|
for _, row := range t.rows {
|
|
|
|
for col, v := range row {
|
|
|
|
if col == colNumber && maxWidth < len(v)+t.padding*2 {
|
|
|
|
maxWidth = len(v) + t.padding*2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TableBuilder) CreateTable() *table.Table {
|
|
|
|
width, _, _ := terminal.GetSize(0)
|
|
|
|
t.Table.
|
|
|
|
BorderRow(false).BorderColumn(false).
|
|
|
|
BorderLeft(false).BorderRight(false).BorderTop(false).BorderBottom(false).
|
|
|
|
StyleFunc(func(row, col int) lipgloss.Style {
|
|
|
|
re := lipgloss.NewRenderer(os.Stdout)
|
|
|
|
if row == -1 {
|
|
|
|
// Align(lipgloss.Center)
|
|
|
|
return re.NewStyle().Bold(true).Align(lipgloss.Left).Padding(0, 2, 0, 0).Foreground(Green)
|
|
|
|
}
|
|
|
|
return re.NewStyle().Padding(0, 2, 0, 0).Align(lipgloss.Left)
|
|
|
|
}).
|
|
|
|
Headers(t.tableHeader...).
|
|
|
|
Rows(t.rows...)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case width < 100:
|
|
|
|
t.Table.Width(width)
|
|
|
|
case len(t.tableHeader) > 8:
|
|
|
|
t.Table.Width(width)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Table
|
|
|
|
}
|
|
|
|
|
|
|
|
// Justify Выравнивание по ширине терминала
|
|
|
|
func (t *TableBuilder) Justify() {
|
|
|
|
width, _, _ := terminal.GetSize(0)
|
|
|
|
t.Table.Width(width)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetColor(color lipgloss.Color, str string) string {
|
|
|
|
return lipgloss.NewStyle().Foreground(color).Render(str)
|
|
|
|
}
|