package assets
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.brettb.xyz/goinv/client/internal/ui/style"
|
|
"github.com/rivo/tview"
|
|
)
|
|
|
|
const tableHeaderOffset = 1
|
|
|
|
func (a *Assets) refresh() {
|
|
assets := a.getAssetData()
|
|
a.assetTable.Clear()
|
|
a.updateAssetTableTitle(len(assets))
|
|
|
|
a.writeTableHeaders()
|
|
|
|
for i, asset := range assets {
|
|
row := i + tableHeaderOffset
|
|
|
|
a.assetTable.SetCell(row, 0,
|
|
tview.NewTableCell(fmt.Sprintf("%d", asset.ID)).
|
|
SetExpansion(a.assetTableExpansions[0]).
|
|
SetAlign(tview.AlignLeft))
|
|
|
|
a.assetTable.SetCell(row, 1,
|
|
tview.NewTableCell(asset.Name).
|
|
SetExpansion(a.assetTableExpansions[1]).
|
|
SetAlign(tview.AlignLeft))
|
|
|
|
quantity := ""
|
|
if asset.Quantity < 0 {
|
|
quantity = "DNI"
|
|
} else {
|
|
quantity = fmt.Sprintf("%d", asset.Quantity)
|
|
}
|
|
|
|
a.assetTable.SetCell(row, 2,
|
|
tview.NewTableCell(quantity).
|
|
SetExpansion(a.assetTableExpansions[2]).
|
|
SetAlign(tview.AlignLeft))
|
|
|
|
shelfLocation := ""
|
|
if asset.ShelfLocation != nil {
|
|
shelfLocation = asset.ShelfLocation.Name
|
|
}
|
|
|
|
a.assetTable.SetCell(row, 3,
|
|
tview.NewTableCell(shelfLocation).
|
|
SetExpansion(a.assetTableExpansions[3]).
|
|
SetAlign(tview.AlignLeft))
|
|
|
|
a.assetTable.SetCell(row, 4,
|
|
tview.NewTableCell(asset.Manufacturer).
|
|
SetExpansion(a.assetTableExpansions[4]).
|
|
SetAlign(tview.AlignLeft))
|
|
|
|
a.assetTable.SetCell(row, 5,
|
|
tview.NewTableCell(asset.ModelName).
|
|
SetExpansion(a.assetTableExpansions[5]).
|
|
SetAlign(tview.AlignLeft))
|
|
|
|
category := ""
|
|
if asset.Category != nil {
|
|
category = asset.Category.Name
|
|
}
|
|
|
|
a.assetTable.SetCell(row, 6,
|
|
tview.NewTableCell(category).
|
|
SetExpansion(a.assetTableExpansions[6]).
|
|
SetAlign(tview.AlignLeft))
|
|
}
|
|
}
|
|
|
|
func (a *Assets) updateAssetTableTitle(count int) {
|
|
dirtyFlag := ""
|
|
if a.assetList.dirty {
|
|
dirtyFlag = "*"
|
|
}
|
|
title := fmt.Sprintf("[::b]ASSETS [%s%d]", dirtyFlag, count)
|
|
a.assetTable.SetTitle(title)
|
|
}
|
|
|
|
func (a *Assets) writeTableHeaders() {
|
|
for i, headerText := range a.assetTableHeaders {
|
|
header := fmt.Sprintf("[::b]%s", strings.ToUpper(headerText))
|
|
a.assetTable.SetCell(0, i,
|
|
tview.NewTableCell(header).
|
|
SetExpansion(a.assetTableExpansions[i]).
|
|
SetBackgroundColor(style.TableHeaderBgColor).
|
|
SetTextColor(style.TableHeaderFgColor).
|
|
SetAlign(tview.AlignLeft).
|
|
SetSelectable(false))
|
|
}
|
|
}
|