add verbosePrinter to vm-cli example

add info exit from interactive in README
This commit is contained in:
onyx-and-iris 2022-10-05 21:34:59 +01:00
parent c7a265fb33
commit 174db162c8
2 changed files with 50 additions and 28 deletions

View File

@ -31,3 +31,5 @@ Value of strip[0].mute is: 1
Running command bus[0].gain=-8.8 Running command bus[0].gain=-8.8
Running command command.lock=1 Running command command.lock=1
``` ```
If running in interactive mode enter `q`, `quit` or `<Enter>` to exit.

View File

@ -11,11 +11,34 @@ import (
"github.com/onyx-and-iris/voicemeeter" "github.com/onyx-and-iris/voicemeeter"
) )
type (
verbosePrinter struct {
verbose bool
}
)
func newVerbosePrinter() *verbosePrinter {
return &verbosePrinter{}
}
func (v *verbosePrinter) printf(format string, a ...interface{}) {
if v.verbose {
fmt.Printf(format, a...)
}
}
var (
vPrinter *verbosePrinter
)
func init() {
vPrinter = newVerbosePrinter()
}
func main() { func main() {
var ( var (
kind string kind string
delay int delay int
verbose bool
interactive bool interactive bool
) )
@ -23,8 +46,8 @@ func main() {
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(&vPrinter.verbose, "verbose", false, "toggle console output")
flag.BoolVar(&verbose, "v", false, "toggle console output (shorthand)") flag.BoolVar(&vPrinter.verbose, "v", false, "toggle console output (shorthand)")
flag.BoolVar(&interactive, "interactive", false, "toggle interactive mode") flag.BoolVar(&interactive, "interactive", false, "toggle interactive mode")
flag.BoolVar(&interactive, "i", false, "toggle interactive mode (shorthand)") flag.BoolVar(&interactive, "i", false, "toggle interactive mode (shorthand)")
flag.Parse() flag.Parse()
@ -35,7 +58,7 @@ func main() {
} }
defer vm.Logout() defer vm.Logout()
err = runCommands(vm, verbose, interactive) err = runCommands(vm, interactive)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -55,12 +78,16 @@ func vmConnect(kind string, delay int) (*voicemeeter.Remote, error) {
return vm, nil return vm, nil
} }
func runCommands(vm *voicemeeter.Remote, verbose, interactive bool) error { func runCommands(vm *voicemeeter.Remote, interactive bool) error {
if interactive { if interactive {
return interactiveMode(vm, verbose) return interactiveMode(vm)
}
if len(flag.Args()) == 0 {
err := fmt.Errorf("must provide some commands to run")
return err
} }
for _, arg := range flag.Args() { for _, arg := range flag.Args() {
err := parse(vm, arg, verbose) err := parse(vm, arg)
if err != nil { if err != nil {
return err return err
} }
@ -68,7 +95,8 @@ func runCommands(vm *voicemeeter.Remote, verbose, interactive bool) error {
return nil return nil
} }
func interactiveMode(vm *voicemeeter.Remote, verbose bool) error { func interactiveMode(vm *voicemeeter.Remote) error {
vPrinter.printf("running in interactive mode... waiting for input\n")
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() { for scanner.Scan() {
input := scanner.Text() input := scanner.Text()
@ -76,7 +104,7 @@ func interactiveMode(vm *voicemeeter.Remote, verbose bool) error {
return nil return nil
} }
for _, cmd := range strings.Split(input, " ") { for _, cmd := range strings.Split(input, " ") {
err := parse(vm, cmd, verbose) err := parse(vm, cmd)
if err != nil { if err != nil {
return err return err
} }
@ -85,20 +113,20 @@ func interactiveMode(vm *voicemeeter.Remote, verbose bool) error {
return nil return nil
} }
func parse(vm *voicemeeter.Remote, cmd string, verbose bool) error { func parse(vm *voicemeeter.Remote, cmd string) error {
if cmd[0] == '!' { if cmd[0] == '!' {
err := toggleCmd(vm, cmd[1:], verbose) err := toggleCmd(vm, cmd[1:])
if err != nil { if err != nil {
return err return err
} }
} else { } else {
if strings.Contains(cmd, "=") { if strings.Contains(cmd, "=") {
err := setCmd(vm, cmd, verbose) err := setCmd(vm, cmd)
if err != nil { if err != nil {
return err return err
} }
} else { } else {
err := getCmd(vm, cmd, verbose) err := getCmd(vm, cmd)
if err != nil { if err != nil {
return err return err
} }
@ -107,23 +135,19 @@ func parse(vm *voicemeeter.Remote, cmd string, verbose bool) error {
return nil return nil
} }
func toggleCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error { func toggleCmd(vm *voicemeeter.Remote, cmd string) error {
val, err := vm.GetFloat(cmd) val, err := vm.GetFloat(cmd)
if err != nil { if err != nil {
err = fmt.Errorf("unable to toggle %s", cmd) err = fmt.Errorf("unable to toggle %s", cmd)
return err return err
} }
vm.SetFloat(cmd, 1-val) vm.SetFloat(cmd, 1-val)
if verbose { vPrinter.printf("Toggling %s\n", cmd)
fmt.Println("Toggling", cmd)
}
return nil return nil
} }
func setCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error { func setCmd(vm *voicemeeter.Remote, cmd string) error {
if verbose { vPrinter.printf("Running command %s\n", cmd)
fmt.Println("Running command", cmd)
}
err := vm.SendText(cmd) err := vm.SendText(cmd)
if err != nil { if err != nil {
err = fmt.Errorf("unable to set %s", cmd) err = fmt.Errorf("unable to set %s", cmd)
@ -132,7 +156,7 @@ func setCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
return nil return nil
} }
func getCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error { func getCmd(vm *voicemeeter.Remote, cmd string) error {
valF, err := vm.GetFloat(cmd) valF, err := vm.GetFloat(cmd)
if err != nil { if err != nil {
valS, err := vm.GetString(cmd) valS, err := vm.GetString(cmd)
@ -140,13 +164,9 @@ func getCmd(vm *voicemeeter.Remote, cmd string, verbose bool) error {
err = fmt.Errorf("unable to get %s", cmd) err = fmt.Errorf("unable to get %s", cmd)
return err return err
} }
if verbose { vPrinter.printf("Value of %s is: %s\n", cmd, valS)
fmt.Println("Value of", cmd, "is:", valS)
}
} else { } else {
if verbose { vPrinter.printf("Value of %s is: %v\n", cmd, valF)
fmt.Println("Value of", cmd, "is:", valF)
}
} }
return nil return nil
} }