yay/utils.go

74 lines
1.5 KiB
Go
Raw Normal View History

2017-04-29 17:12:12 +00:00
package main
import (
"fmt"
"io"
"math"
"os"
"time"
"github.com/jguer/yay/aur"
2017-05-06 17:32:33 +00:00
"github.com/jguer/yay/config"
2017-04-29 17:12:12 +00:00
pac "github.com/jguer/yay/pacman"
)
// Complete provides completion info for shells
func complete() (err error) {
2017-05-06 17:32:33 +00:00
path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.YayConf.Shell + ".cache"
2017-04-29 17:12:12 +00:00
if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
2017-05-02 10:50:11 +00:00
os.MkdirAll(os.Getenv("HOME")+"/.cache/yay/", 0755)
2017-04-29 17:12:12 +00:00
out, err := os.Create(path)
if err != nil {
return err
}
if aur.CreateAURList(out) != nil {
defer os.Remove(path)
}
err = pac.CreatePackageList(out)
out.Close()
return err
}
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(os.Stdout, in)
return err
}
// Function by pyk https://github.com/pyk/byten
func index(s int64) float64 {
x := math.Log(float64(s)) / math.Log(1024)
return math.Floor(x)
}
// Function by pyk https://github.com/pyk/byten
func countSize(s int64, i float64) float64 {
return float64(s) / math.Pow(1024, math.Floor(i))
}
// Size return a formated string from file size
// Function by pyk https://github.com/pyk/byten
func size(s int64) string {
symbols := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
i := index(s)
if s < 10 {
return fmt.Sprintf("%dB", s)
}
size := countSize(s, i)
format := "%.0f"
if size < 10 {
format = "%.1f"
}
return fmt.Sprintf(format+"%s", size, symbols[int(i)])
}