Skip to content

Commit

Permalink
refactor and bugfix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Edru2 committed Aug 20, 2024
1 parent 7fc65c4 commit f80a42c
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 239 deletions.
62 changes: 32 additions & 30 deletions AliasFunctions.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
package main

import (
"encoding/json"
"os"
"path/filepath"
"strings"
)

func handleAliases(aliasList *[]Alias, parentDir string) {
func writeAliasList(aliasList *[]AliasEntity, parentDir string, seenNames map[string]bool) {
for i := range *aliasList {
alias := &(*aliasList)[i]
if alias.IsFolder == "no" {
handleAliases(&alias.AliasList, parentDir, seenNames)
}
writeScriptFiles(alias, parentDir, seenNames)
}
}

func writeAliasJson(aliasList *[]AliasEntity, parentDir string) {
writeJsonToFilewriteJsonToFile(aliasList, parentDir, "aliases.json")
}

func handleAliases(aliasList *[]AliasEntity, parentDir string, seenNames map[string]bool) {
if len(*aliasList) == 0 {
return
}
var jsonFile []Alias
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}

for _, alias := range *aliasList {
aliasFileName := strings.ReplaceAll(alias.Name, " ", "_")
aliasFilePath := filepath.Join(parentDir, aliasFileName+".lua")
if len(alias.Script) > 0 && !containsIllegalCharacters(aliasFileName) {
if err := os.WriteFile(aliasFilePath, []byte(alias.Script), 0644); err != nil {
panic(err)
}
alias.Script = ""
writeAliasList(aliasList, parentDir, seenNames)
writeAliasJson(aliasList, parentDir)
}
func handleAliasGroups(groups *[]AliasEntity, baseDir string, seenNames map[string]bool) {
for i := range *groups {
group := &(*groups)[i]

groupPath := filepath.Join(baseDir, group.Name)
if err := os.MkdirAll(groupPath, 0755); err != nil {
panic(err)
}
jsonFile = append(jsonFile, alias)
}

jsonFilePath := filepath.Join(parentDir, "aliases.json")
jsonData, err := json.MarshalIndent(jsonFile, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(jsonFilePath, jsonData, 0644)
if err != nil {
panic(err)
handleAliases(&group.AliasList, groupPath, seenNames)
handleAliasGroups(&group.AliasGroup, groupPath, seenNames)
group.AliasList = nil
var singleGroup []AliasEntity
singleGroup = append(singleGroup, *group)
writeAliasList(&singleGroup, baseDir, seenNames)
writeAliasJson(&singleGroup, baseDir)
}

}

func handleAliasGroups(groups *[]AliasGroup, baseDir string) {
for i := range *groups {
groupPath := filepath.Join(baseDir, (*groups)[i].Name)
handleAliases(&((*groups)[i].AliasList), groupPath)
handleAliasGroups(&((*groups)[i].AliasGroup), groupPath)
}
}
36 changes: 22 additions & 14 deletions AliasModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ import (
)

type AliasPackage struct {
XMLName xml.Name `xml:"AliasPackage" json:"-"`
AliasList []Alias `xml:"Alias" json:"-"`
AliasGroup []AliasGroup `xml:"AliasGroup" json:"-"`
XMLName xml.Name `xml:"AliasPackage" json:"-"`
AliasList []AliasEntity `xml:"Alias" json:"-"`
AliasGroup []AliasEntity `xml:"AliasGroup" json:"-"`
}

type Alias struct {
IsActive string `xml:"isActive,attr" json:"isActive"`
IsFolder string `xml:"isFolder,attr" json:"isFolder"`
Name string `xml:"name" json:"name"`
Regex string `xml:"regex" json:"regex,omitempty"`
Script string `xml:"script" json:"script"`
Command string `xml:"command" json:"command,omitempty"`
AliasList []Alias `xml:"Alias" json:"-"`
AliasGroup []AliasGroup `xml:"AliasGroup" json:"-"`
type AliasEntity struct {
Name string `xml:"name" json:"name"`
IsActive string `xml:"isActive,attr" json:"isActive"`
Command string `xml:"command" json:"command,omitempty"`
Regex string `xml:"regex" json:"regex,omitempty"`
Script string `xml:"script" json:"script"`
AliasList []AliasEntity `xml:"Alias" json:"children,omitempty"`
IsFolder string `xml:"isFolder,attr" json:"isFolder"`
AliasGroup []AliasEntity `xml:"AliasGroup" json:"-"`
}

type AliasGroup struct {
Alias
func (a *AliasEntity) GetName() string {
return a.Name
}

func (a *AliasEntity) GetScript() string {
return a.Script
}

func (a *AliasEntity) SetScript(script string) {
a.Script = script
}
71 changes: 38 additions & 33 deletions KeyFunctions.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,66 @@
package main

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)

func handleKeys(keys *[]Key, parentDir string) {
func writeKeyList(keys *[]KeyEntity, parentDir string, seenNames map[string]bool) {
for i := range *keys {
key := &(*keys)[i]
if key.IsFolder == "no" {
handleKeys(&key.KeyList, parentDir, seenNames)
}
writeScriptFiles(key, parentDir, seenNames)
}
}

func writeKeyJson(keys *[]KeyEntity, parentDir string) {

for i := range *keys {
key := &(*keys)[i]
convertKeyCodes(key)
}
writeJsonToFilewriteJsonToFile(keys, parentDir, "keys.json")
}

func handleKeys(keys *[]KeyEntity, parentDir string, seenNames map[string]bool) {
if len(*keys) == 0 {
return
}
var jsonFile []Key
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}

for _, key := range *keys {
keyFileName := strings.ReplaceAll(key.Name, " ", "_")
keyFilePath := filepath.Join(parentDir, keyFileName+".lua")
if len(key.Script) > 0 && !containsIllegalCharacters(keyFileName) {
if err := os.WriteFile(keyFilePath, []byte(key.Script), 0644); err != nil {
panic(err)
}
key.Script = ""
}
key = convertKeyCodes(key)
jsonFile = append(jsonFile, key)
}

jsonFilePath := filepath.Join(parentDir, "keys.json")
jsonData, err := json.MarshalIndent(jsonFile, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(jsonFilePath, jsonData, 0644)
if err != nil {
panic(err)
}

writeKeyList(keys, parentDir, seenNames)
writeKeyJson(keys, parentDir)
}

func handleKeyGroups(groups *[]KeyGroup, baseDir string) {
func handleKeyGroups(groups *[]KeyEntity, baseDir string, seenNames map[string]bool) {
for i := range *groups {
groupPath := filepath.Join(baseDir, (*groups)[i].Name)
handleKeys(&((*groups)[i].KeyList), groupPath)
handleKeyGroups(&((*groups)[i].KeyGroup), groupPath)
group := &(*groups)[i]

groupPath := filepath.Join(baseDir, group.Name)
if err := os.MkdirAll(groupPath, 0755); err != nil {
panic(err)
}

handleKeys(&group.KeyList, groupPath, seenNames)
handleKeyGroups(&group.KeyGroup, groupPath, seenNames)
group.KeyList = nil
var singleGroup []KeyEntity
singleGroup = append(singleGroup, *group)
writeKeyList(&singleGroup, baseDir, seenNames)
writeKeyJson(&singleGroup, baseDir)
}
}

func convertKeyCodes(key Key) Key {
func convertKeyCodes(key *KeyEntity){
var pressedKeys []string
for _, mod := range keyModifiers {
value, err := strconv.ParseInt(key.KeyModifier, 10, 32)
Expand All @@ -74,6 +81,4 @@ func convertKeyCodes(key Key) Key {
pressedKeys = append(pressedKeys, val)
}
key.Keys = strings.Join(pressedKeys, "+")

return key
}
44 changes: 26 additions & 18 deletions KeyModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@ import (
)

type KeyPackage struct {
XMLName xml.Name `xml:"KeyPackage" json:"-"`
KeyList []Key `xml:"Key" json:"-"`
KeyGroup []KeyGroup `xml:"KeyGroup" json:"-"`
XMLName xml.Name `xml:"KeyPackage" json:"-"`
KeyList []KeyEntity `xml:"Key" json:"-"`
KeyGroup []KeyEntity `xml:"KeyGroup" json:"-"`
}

type Key struct {
IsActive string `xml:"isActive,attr" json:"isActive"`
IsFolder string `xml:"isFolder,attr" json:"isFolder"`
Name string `xml:"name" json:"name"`
Command string `xml:"command" json:"command"`
Script string `xml:"script" json:"script"`
Keys string `xml:"-" json:"keys"`
KeyCode string `xml:"keyCode" json:"-"`
KeyModifier string `xml:"keyModifier" json:"-"`
KeyList []Key `xml:"Key" json:"-"`
KeyGroup []KeyGroup `xml:"KeyGroup" json:"-"`
}

type KeyGroup struct {
Key
type KeyEntity struct {
Name string `xml:"name" json:"name"`
IsActive string `xml:"isActive,attr" json:"isActive"`
Command string `xml:"command" json:"command"`
Script string `xml:"script" json:"script"`
Keys string `xml:"-" json:"keys"`
KeyCode string `xml:"keyCode" json:"-"`
KeyModifier string `xml:"keyModifier" json:"-"`
KeyList []KeyEntity `xml:"Key" json:"children,omitempty"`
IsFolder string `xml:"isFolder,attr" json:"isFolder"`
KeyGroup []KeyEntity `xml:"KeyGroup" json:"-"`
}

type Modifier struct {
Expand Down Expand Up @@ -131,3 +127,15 @@ var keyMap = map[int]string{
0x2e: ".",
0x7c: "|",
}

func (k *KeyEntity) GetName() string {
return k.Name
}

func (k *KeyEntity) GetScript() string {
return k.Script
}

func (k *KeyEntity) SetScript(script string) {
k.Script = script
}
62 changes: 32 additions & 30 deletions ScriptFunctions.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
package main

import (
"encoding/json"
"os"
"path/filepath"
"strings"
)

func handleScripts(scripts *[]Script, parentDir string) {
func writeScripts(scripts *[]ScriptEntity, parentDir string, seenNames map[string]bool) {
for i := range *scripts {
script := &(*scripts)[i]
if script.IsFolder == "no" {
handleScripts(&script.Scripts, parentDir, seenNames)
}
writeScriptFiles(script, parentDir, seenNames)
}
}

func writeScriptJson(scripts *[]ScriptEntity, parentDir string) {
writeJsonToFilewriteJsonToFile(scripts, parentDir, "scripts.json")
}

func handleScripts(scripts *[]ScriptEntity, parentDir string, seenNames map[string]bool) {
if len(*scripts) == 0 {
return
}
var jsonFile []Script
if parentDir != "" {
if err := os.MkdirAll(parentDir, 0755); err != nil {
panic(err)
}
}

for _, script := range *scripts {
scriptFileName := strings.ReplaceAll(script.Name, " ", "_")
scriptFilePath := filepath.Join(parentDir, scriptFileName+".lua")
if len(script.Script) > 0 && !containsIllegalCharacters(scriptFileName) {
if err := os.WriteFile(scriptFilePath, []byte(script.Script), 0644); err != nil {
panic(err)
}
script.Script = ""
}
jsonFile = append(jsonFile, script)
}

jsonFilePath := filepath.Join(parentDir, "scripts.json")
jsonData, err := json.MarshalIndent(jsonFile, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(jsonFilePath, jsonData, 0644)
if err != nil {
panic(err)
}

writeScripts(scripts, parentDir, seenNames)
writeScriptJson(scripts, parentDir)
}

func handleScriptGroups(groups *[]ScriptGroup, baseDir string) {
func handleScriptGroups(groups *[]ScriptEntity, baseDir string, seenNames map[string]bool) {
for i := range *groups {
groupPath := filepath.Join(baseDir, (*groups)[i].Name)
handleScripts(&((*groups)[i].Scripts), groupPath)
handleScriptGroups(&((*groups)[i].ScriptGroup), groupPath)
group := &(*groups)[i]

groupPath := filepath.Join(baseDir, group.Name)
if err := os.MkdirAll(groupPath, 0755); err != nil {
panic(err)
}

handleScripts(&group.Scripts, groupPath, seenNames)
handleScriptGroups(&group.ScriptGroup, groupPath, seenNames)
group.Scripts = nil
var singleGroup []ScriptEntity
singleGroup = append(singleGroup, *group)
writeScripts(&singleGroup, baseDir, seenNames)
writeScriptJson(&singleGroup, baseDir)
}
}
Loading

0 comments on commit f80a42c

Please sign in to comment.