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

View File

@ -1,8 +1,6 @@
package main package main
import ( import (
"fmt"
"io/ioutil"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@ -82,27 +80,3 @@ func formatString(a interface{}) string {
return "Paused" 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 ""
}