Skip to content

Commit

Permalink
calculate width based on data width
Browse files Browse the repository at this point in the history
Signed-off-by: Pulak Kanti Bhowmick <pkbhowmick007@gmail.com>
  • Loading branch information
pkbhowmick committed Dec 16, 2024
1 parent 1fa0f47 commit f368057
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions pkg/list/list_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func generateTable(data tableData) {
return
}

// Dynamically calculate column widths
columnWidths := calculateColumnWidths(data)

// Renderer for styling
re := lipgloss.NewRenderer(os.Stdout)

Expand All @@ -218,8 +221,7 @@ func generateTable(data tableData) {
Align(lipgloss.Center) // Header style

CellStyle = re.NewStyle().
Padding(0, 1).
Width(20) // Base style for rows
Padding(0, 1) // Base style for rows

OddRowStyle = CellStyle.Foreground(gray) // Style for odd rows
EvenRowStyle = CellStyle.Foreground(lightGray) // Style for even rows
Expand All @@ -244,10 +246,8 @@ func generateTable(data tableData) {
style = OddRowStyle // Odd rows
}

// Optional: Adjust specific columns (e.g., make the middle column wider)
if col == 1 {
style = style.Width(25)
}
// Apply dynamic width to each column
style = style.Width(columnWidths[col])

return style
}).
Expand All @@ -258,6 +258,35 @@ func generateTable(data tableData) {
fmt.Println(t)
}

// Calculate the maximum width for each column
func calculateColumnWidths(data tableData) []int {
columnCount := len(data.header)
columnWidths := make([]int, columnCount)

// Check headers
for i, header := range data.header {
if len(header) > columnWidths[i] {
columnWidths[i] = len(header)
}
}

// Check rows
for _, row := range data.rows {
for i, cell := range row {
if len(cell) > columnWidths[i] {
columnWidths[i] = len(cell)
}
}
}

// Add padding for aesthetics
for i := range columnWidths {
columnWidths[i] += 2 // Add 2 spaces for padding
}

return columnWidths
}

// FilterAndListComponents orchestrates the process
func FilterAndListComponents(stackFlag string, abstractFlag bool, stacksMap map[string]any, listConfig schema.ListConfig) (string, error) {
// Step 1: Parse columns
Expand Down

0 comments on commit f368057

Please sign in to comment.