diff --git a/cmd/dibs/tui.go b/cmd/dibs/tui.go index 926d16f..cfdf2b7 100644 --- a/cmd/dibs/tui.go +++ b/cmd/dibs/tui.go @@ -5,10 +5,13 @@ import ( "errors" "io" "log/slog" + "os" "os/signal" + "strings" "syscall" tea "charm.land/bubbletea/v2" + "github.com/charmbracelet/colorprofile" "git.security-command.org/Overlord/dibs/internal/config" "git.security-command.org/Overlord/dibs/internal/tui" @@ -26,7 +29,7 @@ func runTUI(cfg *config.Config, stderr io.Writer) int { w, err := newWatcher(ctx, cfg, logger, slog.LevelDebug) if err != nil { return fail(stderr, "watch", err) } - p := tea.NewProgram(tui.New(w, cfg.Path, logs), tea.WithContext(ctx)) + p := tea.NewProgram(tui.New(w, cfg.Path, logs), append(terminalOptions(), tea.WithContext(ctx))...) done := make(chan struct{}) go func() { @@ -46,3 +49,19 @@ func runTUI(cfg *config.Config, stderr io.Writer) int { return 0 } + +// terminalOptions works around terminals that claim to be xterm but are not. +// Under a TERM the renderer does not know, it redraws with plain writes only. +// The color profile is detected from the real environment first, so colors stay. +func terminalOptions() []tea.ProgramOption { + if os.Getenv("TERMINAL_EMULATOR") != "JetBrains-JediTerm" { return nil } + + profile := colorprofile.Detect(os.Stdout, os.Environ()) + + env := []string{"TERM=jediterm"} + for _, kv := range os.Environ() { + if !strings.HasPrefix(kv, "TERM=") { env = append(env, kv) } + } + + return []tea.ProgramOption{tea.WithEnvironment(env), tea.WithColorProfile(profile)} +} diff --git a/go.mod b/go.mod index 3646925..8a50fd7 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/andybalholm/cascadia v1.3.4 github.com/bogdanfinn/fhttp v0.6.9 github.com/bogdanfinn/tls-client v1.16.0 + github.com/charmbracelet/colorprofile v0.4.3 github.com/charmbracelet/x/ansi v0.11.9 github.com/gen2brain/beeep v0.11.2 github.com/pelletier/go-toml/v2 v2.4.3 @@ -27,7 +28,6 @@ require ( github.com/bogdanfinn/quic-go-utls v1.0.10-utls // indirect github.com/bogdanfinn/utls v1.7.8-barnius // indirect github.com/bogdanfinn/websocket v1.5.6-barnius // indirect - github.com/charmbracelet/colorprofile v0.4.3 // indirect github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4 // indirect github.com/charmbracelet/x/term v0.2.2 // indirect github.com/charmbracelet/x/termios v0.1.1 // indirect diff --git a/internal/ratelimit/ratelimit.go b/internal/ratelimit/ratelimit.go index 798500f..5fc0c80 100644 --- a/internal/ratelimit/ratelimit.go +++ b/internal/ratelimit/ratelimit.go @@ -215,6 +215,14 @@ func (l *Limiter) URLReady(pageURL string) time.Time { return l.urlReady(pageURL) } +// LastCheck is when the URL was last checked by any dibs process, or zero. +func (l *Limiter) LastCheck(pageURL string) time.Time { + l.mu.Lock() + defer l.mu.Unlock() + + return l.lastCheck(pageURL) +} + // Jittered stretches or shrinks d randomly by up to ±20%, but never below // floor. func (l *Limiter) Jittered(d, floor time.Duration) time.Duration { diff --git a/internal/tui/model.go b/internal/tui/model.go index f0fdc8a..2793d79 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -87,7 +87,8 @@ type Model struct { rows []watch.Status results map[string]*result // by configured URL cursor int - offset int // the first table row on screen + offset int // the first table row on screen + widths []int // the column widths, reset by resizes and reloads logs []LogLine log viewport.Model @@ -138,6 +139,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: m.width, m.height = msg.Width, msg.Height + m.widths = nil m.layout() case tickMsg: m.now = time.Time(msg) @@ -152,6 +154,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case reloadMsg: m.reloading = false m.rows = m.ctl.Statuses() + m.widths = nil m.clampCursor() if msg.err != nil { m.setNote(reloadError(msg.err), true) } else { m.setNote(reloadNote(msg.summary), false) } case openMsg: diff --git a/internal/tui/view.go b/internal/tui/view.go index b59b352..a0f291a 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -40,8 +40,8 @@ var columns = []struct { {"STATUS", 9, 9}, {"PRICE", 5, 16}, {"SELLER", 6, 11}, - {"LAST", 4, 9}, - {"NEXT", 4, 10}, + {"LAST", 10, 10}, + {"NEXT", 9, 9}, } // layout sizes the table and the log pane to the window. @@ -96,6 +96,13 @@ func (m *Model) table() string { for j, c := range cells[i] { widths[j] = max(widths[j], min(ansi.StringWidth(c), columns[j].max)) } } + // Columns only grow, so they do not shift every time a price or a time + // gets shorter. + if len(m.widths) == len(widths) { + for i := range widths { widths[i] = max(widths[i], m.widths[i]) } + } + m.widths = append(m.widths[:0], widths...) + // The ITEM column gives way first when the window is narrow. total := 2 for _, w := range widths { total += w + 2 } diff --git a/internal/watch/watch.go b/internal/watch/watch.go index e4dbd04..948c027 100644 --- a/internal/watch/watch.go +++ b/internal/watch/watch.go @@ -174,6 +174,7 @@ func (s *Scheduler) plan(ctx context.Context, cfg *config.Config, host *plugin.H fetchURL: fetchURL, host: strings.ToLower(u.Hostname()), plugin: p, + last: s.limiter.LastCheck(fetchURL), next: s.limiter.NextCheck(fetchURL, item.Interval.Duration), }) }