Reading Mappings from config file
Following Changes have been made in addition to
`1572a460b0
`
1. config.go :
ReadMappings Reads Array for each function
Basically we are going to define mappings like this :
```yml
Function :
- first mapping
- second mapping
- third mapping
`````
2. kMap.go :
GetAsciiValues will help us to get e.Rune() for the event for
the handler function. So basically we will have a keymappings map
generated which will help us to handle events.
for e.g
if an user has defined following mappings
````yml
togglePlayBack :
- P
- p
- B
````
then the keymappings map will look like this
[]keymappings = {
GetAsciiValues("P") : togglePlayBack(),
GetAsciiValues("p") : togglePlayBack(),
GetAsciiValues("B") : togglePlayBack(),
}
so when the handler function will get an event e
we will just pass it to this keymappings map i.e
keymappings[e] which will return the function
This commit is contained in:
parent
1572a460b0
commit
9c039a8681
66
config/config.go
Normal file
66
config/config.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadMappings(funcMap map[string]func()) {
|
||||||
|
for k := range funcMap {
|
||||||
|
fmt.Println(k, " : ", viper.GetStringSlice(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ""
|
||||||
|
}
|
27
config/kMap.go
Normal file
27
config/kMap.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var KMAP = map[string]int{
|
||||||
|
"TAB": 9,
|
||||||
|
"RETURN": 13,
|
||||||
|
"ENTER": 13,
|
||||||
|
"SPACE": 32,
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAsciiValue(s string) (int, error) {
|
||||||
|
if len([]rune(s)) == 1 {
|
||||||
|
char := []rune(s)[0]
|
||||||
|
if (int(char) >= 65 && int(char) <= 90) || (int(char) >= 97 && int(char) <= 122) {
|
||||||
|
return int(char), nil
|
||||||
|
} else {
|
||||||
|
return -1, errors.New("Not Found")
|
||||||
|
}
|
||||||
|
} else if val, ok := KMAP[s]; ok {
|
||||||
|
return val, nil
|
||||||
|
} else {
|
||||||
|
return -1, errors.New("Not Found")
|
||||||
|
}
|
||||||
|
}
|
32
config/kMap_test.go
Normal file
32
config/kMap_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetAsciiValue(t *testing.T) {
|
||||||
|
for k, v := range KMAP {
|
||||||
|
result, err := GetAsciiValue(k)
|
||||||
|
if result != v {
|
||||||
|
t.Errorf("Values From KMAP Failed")
|
||||||
|
} else if err != nil {
|
||||||
|
t.Errorf("Error Received: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 65; i < (65 + 26); i++ {
|
||||||
|
result, err := GetAsciiValue(string(rune(i)))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error Received! %v", err)
|
||||||
|
} else if result != i {
|
||||||
|
t.Errorf("Invalid Value Received for small alphabets Result Received: %d expecting %d", result, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 97; i < (97 + 26); i++ {
|
||||||
|
result, err := GetAsciiValue(string(rune(i)))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error Received: %v", err)
|
||||||
|
} else if result != i {
|
||||||
|
t.Errorf("Invalid Value Received for Big alphabets Result Received: %d expecting %d", result, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user