Skip to content

Commit

Permalink
FIXUP
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed Sep 9, 2024
1 parent 2d71891 commit f250a42
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 97 deletions.
12 changes: 6 additions & 6 deletions internal/cli/kraft/cloud/instance/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (opts *MetricsOptions) printTop(ctx context.Context) error {
{Title: "STATE"},
{Title: "UPTIME"},
{Title: "CPU"},
{Title: "MEM"},
{Title: "MEMORY"},
{Title: "NET RX / TX"},
},
func(ctx context.Context) ([]pstable.Row, error) {
Expand Down Expand Up @@ -153,16 +153,16 @@ func (opts *MetricsOptions) printTop(ctx context.Context) error {

rows[metric.UUID][3] = pstable.GuageCell{
Cs: iostreams.G(ctx).ColorScheme(),
Current: float64(metric.CPUTimeMs - previousMetrics[metric.UUID].CPUTimeMs),
Current: int(metric.CPUTimeMs - previousMetrics[metric.UUID].CPUTimeMs),
Max: 1000,
Width: 10,
Width: 5,
}

rows[metric.UUID][4] = pstable.GuageCell{
Cs: iostreams.G(ctx).ColorScheme(),
Current: float64(metric.RSS),
Max: float64(instancesMap[metric.UUID].MemoryMB) * 1024 * 1024,
Width: 10,
Current: int(metric.RSS),
Max: int(instancesMap[metric.UUID].MemoryMB) * 1024 * 1024,
Width: 5,
}
rows[metric.UUID][5] = pstable.StringCell(iostreams.Bold(fmt.Sprintf("%s / %s", humanize.Bytes(metric.RxBytes), humanize.Bytes(metric.TxBytes))))

Expand Down
15 changes: 11 additions & 4 deletions internal/cli/kraft/cloud/utils/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,23 +677,30 @@ func PrintServices(ctx context.Context, format string, resp kcclient.ServiceResp
// An internal utility method for printing a bar based on the provided progress
// and max values and the width of the bar.
func printBar(cs *iostreams.ColorScheme, progress, max int) string {
width := 36
builder := ProgressBarBuilder(cs, progress, max, 36)

return builder.String()
}

func ProgressBarBuilder(cs *iostreams.ColorScheme, progress, max, width int) *strings.Builder {
var ret strings.Builder

percent := math.Floor(float64(progress) / float64(max) * float64(width))
if percent >= float64(width) {
percent = 0
}

color := "green"
if percent > 30 { // ~83%
if percent*100/float64(width) > 83 {
color = "red"
} else if percent > 20 { // ~56%
} else if percent*100/float64(width) > 56 {
color = "yellow"
}

ret.WriteString(cs.ColorFromString(color)(strings.Repeat("█", int(percent))))
ret.WriteString(cs.ColorFromString(":243")(strings.Repeat(" ", width-int(percent))))

return ret.String()
return &ret
}

// PrintQuotas pretty-prints the provided set of user quotas or returns
Expand Down
110 changes: 23 additions & 87 deletions tui/pstable/pstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/text"
"kraftkit.sh/iostreams"
)
Expand All @@ -31,33 +32,25 @@ func (cell StringCell) String() string {

type GuageCell struct {
Cs *iostreams.ColorScheme
Current, Max float64
Current, Max int
Width int
}

func (cell GuageCell) String() string {
if cell.Width == 0 {
return " " // This is enough to cover "100%"
}

var ret strings.Builder
var ret *strings.Builder

percent := math.Floor(cell.Current / cell.Max * 100)
if percent > 100 {
percent = 100
if cell.Width != 0 {
ret = utils.ProgressBarBuilder(cell.Cs, cell.Current, cell.Max, cell.Width)
ret.WriteString(" ")
} else {
ret = &strings.Builder{}
}

color := "green"
if percent > 85 {
color = "red"
} else if percent > 65 {
color = "yellow"
percent := math.Floor(float64(cell.Current) / float64(cell.Max) * 100)
if percent > 100 {
percent = 0
}

ret.WriteString(cell.Cs.ColorFromString(color)(strings.Repeat("█", int(percent)/cell.Width)))
ret.WriteString(strings.Repeat(" ", cell.Width-(int(percent)/cell.Width)))

ret.WriteString(" ")
ret.WriteString(fmt.Sprintf("%.0f%%", percent))

return ret.String()
Expand Down Expand Up @@ -100,20 +93,21 @@ func (pstable *PsTable) Start(ctx context.Context) error {
pstable.table = table.New(
table.WithColumns(cols),
table.WithFocused(true),
table.WithWidth(256),
)

s := table.DefaultStyles()

s.Header = lipgloss.NewStyle().Bold(true)
s.Cell = lipgloss.NewStyle()
s.Cell = lipgloss.NewStyle().Padding(0)
s.Selected = s.Selected.
Background(lipgloss.AdaptiveColor{
Light: "0",
Dark: "0",
}).
Foreground(lipgloss.AdaptiveColor{
Light: "0",
Dark: "#AFAFAF",
Light: "#DFDFDF",
Dark: "#CFCFCF",
})
pstable.table.SetStyles(s)

Expand All @@ -137,9 +131,9 @@ func (pstable *PsTable) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

switch msg := msg.(type) {
case tea.WindowSizeMsg:
pstable.width = msg.Width
pstable.width = 256
pstable.height = msg.Height
pstable.table.SetWidth(msg.Width)
pstable.table.SetWidth(256)

case []Row:
if len(msg) > 0 {
Expand Down Expand Up @@ -197,7 +191,6 @@ func (pstable *PsTable) tick() tea.Cmd {
}

func (pstable *PsTable) calculateColumnWidths(rows []Row) []int {
delimSize := 2
numCols := len(rows[0])
allColWidths := make([][]int, numCols)

Expand All @@ -215,80 +208,23 @@ func (pstable *PsTable) calculateColumnWidths(rows []Row) []int {
maxColWidths[col] = widths[len(widths)-1]
}

colWidths := make([]int, numCols)

// Never truncate the first column
colWidths[0] = maxColWidths[0]

availWidth := func() int {
setWidths := 0
for col := 0; col < numCols; col++ {
setWidths += colWidths[col]
}

return pstable.width - delimSize*(numCols-1) - setWidths
}

numFixedCols := func() int {
fixedCols := 0

for col := 0; col < numCols; col++ {
if colWidths[col] > 0 {
fixedCols++
}
}

return fixedCols
}

// Set the widths of short columns
if w := availWidth(); w > 0 {
if numFlexColumns := numCols - numFixedCols(); numFlexColumns > 0 {
perColumn := w / numFlexColumns
for col := 0; col < numCols; col++ {
if max := maxColWidths[col]; max < perColumn {
colWidths[col] = max
}
}
}
}

firstFlexCol := -1

// truncate long columns to the remaining available width
if numFlexColumns := numCols - numFixedCols(); numFlexColumns > 0 {
perColumn := availWidth() / numFlexColumns
for col := 0; col < numCols; col++ {
if colWidths[col] == 0 {
if firstFlexCol == -1 {
firstFlexCol = col
}
if max := maxColWidths[col]; max < perColumn {
colWidths[col] = max
} else {
colWidths[col] = perColumn
}
}
}
}

// Add remainder to the first flex column
if w := availWidth(); w > 0 && firstFlexCol > -1 {
colWidths[firstFlexCol] += w
if max := maxColWidths[firstFlexCol]; max < colWidths[firstFlexCol] {
colWidths[firstFlexCol] = max
setWidths += maxColWidths[col]
}
return pstable.width - 2*numCols - setWidths
}

// Pad all the columns if there is still space left
if w := availWidth(); w > 0 {
padding := w / numCols
for col := 0; col < numCols; col++ {
colWidths[col] += padding
maxColWidths[col] += padding
}
}

return colWidths
return maxColWidths
}

func (pstable *PsTable) renderTitle() string {
Expand All @@ -298,8 +234,8 @@ func (pstable *PsTable) renderTitle() string {
Dark: "0",
}).
Foreground(lipgloss.AdaptiveColor{
Light: "15",
Dark: "0",
Light: "#DFDFDF",
Dark: "#CFCFCF",
}).
Width(pstable.width).
Render(pstable.title)
Expand Down

0 comments on commit f250a42

Please sign in to comment.