Making a config package for Generating keymappings

Following changes have been made:
    1. Moving the getMusicDirectory() to config package
    2. Moving the config.go to config package
    3. Generating a Function Map that will be used for Generating keymappings in main.go
    4. Using the config packge in main.go
    5. First we are reading the user configuration values with
       config.ReadConfig() and then we are reading the mappings with
       config.ReadMappings() with the help of Function Map that is
       generated.
This commit is contained in:
aditya-K2 2021-11-11 21:57:01 +05:30
parent 2ecee3678a
commit 1572a460b0
3 changed files with 131 additions and 131 deletions

View File

@ -1,34 +0,0 @@
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": getMusicDirectory() + "/",
"PORT": "6600",
"DEFAULT_IMAGE_PATH": "default.jpg",
"COVER_IMAGE_PATH": "cover.jpg",
}
)
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.")
}
}

202
main.go
View File

@ -1,10 +1,12 @@
package main
import (
"fmt"
"log"
"strconv"
"time"
"github.com/aditya-K2/goMP/config"
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
@ -16,7 +18,7 @@ var Repeat bool
var InsidePlaylist bool = true
func main() {
readConfig()
config.ReadConfig()
// Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if err != nil {
@ -53,151 +55,209 @@ func main() {
return UI.expandedView.GetInnerRect()
})
var kMap = map[string]func(){
"showChildrenContent": func() {
r, _ := UI.expandedView.GetSelection()
if !InsidePlaylist {
if len(dirTree.children[r].children) == 0 {
id, _ := conn.AddId(dirTree.children[r].absolutePath, -1)
conn.PlayId(id)
} else {
Update(*conn, dirTree.children[r].children, UI.expandedView)
dirTree = &dirTree.children[r]
}
} else {
conn.Play(r)
}
},
"togglePlayBack": func() {
togglePlayBack(*conn)
},
"showParentContent": func() {
if !InsidePlaylist {
if dirTree.parent != nil {
Update(*conn, dirTree.parent.children, UI.expandedView)
dirTree = dirTree.parent
}
}
},
"nextSong": func() {
conn.Next()
},
"clearPlaylist": func() {
conn.Clear()
if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView)
}
},
"previousSong": func() {
conn.Previous()
},
"addToPlaylist": func() {
if !InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Add(dirTree.children[r].absolutePath)
}
},
"toggleRandom": func() {
err := conn.Random(!Random)
if err == nil {
Random = !Random
}
},
"toggleRepeat": func() {
err := conn.Repeat(!Repeat)
if err == nil {
Repeat = !Repeat
}
},
"decreaseVolume": func() {
if Volume <= 0 {
Volume = 0
} else {
Volume -= 10
}
conn.SetVolume(int(Volume))
},
"increaseVolume": func() {
if Volume >= 100 {
Volume = 100
} else {
Volume += 10
}
conn.SetVolume(int(Volume))
},
"navigateToFiles": func() {
InsidePlaylist = false
UI.Navbar.Select(1, 0)
Update(*conn, dirTree.children, UI.expandedView)
},
"navigateToPlaylist": func() {
InsidePlaylist = true
UI.Navbar.Select(0, 0)
UpdatePlaylist(*conn, UI.expandedView)
},
"navigateToMostPlayed": func() {
InsidePlaylist = false
UI.Navbar.Select(2, 0)
},
"quit": func() {
UI.App.Stop()
},
"stop": func() {
conn.Stop()
},
"updateDB": func() {
_, err = conn.Update("")
if err != nil {
panic(err)
}
},
"deleteSongFromPlaylist": func() {
if InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Delete(r, -1)
}
},
}
config.ReadMappings(kMap)
UI.expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
switch e.Rune() {
case 108: // L : Key
{
r, _ := UI.expandedView.GetSelection()
if !InsidePlaylist {
if len(dirTree.children[r].children) == 0 {
id, _ := conn.AddId(dirTree.children[r].absolutePath, -1)
conn.PlayId(id)
} else {
Update(*conn, dirTree.children[r].children, UI.expandedView)
dirTree = &dirTree.children[r]
}
} else {
conn.Play(r)
}
kMap["showChildrenContent"]()
return nil
}
case 112: // P : Key
{
togglePlayBack(*conn)
kMap["togglePlayBack"]()
return nil
}
case 104: // H : Key
{
if !InsidePlaylist {
if dirTree.parent != nil {
Update(*conn, dirTree.parent.children, UI.expandedView)
dirTree = dirTree.parent
}
}
kMap["showParentContent"]()
return nil
}
case 110: // N : Key
{
conn.Next()
kMap["nextSong"]()
return nil
}
case 99: // C : Key
{
conn.Clear()
if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView)
}
kMap["clearPlaylist"]()
return nil
}
case 78: // Shift - N : Key
{
conn.Previous()
kMap["previousSong"]()
return nil
}
case 97: // A : Key
{
if !InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Add(dirTree.children[r].absolutePath)
}
kMap["addToPlaylist"]()
return nil
}
case 122: // Z : Key
{
err := conn.Random(!Random)
if err == nil {
Random = !Random
}
kMap["toggleRandom"]()
return nil
}
case 114: // R : Key
{
err := conn.Repeat(!Repeat)
if err == nil {
Repeat = !Repeat
}
kMap["toggleRepeat"]()
return nil
}
case 45: // Minus : Key
{
if Volume <= 0 {
Volume = 0
} else {
Volume -= 10
}
conn.SetVolume(int(Volume))
kMap["decreaseVolume"]()
return nil
}
case 61: // Plus : Key
{
if Volume >= 100 {
Volume = 100
} else {
Volume += 10
}
conn.SetVolume(int(Volume))
kMap["increaseVolume"]()
return nil
}
case 50: // 2 : Key
{
InsidePlaylist = false
UI.Navbar.Select(1, 0)
Update(*conn, dirTree.children, UI.expandedView)
kMap["navigateToFiles"]()
return nil
}
case 49: // 1 : Key
{
InsidePlaylist = true
UI.Navbar.Select(0, 0)
UpdatePlaylist(*conn, UI.expandedView)
kMap["navigateToPlaylist"]()
return nil
}
case 51: // 3 : Key
{
InsidePlaylist = false
UI.Navbar.Select(2, 0)
kMap["navigateToMostPlayed"]()
return nil
}
case 113: // q : Key
{
UI.App.Stop()
kMap["quit"]()
return nil
}
case 115: // s: key
{
conn.Stop()
kMap["stop"]()
return nil
}
case 117: // u : key
{
_, err = conn.Update("")
if err != nil {
panic(err)
}
kMap["updateDB"]()
return nil
}
case 100: // d : key
{
if InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Delete(r, -1)
return nil
} else {
return e
}
kMap["deleteSongFromPlaylist"]()
return nil
}
default:
{
fmt.Println(e.Rune())
return e
}
}

View File

@ -1,8 +1,6 @@
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"syscall"
@ -82,27 +80,3 @@ func formatString(a interface{}) string {
return "Paused"
}
}
func getMusicDirectory() string {
content, err := ioutil.ReadFile(HOME_DIR + "/.config/mpd/mpd.conf")
if err != nil {
fmt.Println("No Config File for mpd Found")
panic(err)
}
ab := string(content)
maps := strings.Split(ab, "\n")
for _, j := range maps {
if strings.Contains(j, "music_directory") {
s := strings.SplitAfter(strings.ReplaceAll(j, " ", ""), "y")[1]
s = strings.ReplaceAll(s, "\t", "")
d := ""
for z, m := range s {
if (z != 0) && (z != (len(s) - 1)) {
d += string(m)
}
}
return d
}
}
return ""
}