interactive mode added to vm-cli example

toggle, get and set refactored into separate functions

flag vars moved from global  scope

-i flag added to README
This commit is contained in:
onyx-and-iris 2022-10-04 20:56:38 +01:00
parent 5b3f10c4b8
commit 8bb8336599
2 changed files with 100 additions and 42 deletions

View File

@ -13,6 +13,7 @@ Toggle with `!` prefix, get by excluding `=` and set by including `=`. Mix and m
You may pass the following optional flags: You may pass the following optional flags:
- -v: (-verbose) to toggle console output. - -v: (-verbose) to toggle console output.
- -i: (-interactive) to toggle interactive mode.
- -k: (-kind) to set the kind of Voicemeeter. Defaults to banana. - -k: (-kind) to set the kind of Voicemeeter. Defaults to banana.
- -d: (-delay) to set a delay on the getters. Defaults to 20ms. - -d: (-delay) to set a delay on the getters. Defaults to 20ms.

View File

@ -1,27 +1,32 @@
package main package main
import ( import (
"bufio"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"os"
"strings" "strings"
"github.com/onyx-and-iris/voicemeeter" "github.com/onyx-and-iris/voicemeeter"
) )
var ( func main() {
var (
kind string kind string
delay int delay int
verbose bool verbose bool
) interactive bool
)
func main() {
flag.StringVar(&kind, "kind", "banana", "kind of voicemeeter") flag.StringVar(&kind, "kind", "banana", "kind of voicemeeter")
flag.StringVar(&kind, "k", "banana", "kind of voicemeeter (shorthand)") flag.StringVar(&kind, "k", "banana", "kind of voicemeeter (shorthand)")
flag.IntVar(&delay, "delay", 20, "delay between commands") flag.IntVar(&delay, "delay", 20, "delay between commands")
flag.IntVar(&delay, "d", 20, "delay between commands (shorthand)") flag.IntVar(&delay, "d", 20, "delay between commands (shorthand)")
flag.BoolVar(&verbose, "verbose", false, "toggle console output") flag.BoolVar(&verbose, "verbose", false, "toggle console output")
flag.BoolVar(&verbose, "v", false, "toggle console output (shorthand)") flag.BoolVar(&verbose, "v", false, "toggle console output (shorthand)")
flag.BoolVar(&interactive, "interactive", false, "toggle interactive mode")
flag.BoolVar(&interactive, "i", false, "toggle interactive mode (shorthand)")
flag.Parse() flag.Parse()
vm, err := vmConnect(kind, delay) vm, err := vmConnect(kind, delay)
@ -30,7 +35,7 @@ func main() {
} }
defer vm.Logout() defer vm.Logout()
err = runCommands(vm, verbose) err = runCommands(vm, verbose, interactive)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -50,45 +55,97 @@ func vmConnect(kind string, delay int) (*voicemeeter.Remote, error) {
return vm, nil return vm, nil
} }
func runCommands(vm *voicemeeter.Remote, verbose bool) error { func runCommands(vm *voicemeeter.Remote, verbose, interactive bool) error {
if interactive {
return interactiveMode(vm, verbose)
}
for _, arg := range flag.Args() { for _, arg := range flag.Args() {
if arg[0] == '!' { err := parse(vm, arg, verbose)
val, err := vm.GetFloat(arg[1:])
if err != nil { if err != nil {
err = fmt.Errorf("unable to toggle %s", arg[1:])
return err return err
} }
vm.SetFloat(arg[1:], 1-val)
if verbose {
fmt.Println("Toggling", arg[1:])
} }
} else { return nil
if strings.Contains(arg, "=") { }
if verbose {
fmt.Println("Running command", arg) func interactiveMode(vm *voicemeeter.Remote, verbose bool) error {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input := scanner.Text()
if input == "q" || input == "quit" || input == "" {
return nil
} }
err := vm.SendText(arg) for _, cmd := range strings.Split(input, " ") {
err := parse(vm, cmd, verbose)
if err != nil { if err != nil {
err = fmt.Errorf("unable to set %s", arg)
return err return err
} }
} else { }
valF, err := vm.GetFloat(arg) }
if err != nil { return nil
valS, err := vm.GetString(arg) }
if err != nil {
err = fmt.Errorf("unable to get %s", arg) func parse(vm *voicemeeter.Remote, cmd string, verbose bool) error {
return err if cmd[0] == '!' {
} err := toggleCmd(vm, cmd[1:], verbose)
if verbose { if err != nil {
fmt.Println("Value of", arg, "is:", valS) return err
} }
} else { } else {
if verbose { if strings.Contains(cmd, "=") {
fmt.Println("Value of", arg, "is:", valF) err := setCmd(vm, cmd, verbose)
} if err != nil {
} return err
} }
} else {
err := getCmd(vm, cmd, verbose)
if err != nil {
return err
}
}
}
return nil
}
func toggleCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
val, err := vm.GetFloat(cmd)
if err != nil {
err = fmt.Errorf("unable to toggle %s", cmd)
return err
}
vm.SetFloat(cmd[1:], 1-val)
if verbose {
fmt.Println("Toggling", cmd)
}
return nil
}
func setCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
if verbose {
fmt.Println("Running command", cmd)
}
err := vm.SendText(cmd)
if err != nil {
err = fmt.Errorf("unable to set %s", cmd)
return err
}
return nil
}
func getCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
valF, err := vm.GetFloat(cmd)
if err != nil {
valS, err := vm.GetString(cmd)
if err != nil {
err = fmt.Errorf("unable to get %s", cmd)
return err
}
if verbose {
fmt.Println("Value of", cmd, "is:", valS)
}
} else {
if verbose {
fmt.Println("Value of", cmd, "is:", valF)
} }
} }
return nil return nil