diff options
| author | Jeff Carr <[email protected]> | 2025-10-06 23:10:00 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-06 23:10:00 -0500 |
| commit | b695089179b9003375a8c51ce45efeaffcc2e816 (patch) | |
| tree | 0e0dbdadae2efc4176719a8ad8ebe360b82d8b5e | |
| parent | 96b5157afc629037f436e5a6d6ec40b4915ff64c (diff) | |
awesome shortcuts
| -rw-r--r-- | humanFormat.go | 88 | ||||
| -rw-r--r-- | load.go | 17 | ||||
| -rw-r--r-- | save.go | 19 |
3 files changed, 123 insertions, 1 deletions
diff --git a/humanFormat.go b/humanFormat.go new file mode 100644 index 0000000..cf1f907 --- /dev/null +++ b/humanFormat.go @@ -0,0 +1,88 @@ +package config + +import ( + "fmt" + "time" +) + +// This isn't for the marketing department +// so this isn't going to use 'MiB' and 'GiB' +func HumanFormatBytes(b int) string { + if b < 2000 { + return fmt.Sprintf("%d B", b) + } + + kb := int(b / 1024) + if kb < 2000 { + return fmt.Sprintf("%d KB", kb) + } + + mb := int(b / (1024 * 1024)) + if mb < 2000 { + return fmt.Sprintf("%d MB", mb) + } + + gb := int(b / (1024 * 1024 * 1024)) + if gb < 2000 { + return fmt.Sprintf("%d GB", gb) + } + + tb := int(b / (1024 * 1024 * 1024 * 1024)) + return fmt.Sprintf("%d TB", tb) +} + +func FormatDuration(d time.Duration) string { + result := "" + + // check if it's more than a year + years := int(d.Hours()) / (24 * 365) + if years > 0 { + result += fmt.Sprintf("%dy", years) + return result + } + + // check if it's more than a day + days := int(d.Hours()) / 24 + if days > 0 { + result += fmt.Sprintf("%dd", days) + return result + } + + // check if it's more than an hour + hours := int(d.Hours()) % 24 + if hours > 0 { + result += fmt.Sprintf("%dh", hours) + return result + } + + // check if it's more than a minute + minutes := int(d.Minutes()) % 60 + if minutes > 0 { + result += fmt.Sprintf("%dm", minutes) + return result + } + + // check if it's more than a second + seconds := int(d.Seconds()) % 60 + if seconds > 0 { + result += fmt.Sprintf("%ds", seconds) + return result + } + + // report in milliseconds + ms := int(d.Milliseconds()) + if ms > 100 { + // todo: print .3s, etc ? + return fmt.Sprintf("%1.2fs", float64(seconds)/1000) + } + if ms > 0 { + result += fmt.Sprintf("%dms", ms) + } + + // totally not necessary but wth + var t time.Duration + t = time.Duration(ms) * time.Millisecond + nanos := d - t + result += fmt.Sprintf("%dnanos", nanos) + return result +} @@ -70,6 +70,23 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error { return ErrMarshal } +func Load(pb proto.Message) error { + fullname, ok := GetFilename(pb) + if !ok { + return ErrProtoFilename + } + if strings.HasSuffix(fullname, ".text") { + return loadTEXT(pb, fullname) + } + if strings.HasSuffix(fullname, ".json") { + return loadJSON(pb, fullname) + } + if strings.HasSuffix(fullname, ".pb") { + return loadPB(pb, fullname) + } + return log.Errorf("unknown filetype %s", fullname) +} + func LoadFile(pb proto.Message, fullname string) error { if strings.HasSuffix(fullname, ".text") { return loadTEXT(pb, fullname) @@ -19,6 +19,23 @@ func ConfigSave(pb proto.Message) error { return saveTEXT(pb, "") } +func Save(pb proto.Message) error { + fullname, ok := GetFilename(pb) + if !ok { + return ErrProtoFilename + } + if strings.HasSuffix(fullname, ".pb") { + SavePB(pb, fullname) + } + if strings.HasSuffix(fullname, ".text") { + return saveTEXT(pb, "") + } + if strings.HasSuffix(fullname, ".json") { + return saveJSON(pb) + } + return fmt.Errorf("unknown filetype %s", fullname) +} + func SavePB(pb proto.Message, fullname string) error { if !strings.HasSuffix(fullname, ".pb") { // todo: append .text here? @@ -38,7 +55,7 @@ func SavePB(pb proto.Message, fullname string) error { return err } - log.Infof("ConfigSave() filename=%s %d\n", fullname, len(data)) + log.Infof("ConfigSave() %s (%s)\n", fullname, HumanFormatBytes(len(data))) return configWrite(fullname, data) } |
