Added a config parser

Add a config.yml/config.toml file to $HOME/.config/goMP/ so that user
configurations are read.
This commit is contained in:
aditya-K2 2021-11-05 12:40:46 +05:30
parent 8e05e1a865
commit 5ad7b90844
6 changed files with 46 additions and 12 deletions

32
config.go Normal file
View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
var (
HOME_DIR, _ = os.UserHomeDir()
defaults = map[string]interface{}{
"ADDITIONAL_PADDING_X": 12,
"ADDITIONAL_PADDING_Y": 16,
"IMAGE_WIDTH_EXTRA_X": -1.5,
"IMAGE_WIDTH_EXTRA_Y": -3.75,
"MUSIC_DIRECTORY": HOME_DIR + "/Music",
"PORT": "6600",
}
)
func readConfig() {
for k, v := range defaults {
viper.SetDefault(k, v)
}
viper.SetConfigName("config")
viper.AddConfigPath(HOME_DIR + "/.config/goMP")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("Could Not Read Config file.")
}
}

View File

@ -6,6 +6,7 @@ import (
"github.com/dhowden/tag"
"github.com/nfnt/resize"
"github.com/spf13/viper"
)
/*
@ -14,7 +15,7 @@ import (
path to default image is sent.
*/
func getAlbumArt(uri string) string {
var path string = "default.jpg"
var path string = "/H/code/goMP/default.jpg"
f, err := os.Open(uri)
if err != nil {
panic(err)
@ -25,13 +26,13 @@ func getAlbumArt(uri string) string {
}
albumCover := m.Picture()
if albumCover != nil {
b, err := os.Create("thumb.jpg")
b, err := os.Create("/H/code/goMP/thumb.jpg")
if err != nil {
panic(err)
}
defer b.Close()
b.Write(albumCover.Data)
path = "thumb.jpg"
path = "/H/code/goMP/thumb.jpg"
b.Close()
}
f.Close()
@ -53,7 +54,7 @@ func getImg(uri string) (image.Image, error) {
fw, fh := getFontWidth()
img = resize.Resize(
uint(float32(IMG_W)*(fw+IMAGE_WIDTH_EXTRA_X)), uint(float32(IMG_H)*(fh+IMAGE_WIDTH_EXTRA_Y)),
uint(float32(IMG_W)*(fw+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_X")))), uint(float32(IMG_H)*(fh+float32(viper.GetFloat64("IMAGE_WIDTH_EXTRA_Y")))),
img,
resize.Bilinear,
)

View File

@ -8,6 +8,7 @@ import (
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
)
var Volume int64
@ -16,9 +17,9 @@ var Repeat bool
var InsidePlaylist bool = true
func main() {
readConfig()
// Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600")
conn, err := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if err != nil {
log.Fatalln(err)
}
@ -27,7 +28,7 @@ func main() {
r := newRenderer()
c, _ := conn.CurrentSong()
if len(c) != 0 {
r.Start(DBDIR + c["file"])
r.Start(viper.GetString("MUSIC_DIRECTORY") + c["file"])
} else {
r.Start("stop")
}

View File

@ -7,6 +7,7 @@ import (
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/viper"
)
var CurrentSong string
@ -56,7 +57,7 @@ func (s *progressBar) updateTitle(conn mpd.Client, r *Renderer) {
CurrentSong = ""
r.Send("stop")
} else if song != CurrentSong && len(_currentAttributes) != 0 {
r.Send(DBDIR + _currentAttributes["file"])
r.Send(viper.GetString("music_directory") + _currentAttributes["file"])
CurrentSong = song
}
// fmt.Println(len(_currentAttributes))

View File

@ -1,6 +1,7 @@
package main
import (
"github.com/spf13/viper"
"gitlab.com/diamondburned/ueberzug-go"
)
@ -43,7 +44,7 @@ func openImage(path string, c chan string) {
var im *ueberzug.Image
if path != "stop" {
img2, _ := getImg(getAlbumArt(path))
im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+ADDITIONAL_PADDING_X, int(float32(IMG_Y)*fh)+ADDITIONAL_PADDING_Y)
im, _ = ueberzug.NewImage(img2, int(float32(IMG_X)*fw)+viper.GetInt("ADDITIONAL_PADDING_X"), int(float32(IMG_Y)*fh)+viper.GetInt("ADDITIONAL_PADDING_Y"))
}
d := <-c
if im != nil {

View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
@ -85,8 +84,7 @@ func formatString(a interface{}) string {
}
func getMusicDirectory() string {
a, _ := os.UserHomeDir()
content, err := ioutil.ReadFile(a + "/.config/mpd/mpd.conf")
content, err := ioutil.ReadFile(HOME_DIR + "/.config/mpd/mpd.conf")
if err != nil {
fmt.Println("No Config File for mpd Found")
panic(err)