-
Notifications
You must be signed in to change notification settings - Fork 2
/
gquery.go
77 lines (66 loc) · 1.58 KB
/
gquery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"flag"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/scgolang/sc"
)
// GQuery queries the SuperCollider node graph.
type GQuery struct {
GroupID int
flagErrorHandling flag.ErrorHandling
scc *sc.Client
}
// Run runs the command.
func (gq *GQuery) Run(args []string) error {
fs := flag.NewFlagSet("gquery", gq.flagErrorHandling)
fs.IntVar(&gq.GroupID, "group", 0, "Group ID")
if err := fs.Parse(args); err != nil {
return ErrUsage
}
g, err := gq.scc.QueryGroup(int32(gq.GroupID))
if err != nil {
return errors.Wrap(err, "querying group")
}
printGroup(g)
return nil
}
// Usage prints a usage message.
func (gq *GQuery) Usage() {
fmt.Fprint(os.Stderr, `
scc [GLOBAL OPTIONS] gquery [OPTIONS]
OPTIONS
-group (OPTIONAL) Group ID. Default is 0 (root group).
`)
}
func printGroup(g *sc.GroupNode) error {
err := printGroupP(g, "")
return err
}
func printGroupP(g *sc.GroupNode, prefix string) error {
fmt.Printf("group(id=%d)\n", g.ID())
for i, child := range g.Children {
if i == len(g.Children)-1 {
fmt.Printf(prefix + "\u2514\u2500\u2500 ")
} else {
fmt.Printf(prefix + "\u251c\u2500\u2500 ")
}
switch c := child.(type) {
case *sc.SynthNode:
printSynthP(c, "")
case *sc.GroupNode:
printGroupP(c, prefix+" ")
default:
return errors.Errorf("unrecognized node type: %T", c)
}
}
return nil
}
func printSynthP(s *sc.SynthNode, prefix string) {
fmt.Printf(prefix+"%s(id=%d", s.DefName, s.ID())
for name, val := range s.Controls {
fmt.Printf(", %s=%s", name, val)
}
fmt.Printf(")\n")
}