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:
- -v: (-verbose) to toggle console output.
- -i: (-interactive) to toggle interactive mode.
- -k: (-kind) to set the kind of Voicemeeter. Defaults to banana.
- -d: (-delay) to set a delay on the getters. Defaults to 20ms.

View File

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