Files
pcinv/template_funcs.go

120 lines
2.0 KiB
Go

package main
import (
"fmt"
)
func createDeviceLink(deviceType, name string, qr *int) CreateDeviceLink {
return CreateDeviceLink{
Type: deviceType,
Name: name,
Qr: qr,
}
}
type CreateDeviceLink struct {
Type string
Name string
Qr *int
}
func formatMemoryUnit(size int) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
)
switch {
case size >= GB:
return "GB"
case size >= MB:
return "MB"
case size >= KB:
return "KB"
default:
return "B"
}
}
func formatMemorySize(size int) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
)
switch formatMemoryUnit(size) {
case "GB":
return fmt.Sprintf("%.2f GB", float64(size)/GB)
case "MB":
return fmt.Sprintf("%.2f MB", float64(size)/MB)
case "KB":
return fmt.Sprintf("%.2f KB", float64(size)/KB)
case "B":
return fmt.Sprintf("%d B", size)
default:
panic("invalid memory size")
}
}
func formatMemoryPlainSize(size int) int {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
)
switch formatMemoryUnit(size) {
case "GB":
return size / GB
case "MB":
return size / MB
case "KB":
return size / KB
case "B":
return size
default:
panic("invalid memory size")
}
}
func isRamType(size int, unit string) bool {
if size == 0 && unit == "MB" {
return true
}
actualUnit := formatMemoryUnit(size)
return unit == actualUnit
}
func formatType(t string) string {
for _, assetType := range DescriptorTree.AssetTypes {
if assetType.Id == t {
return assetType.Name
}
}
panic("unknown type")
}
type SelectMenu struct {
Name string
Label string
Selected string
Options []string
DefaultValue string
}
func createSelectMenu(name, label, selected string, options []string) SelectMenu {
return createSelectMenuDefault(name, label, selected, options, "Unknown")
}
func createSelectMenuDefault(name, label, selected string, options []string, defaultValue string) SelectMenu {
return SelectMenu{
Name: name,
Label: label,
Selected: selected,
Options: options,
DefaultValue: defaultValue,
}
}