Simplify projection table generation.
All checks were successful
Build & Test / Main (push) Successful in 1m28s
All checks were successful
Build & Test / Main (push) Successful in 1m28s
Instead of pre-allocating the grid and using tricky indexing to fill in the cells, just fully regenerate it. But do it columnwise first, and then transpose it.
This commit is contained in:
51
tui/tui.go
51
tui/tui.go
@@ -49,19 +49,9 @@ func New(cfg config.Data) Model {
|
||||
Width: 10,
|
||||
})
|
||||
}
|
||||
projectionRows := make([]table.Row, len(math.Goals)+2)
|
||||
for i := range projectionRows {
|
||||
projectionRows[i] = make(table.Row, len(projectionCols))
|
||||
}
|
||||
projectionRows[0][0] = "Starting"
|
||||
projectionRows[1][0] = "CDPR"
|
||||
for i := range math.Goals {
|
||||
projectionRows[i+2][0] = math.Goals[i].Name
|
||||
}
|
||||
projections := table.New(
|
||||
table.WithColumns(projectionCols),
|
||||
table.WithRows(projectionRows),
|
||||
table.WithHeight(len(math.Goals)+2),
|
||||
table.WithHeight(len(math.Labels)),
|
||||
table.WithStyles(tableStyle),
|
||||
)
|
||||
|
||||
@@ -106,30 +96,27 @@ func refillPrice(m *Model) {
|
||||
}
|
||||
|
||||
func refillProjections(m *Model) {
|
||||
rows := m.projections.Rows()
|
||||
rows := []table.Row{m.math.Labels}
|
||||
for i := range m.math.Columns {
|
||||
rows = append(rows, m.math.Columns[i].Column())
|
||||
}
|
||||
rows = transpose(rows)
|
||||
m.projections.SetRows(rows)
|
||||
}
|
||||
|
||||
for col := range m.math.Columns {
|
||||
_ = col
|
||||
never := false
|
||||
if m.math.Columns[col].CDPR <= 1 {
|
||||
never = true
|
||||
}
|
||||
|
||||
rows[0][col+1] = fmt.Sprintf("$%.2f", m.math.Columns[col].StartingPrice)
|
||||
rows[1][col+1] = fmt.Sprintf("%.2f%%", (m.math.Columns[col].CDPR-1)*100)
|
||||
for row := 0; row < len(m.math.Goals); row++ {
|
||||
var cell string
|
||||
if never {
|
||||
cell = "NEVER!!!!!"
|
||||
} else {
|
||||
cell = m.math.Columns[col].
|
||||
Projections.Dates[row].
|
||||
Format("2006-01-02")
|
||||
}
|
||||
rows[row+2][col+1] = cell
|
||||
func transpose(slice []table.Row) []table.Row {
|
||||
xl := len(slice[0])
|
||||
yl := len(slice)
|
||||
result := make([]table.Row, xl)
|
||||
for i := range result {
|
||||
result[i] = make(table.Row, yl)
|
||||
}
|
||||
for i := 0; i < xl; i++ {
|
||||
for j := 0; j < yl; j++ {
|
||||
result[i][j] = slice[j][i]
|
||||
}
|
||||
}
|
||||
m.projections.SetRows(rows)
|
||||
return result
|
||||
}
|
||||
|
||||
func (m Model) View() string {
|
||||
|
Reference in New Issue
Block a user