2021-12-12 13:05:40 -07:00
|
|
|
package utils
|
2021-10-17 10:56:26 -06:00
|
|
|
|
|
|
|
import (
|
2021-12-08 14:04:49 -07:00
|
|
|
"io/ioutil"
|
2022-09-12 11:46:24 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-10-16 23:40:38 -06:00
|
|
|
"strconv"
|
2021-10-17 10:56:26 -06:00
|
|
|
"strings"
|
2021-10-28 22:38:09 -06:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2022-09-14 09:12:28 -06:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2021-10-16 23:40:38 -06:00
|
|
|
)
|
|
|
|
|
2021-10-28 22:38:09 -06:00
|
|
|
type winsize struct {
|
|
|
|
Row uint16
|
|
|
|
Col uint16
|
|
|
|
Xpixel uint16
|
|
|
|
Ypixel uint16
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:05:40 -07:00
|
|
|
func GetWidth() *winsize {
|
2021-10-28 22:38:09 -06:00
|
|
|
ws := &winsize{}
|
|
|
|
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
|
|
|
|
uintptr(syscall.Stdin),
|
|
|
|
uintptr(syscall.TIOCGWINSZ),
|
|
|
|
uintptr(unsafe.Pointer(ws)))
|
|
|
|
|
|
|
|
if int(retCode) == -1 {
|
|
|
|
panic(errno)
|
|
|
|
}
|
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:05:40 -07:00
|
|
|
func GetFontWidth() (float32, float32) {
|
|
|
|
g := GetWidth()
|
2022-03-15 15:18:53 -06:00
|
|
|
fw := float32(g.Xpixel) / float32(g.Col)
|
|
|
|
fh := float32(g.Ypixel) / float32(g.Row)
|
2021-10-28 22:38:09 -06:00
|
|
|
return fw, fh
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:05:40 -07:00
|
|
|
func StrTime(e float64) string {
|
2021-10-16 23:40:38 -06:00
|
|
|
a := int(e)
|
|
|
|
var min, seconds string
|
2021-10-17 10:56:26 -06:00
|
|
|
if a/60 < 10 {
|
2021-10-16 23:40:38 -06:00
|
|
|
min = "0"
|
2021-10-17 10:56:26 -06:00
|
|
|
min += strconv.Itoa(a / 60)
|
2021-10-16 23:40:38 -06:00
|
|
|
} else {
|
2021-10-17 10:56:26 -06:00
|
|
|
min = strconv.Itoa(a / 60)
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|
2021-10-17 10:56:26 -06:00
|
|
|
if a%60 < 10 {
|
2021-10-16 23:40:38 -06:00
|
|
|
seconds = "0"
|
2021-10-17 10:56:26 -06:00
|
|
|
seconds += strconv.Itoa(a % 60)
|
|
|
|
} else {
|
|
|
|
seconds = strconv.Itoa(a % 60)
|
2021-10-16 23:40:38 -06:00
|
|
|
}
|
|
|
|
return min + ":" + seconds
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:05:40 -07:00
|
|
|
func InsertAt(inputString, stringTobeInserted string, index int) string {
|
2021-10-17 10:56:26 -06:00
|
|
|
s := inputString[:index] + stringTobeInserted + inputString[index:]
|
2021-10-16 23:40:38 -06:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:05:40 -07:00
|
|
|
func ConvertToArray(ArtistTree map[string]map[string]map[string]string) []string {
|
2021-11-28 11:02:32 -07:00
|
|
|
var p []string
|
2021-12-12 13:05:40 -07:00
|
|
|
for k2, v := range ArtistTree {
|
2021-11-28 11:02:32 -07:00
|
|
|
p = append(p, k2)
|
|
|
|
for k1, v1 := range v {
|
|
|
|
p = append(p, k1)
|
|
|
|
for k := range v1 {
|
|
|
|
p = append(p, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-12-12 13:05:40 -07:00
|
|
|
func FormatString(a interface{}) string {
|
2021-10-17 01:45:52 -06:00
|
|
|
if a == "play" {
|
|
|
|
return "Playing"
|
|
|
|
} else if a == "1" {
|
|
|
|
return "On"
|
|
|
|
} else if a == "0" {
|
|
|
|
return "Off"
|
|
|
|
} else if a == "stop" {
|
|
|
|
return "Stopped"
|
|
|
|
} else {
|
|
|
|
return "Paused"
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 14:04:49 -07:00
|
|
|
|
|
|
|
func Copy(sourceImage, destinationImage string) error {
|
|
|
|
source, err := ioutil.ReadFile(sourceImage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
err = ioutil.WriteFile(destinationImage, source, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-12-12 13:05:40 -07:00
|
|
|
|
|
|
|
func GetFormattedString(s string, width int) string {
|
|
|
|
if len(s) < width {
|
2022-03-15 15:18:53 -06:00
|
|
|
s += strings.Repeat(" ", width-len(s))
|
2021-12-12 13:05:40 -07:00
|
|
|
} else {
|
|
|
|
s = s[:(width - 2)]
|
|
|
|
s += " "
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckDirectoryFmt(path string) string {
|
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
return path
|
|
|
|
} else {
|
|
|
|
return path + "/"
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 11:48:23 -07:00
|
|
|
|
2022-09-12 11:46:24 -06:00
|
|
|
func ExpandHomeDir(path string) string {
|
|
|
|
HOME_DIR, _ := os.UserHomeDir()
|
|
|
|
if strings.HasPrefix(path, "~/") {
|
|
|
|
return filepath.Join(HOME_DIR, path[1:])
|
|
|
|
} else if path == "~" {
|
|
|
|
return HOME_DIR
|
|
|
|
} else {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 09:52:34 -07:00
|
|
|
func GetMatchedString(a []int, s, color string) string {
|
|
|
|
// The Matches are sorted so we just have to traverse the Matches and if the two adjacent matches are not consecutive
|
|
|
|
// then we append the color string at the start + offset and the nulcol ( reset ) at end + offset + 1 and then reset
|
|
|
|
// start and end to a[k+1] for e.g if matches := []int{1, 2, 4, 5, 6, 9} then the start will be 1 and end will be 1
|
|
|
|
// now until we reach `4` the value of end will change to `2` that means when we reach `4` the s string will be
|
|
|
|
// `O[yellow:-:-]ut[-:-:-]putString` after that until we reach the end will be changed and finally become `6` and the
|
|
|
|
// s string will be `O[yellow:-:-]ut[-:-:-]p[yellow:-:-]utS[-:-:-]tring`
|
|
|
|
// Please note that after around 45 simulatenously highlighted characters tview stops highlighting and the color
|
|
|
|
// sequences are rendered hope no one has that big of search query.
|
|
|
|
start := a[0]
|
|
|
|
end := a[0]
|
|
|
|
offset := 0
|
|
|
|
nulcol := "[-:-:-]"
|
|
|
|
for k := range a {
|
|
|
|
if k < len(a)-1 && a[k+1]-a[k] == 1 {
|
|
|
|
end = a[k+1]
|
|
|
|
} else if k < len(a)-1 {
|
|
|
|
s = InsertAt(s, color, start+offset)
|
|
|
|
offset += len(color)
|
|
|
|
s = InsertAt(s, nulcol, end+offset+1)
|
|
|
|
offset += len(nulcol)
|
|
|
|
start = a[k+1]
|
|
|
|
end = a[k+1]
|
|
|
|
} else if k == len(a)-1 {
|
|
|
|
s = InsertAt(s, color, start+offset)
|
|
|
|
offset += len(color)
|
|
|
|
s = InsertAt(s, nulcol, end+offset+1)
|
|
|
|
offset += len(nulcol)
|
2021-12-25 11:48:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2021-12-29 11:28:21 -07:00
|
|
|
|
|
|
|
func Unique(intSlice []int) []int {
|
|
|
|
keys := make(map[int]bool)
|
2022-03-15 15:18:53 -06:00
|
|
|
var list []int
|
2021-12-29 11:28:21 -07:00
|
|
|
for _, entry := range intSlice {
|
|
|
|
if _, exists := keys[entry]; !exists {
|
|
|
|
keys[entry] = true
|
|
|
|
list = append(list, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
2022-09-14 09:12:28 -06:00
|
|
|
|
|
|
|
func GetNetwork() (string, string) {
|
|
|
|
del := ""
|
|
|
|
nt := viper.GetString("NETWORK_TYPE")
|
|
|
|
port := viper.GetString("MPD_PORT")
|
|
|
|
if nt == "tcp" {
|
|
|
|
del = ":"
|
|
|
|
} else if nt == "unix" && port != "" {
|
|
|
|
port = ""
|
|
|
|
}
|
|
|
|
return nt, viper.GetString("NETWORK_ADDRESS") + del + port
|
|
|
|
}
|