Skip to content

Commit

Permalink
align table width
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jan 27, 2024
1 parent 6c684a0 commit 6fcf178
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
44 changes: 39 additions & 5 deletions internal/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type Table struct {

// heights tracks the height of each row.
heights []int

fixedColumns []int
}

// New returns a new Table that can be modified through different
Expand Down Expand Up @@ -328,11 +330,14 @@ func (t *Table) String() string {
if width < t.width && t.width > 0 {
// Table is too narrow, expand the columns evenly until it reaches the
// desired width.
var i int
for width < t.width {
t.widths[i]++
width++
i = (i + 1) % len(t.widths)
idx := t.getExpandableColumns()
if len(idx) > 0 {
var i int
for width < t.width {
t.widths[idx[i]]++
width++
i = (i + 1) % len(idx)
}
}
} else if width > t.width && t.width > 0 {
// Table is too wide, calculate the median non-whitespace length of each
Expand Down Expand Up @@ -403,6 +408,35 @@ func (t *Table) String() string {
MaxWidth(t.width).Render(s.String())
}

// GetTotalWidth returns the total width of the table, usually called after String.
func (t *Table) GetTotalWidth() int {
return t.computeWidth()
}

// FixedColumns make sure the columns not to be expanded when table is too narrow.
func (t *Table) FixedColumns(columns ...int) *Table {
t.fixedColumns = append(t.fixedColumns, columns...)
return t
}

// getExpandableColumns returns the non-fixed columns.
func (t *Table) getExpandableColumns() []int {
var idx []int
for i := 0; i < len(t.widths); i++ {
fixed := false
for _, j := range t.fixedColumns {
if i == j {
fixed = true
break
}
}
if !fixed {
idx = append(idx, i)
}
}
return idx
}

// computeWidth computes the width of the table in it's current configuration.
func (t *Table) computeWidth() int {
width := sum(t.widths) + btoi(t.borderLeft) + btoi(t.borderRight)
Expand Down
16 changes: 12 additions & 4 deletions tssh/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type tableTheme struct {
detailsNameStyle lipgloss.Style
detailsValueStyle lipgloss.Style
detailsBorderStyle lipgloss.Style
tableWidth int
}

func (t *tableTheme) cellStyle(host *sshHost, row, col int) lipgloss.Style {
Expand Down Expand Up @@ -199,7 +200,7 @@ func (t *tableTheme) renderItems(items []interface{}, idx int) string {
}
data = append(data, []string{icon, host.Alias, host.Host, host.GroupLabels})
}
return table.New().BorderRow(true).
tbl := table.New().BorderRow(true).
Headers("", "Alias", "Host Name", "Group Labels").Rows(data...).
StyleFunc(func(row, col int) lipgloss.Style {
var host *sshHost
Expand All @@ -210,7 +211,10 @@ func (t *tableTheme) renderItems(items []interface{}, idx int) string {
}).
BorderStyleFunc(func(row, col int, borderType table.BorderType) lipgloss.Style {
return t.borderStyle(idx, row, col, borderType)
}).String()
})
result := tbl.String()
t.tableWidth = tbl.GetTotalWidth()
return result
}

func (t *tableTheme) renderDetails(item interface{}) string {
Expand Down Expand Up @@ -247,14 +251,18 @@ func (t *tableTheme) renderDetails(item interface{}) string {
warning("Unknown prompt detail item: %s", item)
}
}
return table.New().BorderRow(true).Rows(data...).
tbl := table.New().BorderRow(true).Rows(data...).
BorderStyle(t.detailsBorderStyle).
StyleFunc(func(row, col int) lipgloss.Style {
if col == 0 {
return t.detailsNameStyle
}
return t.detailsValueStyle
}).String()
}).FixedColumns(0)
if t.tableWidth > 0 {
tbl.Width(t.tableWidth)
}
return tbl.String()
}

func getTableTheme() *promptTheme {
Expand Down

0 comments on commit 6fcf178

Please sign in to comment.