first commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"ArinDash/apis/docker"
|
||||
"ArinDash/util"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mum4k/termdash/cell"
|
||||
"github.com/mum4k/termdash/terminal/terminalapi"
|
||||
"github.com/mum4k/termdash/widgetapi"
|
||||
"github.com/mum4k/termdash/widgets/text"
|
||||
)
|
||||
|
||||
type DockerListOptions struct {
|
||||
}
|
||||
|
||||
func DockerList() DockerListOptions {
|
||||
widgetOptions["DockerListOptions"] = createDockerList
|
||||
return DockerListOptions{}
|
||||
}
|
||||
|
||||
func createDockerList(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
|
||||
list := util.PanicOnErrorWithResult(text.New())
|
||||
|
||||
go util.Periodic(ctx, 1*time.Second, func() error {
|
||||
ms, err := docker.FetchDockerMetrics(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Slice(ms, func(i, j int) bool { return ms[i].CPUPercent > ms[j].CPUPercent })
|
||||
if err := list.Write(fmt.Sprintf("%-20s | %-6s | %-6s | %s\n", "Name", "CPU", "MEM", "Status"), text.WriteReplace()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("─────────────────────┼────────┼────────┼─────────────────────\n")); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range ms {
|
||||
status := cell.ColorWhite
|
||||
if strings.Contains(m.Status, "Exited") {
|
||||
status = cell.ColorRed
|
||||
}
|
||||
if strings.Contains(m.Status, "unhealthy") {
|
||||
status = cell.ColorYellow
|
||||
}
|
||||
if strings.Contains(m.Status, "Restarting") {
|
||||
status = cell.ColorYellow
|
||||
}
|
||||
if strings.Contains(m.Status, "Paused") {
|
||||
status = cell.ColorBlue
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%-20s", m.Name), text.WriteCellOpts(cell.FgColor(status))); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint(" | ")); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writePercent(m.CPUPercent, list); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint(" | ")); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writePercent(m.MemPercent, list); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprint(" | ")); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%s\n", m.Status), text.WriteCellOpts(cell.FgColor(status))); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
func writePercent(p float64, list *text.Text) error {
|
||||
color := cell.ColorWhite
|
||||
if p > 75 {
|
||||
color = cell.ColorRed
|
||||
}
|
||||
if p > 50 {
|
||||
color = cell.ColorYellow
|
||||
}
|
||||
if err := list.Write(fmt.Sprintf("%-5.1f%%", p), text.WriteCellOpts(cell.FgColor(color))); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user