Add the TUI.
A table with one row per URL, a level-colored log pane and a status bar. New alerts flash their row and ring the bell. Keys pause, force checks, open the product page or cart link, and reload the config, which is validated first and applied in place, keeping each URL's timing. Headless mode and the TUI now share one watcher. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f0eda5d4d
commit
a19e6fa711
14 files changed
+1359
-105
No files matched your search
@@ -0,0 +1,304 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
tea "charm.land/bubbletea/v2"
|
||||
"charm.land/lipgloss/v2"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
|
||||
"git.security-command.org/Overlord/dibs/internal/watch"
|
||||
)
|
||||
|
||||
// The colors are ANSI palette entries, so they follow the terminal's theme.
|
||||
var (
|
||||
bold = lipgloss.NewStyle().Bold(true)
|
||||
faint = lipgloss.NewStyle().Faint(true)
|
||||
green = lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Bold(true)
|
||||
yellow = lipgloss.NewStyle().Foreground(lipgloss.Color("11"))
|
||||
red = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
|
||||
blue = lipgloss.NewStyle().Foreground(lipgloss.Color("12"))
|
||||
selected = lipgloss.NewStyle().Reverse(true)
|
||||
flashing = lipgloss.NewStyle().Background(lipgloss.Color("10")).Foreground(lipgloss.Color("0")).Bold(true)
|
||||
)
|
||||
|
||||
// fixedLines are the lines around the table body and the log: the header,
|
||||
// the column titles, the log title, the status bar and the key help.
|
||||
const fixedLines = 5
|
||||
|
||||
var columns = []struct {
|
||||
title string
|
||||
min, max int
|
||||
} {
|
||||
{"ITEM", 8, 32},
|
||||
{"SHOP", 4, 12},
|
||||
{"TLD", 3, 6},
|
||||
{"STATUS", 9, 9},
|
||||
{"PRICE", 5, 16},
|
||||
{"SELLER", 6, 11},
|
||||
{"LAST", 4, 9},
|
||||
{"NEXT", 4, 10},
|
||||
}
|
||||
|
||||
// layout sizes the table and the log pane to the window.
|
||||
func (m *Model) layout() {
|
||||
avail := max(m.height-fixedLines, 2)
|
||||
body := min(len(m.rows), max(3, avail*55/100), avail-1)
|
||||
body = max(body, 1)
|
||||
|
||||
m.log.SetWidth(m.width)
|
||||
m.log.SetHeight(max(avail-body, 1))
|
||||
|
||||
if m.cursor < m.offset { m.offset = m.cursor }
|
||||
if m.cursor >= m.offset+body { m.offset = m.cursor - body + 1 }
|
||||
m.offset = max(0, min(m.offset, len(m.rows)-body))
|
||||
}
|
||||
|
||||
func (m *Model) bodyHeight() int {
|
||||
return max(m.height-fixedLines, 2) - m.log.Height()
|
||||
}
|
||||
|
||||
func (m *Model) View() tea.View {
|
||||
v := tea.NewView("")
|
||||
v.AltScreen = true
|
||||
v.WindowTitle = "dibs"
|
||||
|
||||
if n := m.count(statusAvailable); n > 0 { v.WindowTitle = fmt.Sprintf("dibs · %d AVAILABLE", n) }
|
||||
|
||||
if m.width == 0 { return v }
|
||||
|
||||
m.layout()
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString(fit(bold.Render("dibs")+" "+faint.Render(m.path), m.width) + "\n")
|
||||
b.WriteString(m.table())
|
||||
b.WriteString(m.logTitle() + "\n")
|
||||
b.WriteString(m.log.View() + "\n")
|
||||
b.WriteString(m.statusBar() + "\n")
|
||||
b.WriteString(fit(faint.Render(m.help()), m.width))
|
||||
|
||||
v.SetContent(b.String())
|
||||
return v
|
||||
}
|
||||
|
||||
func (m *Model) table() string {
|
||||
cells := make([][]string, len(m.rows))
|
||||
widths := make([]int, len(columns))
|
||||
|
||||
for i, c := range columns { widths[i] = max(c.min, len(c.title)) }
|
||||
|
||||
for i, row := range m.rows {
|
||||
cells[i] = m.cells(row)
|
||||
for j, c := range cells[i] { widths[j] = max(widths[j], min(ansi.StringWidth(c), columns[j].max)) }
|
||||
}
|
||||
|
||||
// The ITEM column gives way first when the window is narrow.
|
||||
total := 2
|
||||
for _, w := range widths { total += w + 2 }
|
||||
if over := total - m.width; over > 0 { widths[0] = max(columns[0].min, widths[0]-over) }
|
||||
|
||||
var b strings.Builder
|
||||
|
||||
titles := make([]string, len(columns))
|
||||
for i, c := range columns { titles[i] = c.title }
|
||||
b.WriteString(fit(faint.Render(" "+joinCells(titles, widths)), m.width) + "\n")
|
||||
|
||||
body := m.bodyHeight()
|
||||
for i := m.offset; i < m.offset+body; i++ {
|
||||
if i >= len(m.rows) {
|
||||
b.WriteString("\n")
|
||||
continue
|
||||
}
|
||||
|
||||
b.WriteString(m.rowLine(i, cells[i], widths) + "\n")
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Model) rowLine(i int, cells []string, widths []int) string {
|
||||
r := m.results[m.rows[i].URL]
|
||||
|
||||
switch {
|
||||
case r != nil && m.now.Before(r.flash) && m.now.UnixMilli()/tick.Milliseconds()%2 == 0:
|
||||
return flashing.Render(padRight(" "+joinCells(cells, widths), m.width))
|
||||
case i == m.cursor && !m.logFocus:
|
||||
return selected.Render(padRight("> "+joinCells(cells, widths), m.width))
|
||||
}
|
||||
|
||||
// Only the status cell is colored.
|
||||
status := statusStyle(cells[3]).Render(padRight(cells[3], widths[3]))
|
||||
line := " " + joinCells(cells[:3], widths[:3]) + " " + status + " " + joinCells(cells[4:], widths[4:])
|
||||
|
||||
return fit(line, m.width)
|
||||
}
|
||||
|
||||
func (m *Model) cells(row watch.Status) []string {
|
||||
r := m.results[row.URL]
|
||||
if r == nil { r = &result{status: statusWatching} }
|
||||
|
||||
status := r.status
|
||||
if row.Paused { status = statusPaused }
|
||||
|
||||
return []string{row.Item, row.Shop, tld(row.Host), status, orDash(r.price), orDash(r.seller), m.ago(row.Last), m.until(row)}
|
||||
}
|
||||
|
||||
func statusStyle(status string) lipgloss.Style {
|
||||
switch status {
|
||||
case statusAvailable:
|
||||
return green
|
||||
case statusBlocked:
|
||||
return yellow
|
||||
case statusError:
|
||||
return red
|
||||
case statusPaused:
|
||||
return faint
|
||||
default:
|
||||
return lipgloss.NewStyle()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) logTitle() string {
|
||||
title := "── log "
|
||||
if m.logFocus { return fit(bold.Render(title+"(scrolling) "+strings.Repeat("─", m.width)), m.width) }
|
||||
|
||||
return fit(faint.Render(title+strings.Repeat("─", m.width)), m.width)
|
||||
}
|
||||
|
||||
func (m *Model) logLines() []string {
|
||||
out := make([]string, len(m.logs))
|
||||
|
||||
for i, l := range m.logs {
|
||||
level := fmt.Sprintf("%-5s", l.Level)
|
||||
switch {
|
||||
case l.Level >= slog.LevelError:
|
||||
level = red.Render(level)
|
||||
case l.Level >= slog.LevelWarn:
|
||||
level = yellow.Render(level)
|
||||
case l.Level >= slog.LevelInfo:
|
||||
level = blue.Render(level)
|
||||
default:
|
||||
level = faint.Render(level)
|
||||
}
|
||||
|
||||
msg := l.Msg
|
||||
if msg == "ALERT" { msg = green.Render(msg) }
|
||||
|
||||
out[i] = faint.Render(l.Time.Format(time.TimeOnly)) + " " + level + " " + msg + " " + faint.Render(l.Attrs)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (m *Model) statusBar() string {
|
||||
parts := []string{fmt.Sprintf("%d URLs", len(m.rows))}
|
||||
|
||||
for _, s := range []struct {
|
||||
status string
|
||||
style lipgloss.Style
|
||||
} {
|
||||
{statusAvailable, green},
|
||||
{statusBlocked, yellow},
|
||||
{statusError, red},
|
||||
{statusPaused, faint},
|
||||
} {
|
||||
if n := m.count(s.status); n > 0 { parts = append(parts, s.style.Render(fmt.Sprintf("%d %s", n, s.status))) }
|
||||
}
|
||||
|
||||
left := strings.Join(parts, faint.Render(" · "))
|
||||
|
||||
note := ""
|
||||
if m.note != "" && time.Since(m.noteTime) < noteFor {
|
||||
note = m.note
|
||||
if m.noteError { note = red.Render(note) }
|
||||
}
|
||||
|
||||
gap := m.width - ansi.StringWidth(left) - ansi.StringWidth(note)
|
||||
if gap < 2 { return fit(left+" "+note, m.width) }
|
||||
|
||||
return left + strings.Repeat(" ", gap) + note
|
||||
}
|
||||
|
||||
func (m *Model) help() string {
|
||||
if m.logFocus { return "↑↓ pgup pgdn scroll · l/esc back to the table · r reload · q quit" }
|
||||
|
||||
return "↑↓ select · p pause · P pause all · c check now · o open page · enter cart · l log · r reload · q quit"
|
||||
}
|
||||
|
||||
// count is the number of rows showing status.
|
||||
func (m *Model) count(status string) int {
|
||||
n := 0
|
||||
for _, row := range m.rows {
|
||||
if m.cells(row)[3] == status { n++ }
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
func (m *Model) ago(t time.Time) string {
|
||||
if t.IsZero() { return "—" }
|
||||
|
||||
return short(m.now.Sub(t)) + " ago"
|
||||
}
|
||||
|
||||
func (m *Model) until(row watch.Status) string {
|
||||
switch {
|
||||
case row.Paused:
|
||||
return "—"
|
||||
case row.Checking:
|
||||
return "checking"
|
||||
}
|
||||
|
||||
d := row.Next.Sub(m.now)
|
||||
if d <= 0 { return "due" }
|
||||
|
||||
return "in " + short(d)
|
||||
}
|
||||
|
||||
// short formats d as "45s", "3m05s" or "1h20m".
|
||||
func short(d time.Duration) string {
|
||||
d = max(d, 0).Round(time.Second)
|
||||
|
||||
switch {
|
||||
case d < time.Minute:
|
||||
return fmt.Sprintf("%ds", int(d.Seconds()))
|
||||
case d < time.Hour:
|
||||
return fmt.Sprintf("%dm%02ds", int(d.Minutes()), int(d.Seconds())%60)
|
||||
default:
|
||||
return fmt.Sprintf("%dh%02dm", int(d.Hours()), int(d.Minutes())%60)
|
||||
}
|
||||
}
|
||||
|
||||
func tld(host string) string {
|
||||
suffix, _ := publicsuffix.PublicSuffix(host)
|
||||
return suffix
|
||||
}
|
||||
|
||||
func orDash(s string) string {
|
||||
if s == "" { return "—" }
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func joinCells(cells []string, widths []int) string {
|
||||
var b strings.Builder
|
||||
for i, c := range cells {
|
||||
if i > 0 { b.WriteString(" ") }
|
||||
b.WriteString(padRight(ansi.Truncate(c, widths[i], "…"), widths[i]))
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func padRight(s string, w int) string {
|
||||
return s + strings.Repeat(" ", max(0, w-ansi.StringWidth(s)))
|
||||
}
|
||||
|
||||
// fit cuts s to the window width.
|
||||
func fit(s string, w int) string {
|
||||
return ansi.Truncate(s, w, "")
|
||||
}
|
||||
Reference in new issue
Block a user