|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "bleach/internal/tui/dashboard" |
| 8 | + "bleach/internal/tui/menu" |
| 9 | + "bleach/internal/ops" |
| 10 | + "bleach/internal/tui/styles" |
| 11 | + |
| 12 | + tea "github.com/charmbracelet/bubbletea" |
| 13 | + "github.com/charmbracelet/lipgloss" |
| 14 | +) |
| 15 | + |
| 16 | +type appsState int |
| 17 | +const ( |
| 18 | + stateDashboard appsState = iota |
| 19 | + stateRunning |
| 20 | +) |
| 21 | + |
| 22 | +const Version = "v1.0.0" |
| 23 | + |
| 24 | +type model struct { |
| 25 | + width int |
| 26 | + height int |
| 27 | + state appsState |
| 28 | + |
| 29 | + dashboard dashboard.Model |
| 30 | + menu menu.Model |
| 31 | + |
| 32 | + outputLog string |
| 33 | +} |
| 34 | + |
| 35 | +func initialModel() model { |
| 36 | + return model{ |
| 37 | + dashboard: dashboard.NewModel(), |
| 38 | + menu: menu.NewModel(), |
| 39 | + outputLog: fmt.Sprintf("Ready (v%s). Select an action.", Version), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (m model) Init() tea.Cmd { |
| 44 | + return tea.Batch(m.dashboard.Init(), m.menu.Init()) |
| 45 | +} |
| 46 | + |
| 47 | +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 48 | + var cmds []tea.Cmd |
| 49 | + |
| 50 | + switch msg := msg.(type) { |
| 51 | + case tea.KeyMsg: |
| 52 | + if msg.String() == "q" || msg.String() == "ctrl+c" { |
| 53 | + return m, tea.Quit |
| 54 | + } |
| 55 | + // If running, ignore keys or allow cancel? |
| 56 | + if m.state == stateRunning { |
| 57 | + return m, nil |
| 58 | + } |
| 59 | + |
| 60 | + case tea.WindowSizeMsg: |
| 61 | + m.width = msg.Width |
| 62 | + m.height = msg.Height |
| 63 | + |
| 64 | + // Resize Dashboard |
| 65 | + var dCmd tea.Cmd |
| 66 | + m.dashboard, dCmd = m.dashboard.Update(msg) |
| 67 | + cmds = append(cmds, dCmd) |
| 68 | + |
| 69 | + // Resize Menu (Bottom Left) |
| 70 | + // Logic: Menu gets half width? or full width bottom? |
| 71 | + // Design said: Bottom Left = Menu, Bottom Right = Output. |
| 72 | + // Let's pass half width to menu. |
| 73 | + halfWidth := (m.width / 2) - 4 |
| 74 | + m.menu.Width = halfWidth |
| 75 | + m.menu, _ = m.menu.Update(msg) // just to update width |
| 76 | + |
| 77 | + case ops.OpResultMsg: |
| 78 | + m.state = stateDashboard |
| 79 | + if msg.Err != nil { |
| 80 | + m.outputLog = fmt.Sprintf("Error: %v\n%s", msg.Err, msg.Output) |
| 81 | + } else { |
| 82 | + m.outputLog = fmt.Sprintf("Success:\n%s", msg.Output) |
| 83 | + } |
| 84 | + // Clear selection |
| 85 | + m.menu.Selected = nil |
| 86 | + } |
| 87 | + |
| 88 | + // Route messages to components |
| 89 | + // Dashboard always updates (ticks) |
| 90 | + var dCmd tea.Cmd |
| 91 | + m.dashboard, dCmd = m.dashboard.Update(msg) |
| 92 | + cmds = append(cmds, dCmd) |
| 93 | + |
| 94 | + // Menu updates only if not running |
| 95 | + if m.state == stateDashboard { |
| 96 | + newMenu, mCmd := m.menu.Update(msg) |
| 97 | + m.menu = newMenu |
| 98 | + cmds = append(cmds, mCmd) |
| 99 | + |
| 100 | + // Check selection |
| 101 | + if m.menu.Selected != nil { |
| 102 | + // Trigger Action |
| 103 | + switch m.menu.Selected.Title { |
| 104 | + case "Exit": |
| 105 | + return m, tea.Quit |
| 106 | + case "System Cleanup": |
| 107 | + m.state = stateRunning |
| 108 | + m.outputLog = "Running System Cleanup... (Password may be required in terminal if not cached)" |
| 109 | + cmds = append(cmds, ops.RunCleanupCmd()) |
| 110 | + case "System Updates": |
| 111 | + m.state = stateRunning |
| 112 | + m.outputLog = "Running System Updates..." |
| 113 | + cmds = append(cmds, ops.RunUpdateCmd()) |
| 114 | + case "Maintenance": |
| 115 | + m.state = stateRunning |
| 116 | + m.outputLog = "Running Maintenance..." |
| 117 | + cmds = append(cmds, ops.RunMaintenanceCmd()) |
| 118 | + case "View Logs": |
| 119 | + m.outputLog = "Log viewing not implemented yet." |
| 120 | + m.menu.Selected = nil |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return m, tea.Batch(cmds...) |
| 126 | +} |
| 127 | + |
| 128 | +func (m model) View() string { |
| 129 | + if m.width == 0 { |
| 130 | + return "Initializing..." |
| 131 | + } |
| 132 | + |
| 133 | + // 1. Dashboard (Top) |
| 134 | + topView := m.dashboard.View() |
| 135 | + |
| 136 | + // 2. Bottom Area |
| 137 | + // Left: Menu |
| 138 | + menuView := m.menu.View() |
| 139 | + |
| 140 | + // Right: Output Log |
| 141 | + // Calculate width for right box |
| 142 | + rWidth := m.width - m.menu.Width - 6 // margin |
| 143 | + outputView := styles.Panel.Width(rWidth).Height(6).Render( |
| 144 | + lipgloss.JoinVertical(lipgloss.Left, |
| 145 | + styles.Label.Render("OUTPUT"), |
| 146 | + styles.Value.Render(truncate(m.outputLog, 200)), // simple truncate |
| 147 | + ), |
| 148 | + ) |
| 149 | + |
| 150 | + bottomView := lipgloss.JoinHorizontal(lipgloss.Top, menuView, outputView) |
| 151 | + |
| 152 | + return lipgloss.JoinVertical(lipgloss.Left, topView, bottomView) |
| 153 | +} |
| 154 | + |
| 155 | +func truncate(s string, max int) string { |
| 156 | + if len(s) > max { |
| 157 | + return s[:max] + "..." |
| 158 | + } |
| 159 | + return s |
| 160 | +} |
| 161 | + |
| 162 | +func main() { |
| 163 | + // Enable mouse? tea.WithMouseCellMotion() |
| 164 | + p := tea.NewProgram(initialModel(), tea.WithAltScreen()) |
| 165 | + if _, err := p.Run(); err != nil { |
| 166 | + fmt.Printf("Error: %v", err) |
| 167 | + os.Exit(1) |
| 168 | + } |
| 169 | +} |
0 commit comments