-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcmd.go
71 lines (58 loc) · 1.46 KB
/
cmd.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
package help
import (
_ "embed"
"encoding/json"
"fmt"
"github.com/charmbracelet/glamour"
"github.com/rwxrob/bonzai"
"github.com/rwxrob/bonzai/mark"
"gopkg.in/yaml.v3"
)
//go:embed style.yaml
var Style []byte
var Cmd = &bonzai.Cmd{
Name: `help`,
Alias: `h|-h|--help|--h|/?`,
Vers: `v0.8.0`,
Short: `display command help`,
Long: `
The {{code .Name}} command displays the help information for the
immediate previous command unless it is passed arguments, in which
case it resolves the arguments as if they were passed to the
previous command and the help for the leaf command is displayed
instead.`,
Do: func(x *bonzai.Cmd, args ...string) (err error) {
if len(args) > 0 {
x, args, err = x.Caller().SeekInit(args...)
} else {
x = x.Caller()
}
md, err := mark.Bonzai(x)
if err != nil {
return err
}
// load embedded yaml file and convert to json
styleMap := map[string]any{}
if err := yaml.Unmarshal(Style, &styleMap); err != nil {
return err
}
jsonBytes, err := json.Marshal(styleMap)
if err != nil {
return err
}
renderer, err := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
glamour.WithPreservedNewLines(),
glamour.WithStylesFromJSONBytes(jsonBytes),
)
if err != nil {
return fmt.Errorf("developer-error: %v", err)
}
rendered, err := renderer.Render(md)
if err != nil {
return fmt.Errorf("developer-error: %v", err)
}
fmt.Println("\u001b[2J\u001b[H" + rendered)
return nil
},
}