Skip to content

Commit afc5172

Browse files
committed
added d2
1 parent 8542456 commit afc5172

File tree

9 files changed

+282
-0
lines changed

9 files changed

+282
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var fmtCmd = &cobra.Command{
9+
Use: "fmt file.d2 ...",
10+
Short: "Format all passed files",
11+
Run: func(cmd *cobra.Command, args []string) {},
12+
}
13+
14+
func init() {
15+
carapace.Gen(fmtCmd).Standalone()
16+
rootCmd.AddCommand(fmtCmd)
17+
18+
carapace.Gen(fmtCmd).PositionalAnyCompletion(
19+
carapace.ActionFiles(".d2"),
20+
)
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/d2"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var layoutCmd = &cobra.Command{
10+
Use: "layout [name]",
11+
Short: "Lists available layout engine options with short help",
12+
Run: func(cmd *cobra.Command, args []string) {},
13+
}
14+
15+
func init() {
16+
carapace.Gen(layoutCmd).Standalone()
17+
18+
rootCmd.AddCommand(layoutCmd)
19+
20+
carapace.Gen(layoutCmd).PositionalCompletion(
21+
d2.ActionLayouts(),
22+
)
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var playCmd = &cobra.Command{
9+
Use: "play",
10+
Short: "Opens the file in playground, an online web viewer",
11+
Run: func(cmd *cobra.Command, args []string) {},
12+
}
13+
14+
func init() {
15+
carapace.Gen(playCmd).Standalone()
16+
17+
rootCmd.AddCommand(playCmd)
18+
19+
carapace.Gen(playCmd).PositionalCompletion(
20+
carapace.ActionFiles(".d2"),
21+
)
22+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/net"
6+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/d2"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var rootCmd = &cobra.Command{
11+
Use: "d2",
12+
Short: "compiles and renders d2 diagrams into svgs",
13+
Long: "https://d2lang.com/",
14+
Run: func(cmd *cobra.Command, args []string) {},
15+
}
16+
17+
func Execute() error {
18+
return rootCmd.Execute()
19+
}
20+
func init() {
21+
carapace.Gen(rootCmd).Standalone()
22+
23+
rootCmd.Flags().String("animate-interval", "", "multiple boards are packaged as 1 SVG")
24+
rootCmd.Flags().String("ascii-mode", "", "ASCII rendering mode for text outputs")
25+
rootCmd.Flags().String("browser", "", "browser executable that watch opens")
26+
rootCmd.Flags().BoolP("bundle", "b", false, "bundle all assets and layers into the output file")
27+
rootCmd.Flags().BoolP("center", "c", false, "center the SVG in the containing viewbox")
28+
rootCmd.Flags().Bool("check", false, "check that the specified files are formatted correctly")
29+
rootCmd.Flags().String("dark-theme", "", "the theme to use when the viewer's browser is in dark mode")
30+
rootCmd.Flags().BoolP("debug", "d", false, "print debug logs")
31+
rootCmd.Flags().String("font-bold", "", "path to .ttf file to use for the bold font")
32+
rootCmd.Flags().String("font-italic", "", "path to .ttf file to use for the italic font")
33+
rootCmd.Flags().String("font-mono", "", "path to .ttf file to use for the monospace font")
34+
rootCmd.Flags().String("font-mono-bold", "", "path to .ttf file to use for the monospace bold font")
35+
rootCmd.Flags().String("font-mono-italic", "", "path to .ttf file to use for the monospace italic font")
36+
rootCmd.Flags().String("font-mono-semibold", "", "path to .ttf file to use for the monospace semibold font")
37+
rootCmd.Flags().String("font-regular", "", "path to .ttf file to use for the regular font")
38+
rootCmd.Flags().String("font-semibold", "", "path to .ttf file to use for the semibold font")
39+
rootCmd.Flags().Bool("force-appendix", false, "add an appendix to SVG exports")
40+
rootCmd.Flags().StringP("host", "h", "", "host listening address when used with watch")
41+
rootCmd.Flags().Bool("img-cache", false, "images used in icons are cached for subsequent compilations")
42+
rootCmd.Flags().StringP("layout", "l", "", "the layout engine used")
43+
rootCmd.Flags().Bool("no-xml-tag", false, "omit XML tag (<?xml ...?>) from output SVG files")
44+
rootCmd.Flags().Bool("omit-version", false, "omit D2 version from generated image")
45+
rootCmd.Flags().String("pad", "", "pixels padded around the rendered diagram")
46+
rootCmd.Flags().StringP("port", "p", "", "port listening address when used with watch")
47+
rootCmd.Flags().String("salt", "", "Add a salt value to ensure the output uses unique IDs")
48+
rootCmd.Flags().String("scale", "", "scale the output")
49+
rootCmd.Flags().BoolP("sketch", "s", false, "render the diagram to look like it was sketched by hand")
50+
rootCmd.Flags().String("stdout-format", "", "output format when writing to stdout")
51+
rootCmd.Flags().String("target", "", "target board to render")
52+
rootCmd.Flags().StringP("theme", "t", "", "the diagram theme ID")
53+
rootCmd.Flags().String("timeout", "", "the maximum number of seconds that D2 runs")
54+
rootCmd.Flags().BoolP("version", "v", false, "get the version")
55+
rootCmd.Flags().BoolP("watch", "w", false, "watch for changes to input and live reload")
56+
57+
carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
58+
"ascii-mode": carapace.ActionValuesDescribed(
59+
"standard", "basic ASCII chars",
60+
"extended", "Unicode chars",
61+
),
62+
"browser": carapace.Batch(
63+
carapace.ActionExecutables(),
64+
carapace.ActionFiles(),
65+
).ToA(),
66+
"dark-theme": d2.ActionThemes(d2.ThemeOpts{Dark: true}),
67+
"font-bold": carapace.ActionFiles(".ttf"),
68+
"font-italic": carapace.ActionFiles(".ttf"),
69+
"font-mono": carapace.ActionFiles(".ttf"),
70+
"font-mono-bold": carapace.ActionFiles(".ttf"),
71+
"font-mono-italic": carapace.ActionFiles(".ttf"),
72+
"font-mono-semibold": carapace.ActionFiles(".ttf"),
73+
"font-regular": carapace.ActionFiles(".ttf"),
74+
"font-semibold": carapace.ActionFiles(".ttf"),
75+
"layout": d2.ActionLayouts(),
76+
"pad": carapace.ActionValues(),
77+
"port": net.ActionPorts(),
78+
"stdout-format": carapace.ActionValues("svg", "png", "ascii", "txt", "pdf", "pptx", "gif"),
79+
"target": carapace.ActionValues(), // TODO
80+
"theme": d2.ActionThemes(d2.ThemeOpts{}.Default()),
81+
})
82+
83+
carapace.Gen(rootCmd).PositionalCompletion(
84+
carapace.ActionFiles(".d2"),
85+
)
86+
87+
carapace.Gen(rootCmd).PositionalAnyCompletion(
88+
carapace.ActionFiles(),
89+
)
90+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var themesCmd = &cobra.Command{
9+
Use: "themes",
10+
Short: "Lists available themes",
11+
Run: func(cmd *cobra.Command, args []string) {},
12+
}
13+
14+
func init() {
15+
carapace.Gen(themesCmd).Standalone()
16+
17+
rootCmd.AddCommand(themesCmd)
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var validateCmd = &cobra.Command{
9+
Use: "validate",
10+
Short: "Validates file.d2",
11+
Run: func(cmd *cobra.Command, args []string) {},
12+
}
13+
14+
func init() {
15+
carapace.Gen(validateCmd).Standalone()
16+
17+
rootCmd.AddCommand(validateCmd)
18+
19+
carapace.Gen(validateCmd).PositionalCompletion(
20+
carapace.ActionFiles(".d2"),
21+
)
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/carapace-sh/carapace-bin/completers/common/d2_completer/cmd"
4+
5+
func main() {
6+
cmd.Execute()
7+
}

pkg/actions/tools/d2/layout.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package d2
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"github.com/carapace-sh/carapace"
8+
)
9+
10+
// ActionLayouts completes layouts
11+
//
12+
// dagre (The directed graph layout library Dagre)
13+
// elk (Eclipse Layout Kernel (ELK) with the Layered algorithm.)
14+
func ActionLayouts() carapace.Action {
15+
return carapace.ActionExecCommand("d2", "layout")(func(output []byte) carapace.Action {
16+
lines := strings.Split(string(output), "\n")
17+
r := regexp.MustCompile(`^(?P<name>[^ ]+) (\(bundled\) )?- (?P<description>.*)$`)
18+
19+
vals := make([]string, 0)
20+
for _, line := range lines {
21+
if line == "Usage:" {
22+
break
23+
}
24+
if matches := r.FindStringSubmatch(line); matches != nil {
25+
vals = append(vals, matches[1], matches[3])
26+
}
27+
}
28+
return carapace.ActionValuesDescribed(vals...)
29+
}).Tag("layouts")
30+
}

pkg/actions/tools/d2/theme.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package d2
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
7+
"github.com/carapace-sh/carapace"
8+
"github.com/carapace-sh/carapace/pkg/style"
9+
)
10+
11+
type ThemeOpts struct {
12+
Dark bool
13+
Light bool
14+
}
15+
16+
func (o ThemeOpts) Default() ThemeOpts {
17+
o.Dark = true
18+
o.Light = true
19+
return o
20+
}
21+
22+
// ActionThemes completes themes
23+
//
24+
// 0 (Neutral Default)
25+
// 1 (Neutral Grey)
26+
func ActionThemes(opts ThemeOpts) carapace.Action {
27+
return carapace.ActionExecCommand("d2", "themes")(func(output []byte) carapace.Action {
28+
lines := strings.Split(string(output), "\n")
29+
r := regexp.MustCompile(`^- (?P<name>[^:]+): (?P<description>.*)$`)
30+
31+
dark := false
32+
vals := make([]string, 0)
33+
for _, line := range lines {
34+
if line == "Dark:" {
35+
dark = true
36+
continue
37+
}
38+
if matches := r.FindStringSubmatch(line); matches != nil {
39+
switch {
40+
case dark && opts.Dark:
41+
vals = append(vals, matches[2], matches[1], style.Blue)
42+
case !dark && opts.Light:
43+
vals = append(vals, matches[2], matches[1], style.Default)
44+
}
45+
}
46+
}
47+
return carapace.ActionStyledValuesDescribed(vals...)
48+
}).Tag("themes")
49+
}

0 commit comments

Comments
 (0)