ExecuteWithStream resolves the data source, writes the HTML and opens it.
(_ context.Context, args []string, _ func(string))
| 198 | |
| 199 | // ExecuteWithStream resolves the data source, writes the HTML and opens it. |
| 200 | func (p *BuiltinGraphViewPlugin) ExecuteWithStream(_ context.Context, args []string, _ func(string)) (string, error) { |
| 201 | cfg, err := parseGraphViewArgs(args) |
| 202 | if err != nil { |
| 203 | return "", fmt.Errorf("@graphview: %w", err) |
| 204 | } |
| 205 | |
| 206 | data, err := resolveGraphData(cfg) |
| 207 | if err != nil { |
| 208 | return "", fmt.Errorf("@graphview: %w", err) |
| 209 | } |
| 210 | if len(data.Nodes) == 0 { |
| 211 | return i18n.T("plugins.graphview.empty"), nil |
| 212 | } |
| 213 | if len(data.Nodes) > graphViewMaxNodes { |
| 214 | return "", fmt.Errorf("@graphview: %s", i18n.T("plugins.graphview.too_big", len(data.Nodes), graphViewMaxNodes)) |
| 215 | } |
| 216 | |
| 217 | path, err := renderGraphViewHTML(data, cfg.Output) |
| 218 | if err != nil { |
| 219 | return "", fmt.Errorf("@graphview: %w", err) |
| 220 | } |
| 221 | |
| 222 | opened := false |
| 223 | if cfg.shouldOpen() { |
| 224 | if oerr := openInBrowser(path); oerr == nil { |
| 225 | opened = true |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | msg := i18n.T("plugins.graphview.rendered", path, len(data.Nodes), len(data.Edges)) |
| 230 | if opened { |
| 231 | msg += "\n" + i18n.T("plugins.graphview.opened") |
| 232 | } else { |
| 233 | msg += "\n" + i18n.T("plugins.graphview.open_hint", path) |
| 234 | } |
| 235 | return msg, nil |
| 236 | } |
| 237 | |
| 238 | // graphViewArgs is the typed view of @graphview's input. |
| 239 | type graphViewArgs struct { |
no test coverage detected