Skip to content

Commit

Permalink
chore: copy table columns using copy func
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Jul 9, 2024
1 parent 39fb423 commit 38de2fd
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions internal/tui/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ type Model[V resource.Resource] struct {
border lipgloss.Border
borderColor lipgloss.TerminalColor

cursorRow int
cursorID resource.ID
renderedRows int
cursorRow int
cursorID resource.ID

items map[resource.ID]V
sortFunc SortFunc[V]
Expand Down Expand Up @@ -92,7 +91,7 @@ type RenderedRow map[ColumnKey]string
type SortFunc[V any] func(V, V) int

// New creates a new model for the table widget.
func New[V resource.Resource](columns []Column, fn RowRenderer[V], width, height int, opts ...Option[V]) Model[V] {
func New[V resource.Resource](cols []Column, fn RowRenderer[V], width, height int, opts ...Option[V]) Model[V] {
filter := textinput.New()
filter.Prompt = "Filter: "

Expand All @@ -110,13 +109,11 @@ func New[V resource.Resource](columns []Column, fn RowRenderer[V], width, height
fn(&m)
}

// Deliberately use range to copy column structs onto receiver, because the
// caller may be using columns in multiple tables and columns are modified
// by each table.
//
// TODO: use copy, which is more explicit
for _, col := range columns {
// Set default truncation function if unset
// Copy column structs onto receiver, because the caller may modify columns.
copy(m.cols, cols)

// For each column, set default truncation function if unset.
for _, col := range cols {
if col.TruncationFunc == nil {
col.TruncationFunc = defaultTruncationFunc
}
Expand Down Expand Up @@ -335,8 +332,6 @@ func (m *Model[V]) UpdateViewport() {
renderedRows[i] = m.renderRow(m.start + i)
}

m.renderedRows = len(renderedRows)

m.viewport.SetContent(
lipgloss.JoinVertical(lipgloss.Left, renderedRows...),
)
Expand Down

0 comments on commit 38de2fd

Please sign in to comment.