|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/charmbracelet/bubbles/textarea" |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | + "github.com/charmbracelet/lipgloss" |
| 11 | + "github.com/fiffeek/hyprdynamicmonitors/internal/config" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +type HDMGeneratedConfigPreview struct { |
| 16 | + cfg *config.Config |
| 17 | + textarea textarea.Model |
| 18 | + height int |
| 19 | + width int |
| 20 | + runningUnderTest bool |
| 21 | + reloaded bool |
| 22 | + destinationExists bool |
| 23 | +} |
| 24 | + |
| 25 | +func NewHDMGeneratedConfigPreview(cfg *config.Config, |
| 26 | + runningUnderTest bool, |
| 27 | +) *HDMGeneratedConfigPreview { |
| 28 | + ta := textarea.New() |
| 29 | + return &HDMGeneratedConfigPreview{ |
| 30 | + cfg: cfg, |
| 31 | + textarea: ta, |
| 32 | + runningUnderTest: runningUnderTest, |
| 33 | + reloaded: false, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (h *HDMGeneratedConfigPreview) SetHeight(height int) { |
| 38 | + h.height = height |
| 39 | + h.textarea.SetHeight(height) |
| 40 | +} |
| 41 | + |
| 42 | +func (h *HDMGeneratedConfigPreview) SetWidth(width int) { |
| 43 | + h.width = width |
| 44 | + h.textarea.SetWidth(width) |
| 45 | +} |
| 46 | + |
| 47 | +func (h *HDMGeneratedConfigPreview) Clear() { |
| 48 | + h.reloaded = false |
| 49 | + h.destinationExists = false |
| 50 | +} |
| 51 | + |
| 52 | +func (h *HDMGeneratedConfigPreview) Update(msg tea.Msg) tea.Cmd { |
| 53 | + cmds := []tea.Cmd{} |
| 54 | + |
| 55 | + switch msg := msg.(type) { |
| 56 | + case ConfigReloaded: |
| 57 | + logrus.Debug("Received config reloaded event") |
| 58 | + h.Clear() |
| 59 | + |
| 60 | + case OperationStatus: |
| 61 | + if msg.name == OperationNameHydrate || msg.name == OperationNameMatchingProfile { |
| 62 | + h.Clear() |
| 63 | + } |
| 64 | + // poll for the destination changes since the TUI does not watch for these |
| 65 | + case reloadHyprConfigMsg: |
| 66 | + logrus.Debug("Reload hypr config requested") |
| 67 | + h.Clear() |
| 68 | + } |
| 69 | + |
| 70 | + if !h.reloaded { |
| 71 | + h.reloaded = true |
| 72 | + |
| 73 | + contents, err := os.ReadFile(*h.cfg.Get().General.Destination) |
| 74 | + text := "" |
| 75 | + if err == nil { |
| 76 | + h.destinationExists = true |
| 77 | + text = string(contents) |
| 78 | + } |
| 79 | + |
| 80 | + if err == nil && text != h.textarea.Value() { |
| 81 | + cmds = append(cmds, OperationStatusCmd(OperationNameReloadHyprDestination, nil)) |
| 82 | + } |
| 83 | + |
| 84 | + logrus.Debugf("Textarea text: %s", text) |
| 85 | + h.textarea.SetValue(text) |
| 86 | + |
| 87 | + logrus.Debug("Requesting reloading hypr config") |
| 88 | + cmds = append(cmds, reloadHyprConfig(5*time.Second)) |
| 89 | + } |
| 90 | + |
| 91 | + return tea.Batch(cmds...) |
| 92 | +} |
| 93 | + |
| 94 | +func (h *HDMGeneratedConfigPreview) View() string { |
| 95 | + sections := []string{} |
| 96 | + availableHeight := h.height |
| 97 | + |
| 98 | + title := TitleStyle.Margin(0, 0, 0, 0).Render("Generated hypr config preview") |
| 99 | + availableHeight -= lipgloss.Height(title) |
| 100 | + sections = append(sections, title) |
| 101 | + |
| 102 | + configFile := *h.cfg.Get().General.Destination |
| 103 | + if h.runningUnderTest { |
| 104 | + configFile = filepath.Base(configFile) |
| 105 | + } |
| 106 | + |
| 107 | + subtitle := SubtitleInfoStyle.Margin(0, 0, 1, 0).Render(configFile) |
| 108 | + availableHeight -= lipgloss.Height(subtitle) |
| 109 | + sections = append(sections, subtitle) |
| 110 | + |
| 111 | + if h.destinationExists { |
| 112 | + h.textarea.SetWidth(h.width) |
| 113 | + h.textarea.SetHeight(availableHeight) |
| 114 | + ta := h.textarea.View() |
| 115 | + sections = append(sections, ta) |
| 116 | + } else { |
| 117 | + warnSections := []string{} |
| 118 | + warnSections = append(warnSections, "Are you running the daemon?") |
| 119 | + warnSections = append(warnSections, lipgloss.NewStyle().Width(h.width).Margin(0, 0, |
| 120 | + 1, 0).Render("See: "+LinkStyle.Render("https://hyprdynamicmonitors.filipmikina.com/docs/quickstart/setup-approaches"))) |
| 121 | + warnSections = append(warnSections, SubtitleInfoStyle.Width(h.width).Render( |
| 122 | + "Alternatively, run the generation once: `hyprdynamicmonitors run --run-once`")) |
| 123 | + sections = append(sections, lipgloss.JoinVertical(lipgloss.Top, warnSections...)) |
| 124 | + } |
| 125 | + |
| 126 | + return lipgloss.JoinVertical(lipgloss.Top, sections...) |
| 127 | +} |
0 commit comments