Skip to content

Commit

Permalink
Fix flex footer (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras authored Mar 8, 2022
1 parent 4a0aec4 commit 47d0cf2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/flex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewModel() Model {
columnKeyElement: "Fire",
columnKeyDescription: "直立した恐竜のような身体と、尻尾の先端に常に燃えている炎が特徴。",
}),
}),
}).WithStaticFooter("A footer!"),
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func (m *Model) recalculateTable() {
func (m Model) View() string {
strs := []string{
"A flexible table that fills available space (Name is fixed-width)",
fmt.Sprintf("Total margin: %d (left/right to adjust)", m.totalMargin),
fmt.Sprintf("Target size: %d (left/right to adjust)", m.totalWidth-m.totalMargin),
"Press q or ctrl+c to quit",
m.flexTable.View(),
}
Expand Down
19 changes: 14 additions & 5 deletions table/dimensions.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package table

func (m *Model) recalculateWidth() {
total := 0
if m.targetTotalWidth != 0 {
m.totalWidth = m.targetTotalWidth
} else {
total := 0

for _, column := range m.columns {
total += column.width
}
for _, column := range m.columns {
total += column.width
}

m.totalWidth = total + len(m.columns) - 1
m.totalWidth = total + len(m.columns) + 1
}

updateColumnWidths(m.columns, m.targetTotalWidth)
}
Expand Down Expand Up @@ -52,6 +56,11 @@ func updateColumnWidths(cols []Column, totalWidth int) {
leftoverWidth--
}

if index == len(cols)-1 {
width += leftoverWidth
leftoverWidth = 0
}

width = max(width, 1)

cols[index].width = width
Expand Down
4 changes: 3 additions & 1 deletion table/footer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func (m Model) renderFooter() string {
return ""
}

styleFooter := m.baseStyle.Copy().Inherit(m.border.styleFooter).Width(m.totalWidth)
const borderAdjustment = 2

styleFooter := m.baseStyle.Copy().Inherit(m.border.styleFooter).Width(m.totalWidth - borderAdjustment)

if m.staticFooter != "" {
return styleFooter.Render(m.staticFooter)
Expand Down
40 changes: 40 additions & 0 deletions table/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -522,3 +523,42 @@ func TestSimpleFlex3x3(t *testing.T) {

assert.Equal(t, expectedTable, rendered)
}

func TestSimpleFlex3x3AtAllTargetWidths(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
NewFlexColumn("2", "2", 1),
NewFlexColumn("3", "3", 2),
}).WithTargetWidth(20)

rows := []Row{}

for rowIndex := 1; rowIndex <= 3; rowIndex++ {
rowData := RowData{}

for columnIndex := 1; columnIndex <= 3; columnIndex++ {
id := fmt.Sprintf("%d", columnIndex)

rowData[id] = fmt.Sprintf("%d,%d", columnIndex, rowIndex)
}

rows = append(rows, NewRow(rowData))
}

model = model.WithRows(rows)

for targetWidth := 15; targetWidth < 100; targetWidth++ {
model = model.WithTargetWidth(targetWidth)

rendered := model.View()

firstLine := strings.Split(rendered, "\n")[0]

assert.Equal(t, targetWidth, model.totalWidth)
assert.Equal(t, targetWidth, runewidth.StringWidth(firstLine))

if t.Failed() {
return
}
}
}

0 comments on commit 47d0cf2

Please sign in to comment.