-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.go
47 lines (37 loc) · 1.17 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/rodaine/table"
)
func CreateOutput(servers []Server) table.Table {
headerFmt := color.New(color.FgWhite, color.Bold, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgHiBlue).SprintfFunc()
tbl := table.New("Server", "Name", "Index", "Free Memory", "Used Memory", "Total Memory")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, server := range servers {
name := server.Name
for _, gpu := range server.Devices {
tbl.AddRow(name, gpu.Name, gpu.Index, AlignRight(gpu.FreeMemory), AlignRight(gpu.UsedMemory), AlignRight(gpu.TotalMemory))
name = ""
}
}
return tbl
}
func AlignRight(s string) string {
return fmt.Sprintf("%10s", s)
}
// Returns a memory value in MiB.
func GetMemoryInMB(s string) (int, error) {
s = strings.TrimSpace(s)
if strings.HasSuffix(s, "MiB") {
return strconv.Atoi(strings.TrimSpace(strings.TrimSuffix(s, "MiB")))
}
if strings.HasSuffix(s, "GiB") {
gib, err := strconv.Atoi(strings.TrimSpace(strings.TrimSuffix(s, "GiB")))
return gib * 1024, err
}
return 0, fmt.Errorf("unknown memory format: %s", s)
}