summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorSean E. Russell <seanerussell@gmail.com>2020-02-13 10:15:52 -0600
committerSean E. Russell <seanerussell@gmail.com>2020-02-13 10:15:52 -0600
commit7e5c0c969c223973335c6fae5432411afc3fb060 (patch)
tree7f785ba4f692b81806209cb5f0c959e86efdba70 /utils
parent4bfe0251a8893ed08654d59b8a8b8182958e907f (diff)
Fixes the directory structure.
Diffstat (limited to 'utils')
-rw-r--r--utils/bytes.go47
-rw-r--r--utils/conversions.go13
-rw-r--r--utils/math.go8
-rw-r--r--utils/xdg.go26
4 files changed, 94 insertions, 0 deletions
diff --git a/utils/bytes.go b/utils/bytes.go
new file mode 100644
index 0000000..b06cf8c
--- /dev/null
+++ b/utils/bytes.go
@@ -0,0 +1,47 @@
+package utils
+
+import (
+ "math"
+)
+
+var (
+ KB = uint64(math.Pow(2, 10))
+ MB = uint64(math.Pow(2, 20))
+ GB = uint64(math.Pow(2, 30))
+ TB = uint64(math.Pow(2, 40))
+)
+
+func CelsiusToFahrenheit(c int) int {
+ return c*9/5 + 32
+}
+
+func BytesToKB(b uint64) float64 {
+ return float64(b) / float64(KB)
+}
+
+func BytesToMB(b uint64) float64 {
+ return float64(b) / float64(MB)
+}
+
+func BytesToGB(b uint64) float64 {
+ return float64(b) / float64(GB)
+}
+
+func BytesToTB(b uint64) float64 {
+ return float64(b) / float64(TB)
+}
+
+func ConvertBytes(b uint64) (float64, string) {
+ switch {
+ case b < KB:
+ return float64(b), "B"
+ case b < MB:
+ return BytesToKB(b), "KB"
+ case b < GB:
+ return BytesToMB(b), "MB"
+ case b < TB:
+ return BytesToGB(b), "GB"
+ default:
+ return BytesToTB(b), "TB"
+ }
+}
diff --git a/utils/conversions.go b/utils/conversions.go
new file mode 100644
index 0000000..5d86ac4
--- /dev/null
+++ b/utils/conversions.go
@@ -0,0 +1,13 @@
+package utils
+
+import (
+ "strings"
+)
+
+func ConvertLocalizedString(s string) string {
+ if strings.ContainsAny(s, ",") {
+ return strings.Replace(s, ",", ".", 1)
+ } else {
+ return s
+ }
+}
diff --git a/utils/math.go b/utils/math.go
new file mode 100644
index 0000000..b710ee6
--- /dev/null
+++ b/utils/math.go
@@ -0,0 +1,8 @@
+package utils
+
+func MaxInt(a, b int) int {
+ if a > b {
+ return a
+ }
+ return b
+}
diff --git a/utils/xdg.go b/utils/xdg.go
new file mode 100644
index 0000000..8489042
--- /dev/null
+++ b/utils/xdg.go
@@ -0,0 +1,26 @@
+package utils
+
+import (
+ "os"
+ "path/filepath"
+)
+
+func GetConfigDir(name string) string {
+ var basedir string
+ if env := os.Getenv("XDG_CONFIG_HOME"); env != "" {
+ basedir = env
+ } else {
+ basedir = filepath.Join(os.Getenv("HOME"), ".config")
+ }
+ return filepath.Join(basedir, name)
+}
+
+func GetLogDir(name string) string {
+ var basedir string
+ if env := os.Getenv("XDG_STATE_HOME"); env != "" {
+ basedir = env
+ } else {
+ basedir = filepath.Join(os.Getenv("HOME"), ".local", "state")
+ }
+ return filepath.Join(basedir, name)
+}