Merge pull request #5 from aditya-K2/keymappings

Keymappings Generator
This commit is contained in:
Aditya Kurdunkar 2021-11-13 00:30:04 +05:30 committed by GitHub
commit 766cfd3d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 335 additions and 196 deletions

View File

@ -15,7 +15,7 @@ https://user-images.githubusercontent.com/51816057/140478368-5b724b2f-2499-4150-
- [ ] Visual Mode (like vim) for updating playlists
- [ ] Music Visualizer
# Prerequisites
# Setting Up
- Music Player Daemon must be setup
- Go Should Be Installed ( for building )
@ -30,6 +30,59 @@ cd goMP &&
go build
```
# Configuration
## Key Mappings
Following Keys can be used for Mappings
| Keys | Using them in Config |
|-----------------|-----------------------|
| a-z | a-z |
| A-Z | A-z |
| {,},(,),[,],<,> | {,},(,),[,],<,> |
| Enter(Return) | ENTER/RETURN |
| Tab | TAB |
| Space | SPACE |
See config/kMap.go for more information
For mapping a key to some function use the following format:
```yml
Function: [ firstMapping, secondMapping, thirdMapping]
```
for.eg
```yml
togglePlayBack : [ "p", "T" ] # using the quotes is neccessary.
```
Following functions are provided :
| Functions |
|------------------------------------|
| "showChildrenContent", |
| "togglePlayBack", |
| "showParentContent", |
| "nextSong", |
| "clearPlaylist", |
| "previousSong", |
| "addToPlaylist", |
| "toggleRandom", |
| "toggleRepeat", |
| "decreaseVolume", |
| "increaseVolume", |
| "navigateToFiles", |
| "navigateToPlaylist", |
| "navigateToMostPlayed", |
| "quit", |
| "stop", |
| "updateDB", |
| "deleteSongFromPlaylist", |
### Tested on following terminals:
- Alacritty

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.")
}
}

74
config/config.go Normal file
View File

@ -0,0 +1,74 @@
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 GenerateKeyMap(funcMap map[string]func()) {
for k := range funcMap {
mappingsForFunctionK := viper.GetStringSlice(k)
if len(mappingsForFunctionK) != 0 {
for _, i := range mappingsForFunctionK {
aV, err := GetAsciiValue(i)
if err == nil {
KEY_MAP[aV] = 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 ""
}

68
config/kMap.go Normal file
View File

@ -0,0 +1,68 @@
package config
import (
"errors"
)
var (
SPECIAL_KEYS = map[string]int{
"TAB": 9,
"RETURN": 13,
"ENTER": 13,
"SPACE": 32,
"[": 91,
"]": 93,
"(": 40,
")": 41,
"{": 123,
"}": 125,
"<": 60,
">": 62,
"?": 63,
"/": 47,
";": 59,
":": 58,
"'": 39,
"\"": 34,
}
// Generating Default KEY_MAP which will then later be changed by GenerateKeyMap
KEY_MAP = map[int]string{
108: "showChildrenContent",
112: "togglePlayBack",
104: "showParentContent",
110: "nextSong",
99: "clearPlaylist",
78: "previousSong",
97: "addToPlaylist",
122: "toggleRandom",
114: "toggleRepeat",
45: "decreaseVolume",
61: "increaseVolume",
50: "navigateToFiles",
49: "navigateToPlaylist",
51: "navigateToMostPlayed",
113: "quit",
115: "stop",
117: "updateDB",
100: "deleteSongFromPlaylist",
}
)
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 if val, ok := SPECIAL_KEYS[s]; ok {
return val, nil
} else {
return -1, errors.New("Not Found in the range")
}
} else if val, ok := SPECIAL_KEYS[s]; ok {
return val, nil
} else {
return -1, errors.New("Not Found")
}
}

36
config/kMap_test.go Normal file
View File

@ -0,0 +1,36 @@
package config
import (
"testing"
)
func TestGetAsciiValue(t *testing.T) {
for k, v := range SPECIAL_KEYS {
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)
}
}
result, err := GetAsciiValue("")
if err == nil || result != -1 {
t.Errorf("Wrong Result Received for Control Characters %v %v", result, err)
}
}

126
main.go
View File

@ -5,6 +5,7 @@ import (
"strconv"
"time"
"github.com/aditya-K2/goMP/config"
"github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2"
"github.com/spf13/viper"
@ -16,7 +17,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,10 +54,8 @@ func main() {
return UI.expandedView.GetInnerRect()
})
UI.expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
switch e.Rune() {
case 108: // L : Key
{
var FUNC_MAP = map[string]func(){
"showChildrenContent": func() {
r, _ := UI.expandedView.GetSelection()
if !InsidePlaylist {
if len(dirTree.children[r].children) == 0 {
@ -69,138 +68,107 @@ func main() {
} else {
conn.Play(r)
}
return nil
}
case 112: // P : Key
{
},
"togglePlayBack": func() {
togglePlayBack(*conn)
return nil
}
case 104: // H : Key
{
},
"showParentContent": func() {
if !InsidePlaylist {
if dirTree.parent != nil {
Update(*conn, dirTree.parent.children, UI.expandedView)
dirTree = dirTree.parent
}
}
return nil
}
case 110: // N : Key
{
},
"nextSong": func() {
conn.Next()
return nil
}
case 99: // C : Key
{
},
"clearPlaylist": func() {
conn.Clear()
if InsidePlaylist {
UpdatePlaylist(*conn, UI.expandedView)
}
return nil
}
case 78: // Shift - N : Key
{
},
"previousSong": func() {
conn.Previous()
return nil
}
case 97: // A : Key
{
},
"addToPlaylist": func() {
if !InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Add(dirTree.children[r].absolutePath)
}
return nil
}
case 122: // Z : Key
{
},
"toggleRandom": func() {
err := conn.Random(!Random)
if err == nil {
Random = !Random
}
return nil
}
case 114: // R : Key
{
},
"toggleRepeat": func() {
err := conn.Repeat(!Repeat)
if err == nil {
Repeat = !Repeat
}
return nil
}
case 45: // Minus : Key
{
},
"decreaseVolume": func() {
if Volume <= 0 {
Volume = 0
} else {
Volume -= 10
}
conn.SetVolume(int(Volume))
return nil
}
case 61: // Plus : Key
{
},
"increaseVolume": func() {
if Volume >= 100 {
Volume = 100
} else {
Volume += 10
}
conn.SetVolume(int(Volume))
return nil
}
case 50: // 2 : Key
{
},
"navigateToFiles": func() {
InsidePlaylist = false
UI.Navbar.Select(1, 0)
Update(*conn, dirTree.children, UI.expandedView)
return nil
}
case 49: // 1 : Key
{
},
"navigateToPlaylist": func() {
InsidePlaylist = true
UI.Navbar.Select(0, 0)
UpdatePlaylist(*conn, UI.expandedView)
return nil
}
case 51: // 3 : Key
{
},
"navigateToMostPlayed": func() {
InsidePlaylist = false
UI.Navbar.Select(2, 0)
return nil
}
case 113: // q : Key
{
},
"quit": func() {
UI.App.Stop()
return nil
}
case 115: // s: key
{
},
"stop": func() {
conn.Stop()
return nil
}
case 117: // u : key
{
},
"updateDB": func() {
_, err = conn.Update("")
if err != nil {
panic(err)
}
return nil
}
case 100: // d : key
{
},
"deleteSongFromPlaylist": func() {
if InsidePlaylist {
r, _ := UI.expandedView.GetSelection()
conn.Delete(r, -1)
}
},
}
config.GenerateKeyMap(FUNC_MAP)
UI.expandedView.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if val, ok := config.KEY_MAP[int(e.Rune())]; ok {
FUNC_MAP[val]()
return nil
} else {
return e
}
}
default:
{
return e
}
}
})
go func() {

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 ""
}