1// Package ghdash provides a dialog component for embedding gh-dash in the TUI.
2package ghdash
3
4import (
5 "context"
6 "fmt"
7 "image/color"
8 "os"
9
10 tea "charm.land/bubbletea/v2"
11
12 "git.secluded.site/crush/internal/terminal"
13 "git.secluded.site/crush/internal/tui/components/dialogs"
14 "git.secluded.site/crush/internal/tui/components/dialogs/termdialog"
15 "git.secluded.site/crush/internal/tui/styles"
16)
17
18// DialogID is the unique identifier for the gh-dash dialog.
19const DialogID dialogs.DialogID = "ghdash"
20
21// NewDialog creates a new gh-dash dialog. The context controls the lifetime
22// of the gh-dash process - when cancelled, the process will be killed.
23func NewDialog(ctx context.Context, workingDir string) *termdialog.Dialog {
24 configFile := createThemedConfig()
25
26 cmd := terminal.PrepareCmd(
27 ctx,
28 "gh",
29 []string{"dash", "--config", configFile},
30 workingDir,
31 nil,
32 )
33
34 return termdialog.New(termdialog.Config{
35 ID: DialogID,
36 Title: "GitHub Dashboard",
37 LoadingMsg: "Starting gh-dash...",
38 Term: terminal.New(terminal.Config{Context: ctx, Cmd: cmd}),
39 OnClose: func() tea.Cmd {
40 if configFile != "" {
41 _ = os.Remove(configFile)
42 }
43 return nil
44 },
45 })
46}
47
48// colorToHex converts a color.Color to a hex string.
49func colorToHex(c color.Color) string {
50 r, g, b, _ := c.RGBA()
51 return fmt.Sprintf("#%02x%02x%02x", r>>8, g>>8, b>>8)
52}
53
54// createThemedConfig creates a temporary gh-dash config file with Crush theme.
55func createThemedConfig() string {
56 t := styles.CurrentTheme()
57
58 config := fmt.Sprintf(`theme:
59 colors:
60 text:
61 primary: "%s"
62 secondary: "%s"
63 inverted: "%s"
64 faint: "%s"
65 warning: "%s"
66 success: "%s"
67 error: "%s"
68 background:
69 selected: "%s"
70 border:
71 primary: "%s"
72 secondary: "%s"
73 faint: "%s"
74`,
75 colorToHex(t.FgBase),
76 colorToHex(t.FgMuted),
77 colorToHex(t.FgSelected),
78 colorToHex(t.FgSubtle),
79 colorToHex(t.Warning),
80 colorToHex(t.Success),
81 colorToHex(t.Error),
82 colorToHex(t.Primary),
83 colorToHex(t.BorderFocus),
84 colorToHex(t.FgMuted),
85 colorToHex(t.BgSubtle),
86 )
87
88 f, err := os.CreateTemp("", "crush-ghdash-*.yml")
89 if err != nil {
90 return ""
91 }
92 defer f.Close()
93
94 _, _ = f.WriteString(config)
95 return f.Name()
96}