|
1 | 1 | package main |
2 | 2 |
|
3 | | -// runTUI is implemented in Task 11. |
4 | | -func runTUI(listenAddr, upstream string, events <-chan Event) { |
5 | | - // Fallback for now: just drain the channel silently. |
6 | | - for range events { |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | +) |
| 11 | + |
| 12 | +const feedSize = 20 |
| 13 | + |
| 14 | +type tickMsg time.Time |
| 15 | +type eventMsg Event |
| 16 | +type doneMsg struct{} |
| 17 | + |
| 18 | +type model struct { |
| 19 | + listenAddr string |
| 20 | + upstream string |
| 21 | + feed []Event |
| 22 | + total int |
| 23 | + errors int |
| 24 | + totalDur time.Duration |
| 25 | + startedAt time.Time |
| 26 | + tab int // 0 = requests, 1 = cert info |
| 27 | +} |
| 28 | + |
| 29 | +func initialModel(listenAddr, upstream string) model { |
| 30 | + return model{ |
| 31 | + listenAddr: listenAddr, |
| 32 | + upstream: upstream, |
| 33 | + startedAt: time.Now(), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (m model) Init() tea.Cmd { return tea.Batch(tick(), tea.EnterAltScreen) } |
| 38 | + |
| 39 | +func tick() tea.Cmd { |
| 40 | + return tea.Tick(100*time.Millisecond, func(t time.Time) tea.Msg { return tickMsg(t) }) |
| 41 | +} |
| 42 | + |
| 43 | +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 44 | + switch msg := msg.(type) { |
| 45 | + case tea.KeyMsg: |
| 46 | + switch msg.String() { |
| 47 | + case "q", "ctrl+c": |
| 48 | + return m, tea.Quit |
| 49 | + case "c": |
| 50 | + m.feed = nil |
| 51 | + case "1": |
| 52 | + m.tab = 0 |
| 53 | + case "2": |
| 54 | + m.tab = 1 |
| 55 | + } |
| 56 | + case tickMsg: |
| 57 | + return m, tick() |
| 58 | + case eventMsg: |
| 59 | + ev := Event(msg) |
| 60 | + m.total++ |
| 61 | + m.totalDur += ev.Duration |
| 62 | + if ev.Status >= 400 { |
| 63 | + m.errors++ |
| 64 | + } |
| 65 | + m.feed = append(m.feed, ev) |
| 66 | + if len(m.feed) > feedSize { |
| 67 | + m.feed = m.feed[len(m.feed)-feedSize:] |
| 68 | + } |
| 69 | + case doneMsg: |
| 70 | + return m, tea.Quit |
| 71 | + } |
| 72 | + return m, nil |
| 73 | +} |
| 74 | + |
| 75 | +// Palette adapted from torlink (github.com/baairon/torlink). |
| 76 | +var ( |
| 77 | + accent = lipgloss.Color("#a78bfa") // violet — active/focus/title |
| 78 | + muted = lipgloss.Color("#6b6577") // purple-grey — dividers, labels, inactive |
| 79 | + ok = lipgloss.Color("#86d6a2") // soft green — 2xx / good counts |
| 80 | + warn = lipgloss.Color("#f0c674") // 3xx |
| 81 | + bad = lipgloss.Color("#e57373") // 4xx/5xx / error counts |
| 82 | + |
| 83 | + titleStyle = lipgloss.NewStyle().Bold(true).Foreground(accent) |
| 84 | + labelStyle = lipgloss.NewStyle().Foreground(muted) |
| 85 | + dimStyle = lipgloss.NewStyle().Foreground(muted).Faint(true) |
| 86 | + sectionHdr = lipgloss.NewStyle().Foreground(accent).Bold(true) |
| 87 | + borderStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(muted).Padding(0, 1) |
| 88 | + statOK = lipgloss.NewStyle().Foreground(ok) |
| 89 | + statBad = lipgloss.NewStyle().Foreground(bad) |
| 90 | + statWarn = lipgloss.NewStyle().Foreground(warn) |
| 91 | + tabActive = lipgloss.NewStyle().Foreground(accent).Bold(true) |
| 92 | + tabInactive = lipgloss.NewStyle().Foreground(muted) |
| 93 | + chev = lipgloss.NewStyle().Foreground(accent).Render("❯") |
| 94 | +) |
| 95 | + |
| 96 | +func (m model) View() string { |
| 97 | + var avg time.Duration |
| 98 | + if m.total > 0 { |
| 99 | + avg = m.totalDur / time.Duration(m.total) |
| 100 | + } |
| 101 | + uptime := time.Since(m.startedAt).Round(time.Second) |
| 102 | + |
| 103 | + header := chev + " " + titleStyle.Render("httpsdev v"+Version) + " " + |
| 104 | + labelStyle.Render(m.listenAddr+" → "+m.upstream) |
| 105 | + |
| 106 | + statsHdr := sectionHdr.Render("▲ live") |
| 107 | + statsBox := borderStyle.Render(fmt.Sprintf( |
| 108 | + "total %d errors %s avg %s uptime %s", |
| 109 | + m.total, |
| 110 | + colorInt(m.errors, m.errors == 0), |
| 111 | + avg.Round(time.Millisecond), |
| 112 | + uptime, |
| 113 | + )) |
| 114 | + |
| 115 | + var mainHdr, mainBox string |
| 116 | + switch m.tab { |
| 117 | + case 0: |
| 118 | + mainHdr = sectionHdr.Render("▲ requests") |
| 119 | + mainBox = borderStyle.Render(m.renderFeed()) |
| 120 | + case 1: |
| 121 | + mainHdr = sectionHdr.Render("▲ cert") |
| 122 | + mainBox = borderStyle.Render(strings.Join([]string{ |
| 123 | + "cert " + labelStyle.Render("mkcert-signed"), |
| 124 | + "subject " + labelStyle.Render("localhost"), |
| 125 | + "valid " + labelStyle.Render("30 days (rolling)"), |
| 126 | + "", |
| 127 | + dimStyle.Render("more introspection in v0.2"), |
| 128 | + }, "\n")) |
| 129 | + } |
| 130 | + |
| 131 | + tabs := []string{"1 requests", "2 cert"} |
| 132 | + if m.tab == 0 { |
| 133 | + tabs[0] = tabActive.Render(tabs[0]) |
| 134 | + tabs[1] = tabInactive.Render(tabs[1]) |
| 135 | + } else { |
| 136 | + tabs[0] = tabInactive.Render(tabs[0]) |
| 137 | + tabs[1] = tabActive.Render(tabs[1]) |
| 138 | + } |
| 139 | + footer := strings.Join(tabs, " ") + labelStyle.Render(" q quit · c clear") |
| 140 | + |
| 141 | + return strings.Join([]string{"", header, "", statsHdr, statsBox, "", mainHdr, mainBox, "", footer}, "\n") |
| 142 | +} |
| 143 | + |
| 144 | +func (m model) renderFeed() string { |
| 145 | + if len(m.feed) == 0 { |
| 146 | + return dimStyle.Render("waiting for requests…") |
| 147 | + } |
| 148 | + var b strings.Builder |
| 149 | + lastIdx := len(m.feed) - 1 |
| 150 | + for i, ev := range m.feed { |
| 151 | + s := statOK |
| 152 | + switch { |
| 153 | + case ev.Status >= 500: |
| 154 | + s = statBad |
| 155 | + case ev.Status >= 400: |
| 156 | + s = statBad |
| 157 | + case ev.Status >= 300: |
| 158 | + s = statWarn |
| 159 | + } |
| 160 | + marker := " " |
| 161 | + if i == lastIdx { |
| 162 | + marker = chev + " " |
| 163 | + } |
| 164 | + fmt.Fprintf(&b, "%s%-6s %-30s %s %s\n", |
| 165 | + marker, |
| 166 | + ev.Method, |
| 167 | + truncate(ev.Path, 30), |
| 168 | + s.Render(fmt.Sprintf("%d", ev.Status)), |
| 169 | + labelStyle.Render(ev.Duration.Round(time.Millisecond).String()), |
| 170 | + ) |
7 | 171 | } |
8 | | - _ = listenAddr |
9 | | - _ = upstream |
| 172 | + return strings.TrimRight(b.String(), "\n") |
| 173 | +} |
| 174 | + |
| 175 | +func colorInt(n int, ok bool) string { |
| 176 | + if ok { |
| 177 | + return statOK.Render(fmt.Sprintf("%d", n)) |
| 178 | + } |
| 179 | + return statBad.Render(fmt.Sprintf("%d", n)) |
| 180 | +} |
| 181 | + |
| 182 | +func truncate(s string, n int) string { |
| 183 | + if len(s) <= n { |
| 184 | + return s |
| 185 | + } |
| 186 | + return s[:n-1] + "…" |
| 187 | +} |
| 188 | + |
| 189 | +// runTUI drives the bubbletea program, feeding it events from the channel. |
| 190 | +func runTUI(listenAddr, upstream string, events <-chan Event) { |
| 191 | + p := tea.NewProgram(initialModel(listenAddr, upstream)) |
| 192 | + go func() { |
| 193 | + for ev := range events { |
| 194 | + p.Send(eventMsg(ev)) |
| 195 | + } |
| 196 | + p.Send(doneMsg{}) |
| 197 | + }() |
| 198 | + _, _ = p.Run() |
10 | 199 | } |
0 commit comments