aboutsummaryrefslogtreecommitdiff
path: root/steam
diff options
context:
space:
mode:
Diffstat (limited to 'steam')
-rw-r--r--steam/formatBytes.go28
-rw-r--r--steam/package.go20
-rw-r--r--steam/steam.go13
3 files changed, 56 insertions, 5 deletions
diff --git a/steam/formatBytes.go b/steam/formatBytes.go
new file mode 100644
index 0000000..8627b58
--- /dev/null
+++ b/steam/formatBytes.go
@@ -0,0 +1,28 @@
+package steam
+
+import (
+ "fmt"
+ "math"
+)
+
+func formatBytes(b int64) string {
+ if b < 1024 {
+ return fmt.Sprintf("%d b", b)
+ }
+
+ s := ""
+
+ pfxs := "kmgt"
+ for i := 0; i < len(pfxs); i++ {
+ pow := math.Pow(float64(1024), float64(i+1))
+ // This one is too big, return the previous string
+ if b < int64(pow) {
+ return s
+ }
+ s = fmt.Sprintf("%.2f %cb",
+ float64(b)/(pow),
+ pfxs[i])
+ }
+
+ return s
+}
diff --git a/steam/package.go b/steam/package.go
index cff086e..76914dc 100644
--- a/steam/package.go
+++ b/steam/package.go
@@ -96,3 +96,23 @@ func (g *Game) Delete() error {
return nil
}
+
+// Get the Size of a game in a pretty format
+func (g Game) GetSize() string {
+ return formatBytes(g.Size)
+}
+
+func (g *Game) setSizeInfo() error {
+ pth := filepath.Join(g.LibraryPath, "common", g.Name)
+ return filepath.Walk(pth, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if info.Mode().IsRegular() {
+ g.Size += info.Size()
+ }
+
+ return nil
+ })
+}
diff --git a/steam/steam.go b/steam/steam.go
index 8c3dcbb..24e4c54 100644
--- a/steam/steam.go
+++ b/steam/steam.go
@@ -3,10 +3,10 @@
package steam
import (
+ "bufio"
+ "errors"
"fmt"
"io/ioutil"
- "errors"
- "bufio"
"os"
"path/filepath"
"strings"
@@ -20,6 +20,7 @@ type Library struct {
type Game struct {
Name string
LibraryPath string
+ Size int64
}
func ProcessMultipleLibraries(r []string) ([]*Library, error) {
@@ -44,7 +45,7 @@ func NewLibrary(path string) (*Library, error) {
return l, err
}
-func NewLibraryMust(path string) (*Library) {
+func NewLibraryMust(path string) *Library {
l, err := NewLibrary(path)
if err != nil {
panic(err)
@@ -67,11 +68,13 @@ func (s *Library) ProcessLibrary(r string) error {
s.Folder = r
for _, f := range dirs {
if f.IsDir() {
- s.Games[f.Name()] = Game{
+ g := &Game{
Name: f.Name(),
LibraryPath: r,
}
- // s.Games = append(s.Games, f.Name())
+ g.setSizeInfo()
+
+ s.Games[f.Name()] = *g
}
}