Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
abs3ntdev committed Jan 4, 2023
1 parent 4b23f27 commit 8cccf39
Showing 1 changed file with 66 additions and 54 deletions.
120 changes: 66 additions & 54 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

func main() {
flags := flags.ReadFlags()

if flags.Version {
version := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
Expand All @@ -26,7 +25,6 @@ func main() {
fmt.Println("version:", version)
return
}

if !(len(os.Args) > 1) || flags.Help {
getopt.Usage()
return
Expand All @@ -44,69 +42,83 @@ func main() {
log.Println(err.Error())
return
}

if flags.Raw {
out := ""
for _, bind := range configValues.KeyboardBinds {
out += fmt.Sprintf("%s = %s %s %s", bind.BindType, bind.Bind, bind.Dispatcher, bind.Command) + "\n"
}
for _, bind := range configValues.MouseBinds {
out += fmt.Sprintf("%s = %s %s %s", bind.BindType, bind.Bind, bind.Dispatcher, bind.Command) + "\n"
}
for _, val := range configValues.Settings {
out += val.Name + " {" + "\n"
for setting, value := range val.Settings {
out += "\t" + setting + " = " + value + "\n"
}
for _, set := range val.SubCategories {
out += "\t" + set.Name + " {\n"
for setting, value := range set.Settings {
out += "\t\t" + setting + " = " + value + "\n"
}
out += "\t}\n"
}
out += "}\n"
}
fmt.Print(out)
if flags.Output != "" {
err := os.WriteFile(flags.Output, []byte(out), 0o644)
if err != nil {
log.Println(err)
return
}
}
rawHandler(configValues, flags)
}

if flags.Json {
out, err := json.MarshalIndent(configValues, "", " ")
jsonHandler(configValues, flags)
}

if flags.Markdown {
markdownHandler(configValues, flags)
}
}

func markdownHandler(configValues *reader.ConfigValues, flags *flags.Flags) error {
md := keybindsToMarkdown(configValues.KeyboardBinds, configValues.MouseBinds)
out := ""
out += "| Keybind | Dispatcher | Command |\n"
out += "|---------|------------|---------|\n"
for _, row := range md {
out += row + "\n"
}
fmt.Println(out)
if flags.Output != "" {
err := os.WriteFile(flags.Output, []byte(out), 0o644)
if err != nil {
log.Println(err.Error())
return
return err
}
fmt.Println(string(out))
if flags.Output != "" {
err := os.WriteFile(flags.Output, out, 0o644)
if err != nil {
log.Println(err)
return
}
}
return nil
}

func jsonHandler(configValues *reader.ConfigValues, flags *flags.Flags) error {
out, err := json.MarshalIndent(configValues, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
if flags.Output != "" {
err := os.WriteFile(flags.Output, out, 0o644)
if err != nil {
return err
}
}
if flags.Markdown {
md := keybindsToMarkdown(configValues.KeyboardBinds, configValues.MouseBinds)
out := ""
out += "| Keybind | Dispatcher | Command |\n"
out += "|---------|------------|---------|\n"
for _, row := range md {
out += row + "\n"
return nil
}

func rawHandler(configValues *reader.ConfigValues, flags *flags.Flags) error {
out := ""
for _, bind := range configValues.KeyboardBinds {
out += fmt.Sprintf("%s = %s %s %s", bind.BindType, bind.Bind, bind.Dispatcher, bind.Command) + "\n"
}
for _, bind := range configValues.MouseBinds {
out += fmt.Sprintf("%s = %s %s %s", bind.BindType, bind.Bind, bind.Dispatcher, bind.Command) + "\n"
}
for _, val := range configValues.Settings {
out += val.Name + " {" + "\n"
for setting, value := range val.Settings {
out += "\t" + setting + " = " + value + "\n"
}
fmt.Println(out)
if flags.Output != "" {
err := os.WriteFile(flags.Output, []byte(out), 0o644)
if err != nil {
log.Println(err)
return
for _, set := range val.SubCategories {
out += "\t" + set.Name + " {\n"
for setting, value := range set.Settings {
out += "\t\t" + setting + " = " + value + "\n"
}
out += "\t}\n"
}
out += "}\n"
}
fmt.Print(out)
if flags.Output != "" {
err := os.WriteFile(flags.Output, []byte(out), 0o644)
if err != nil {
return err
}
}
return nil
}

// Pass both kbKeybinds and mKeybinds to this function
Expand Down

0 comments on commit 8cccf39

Please sign in to comment.