internal/tui/view/statusbar/statusbar.go

// Package statusbar renders the always-on-bottom status line that shows
// the capture source, the visible request count, and a transient hint
// area for commands like "exported to /tmp/out.har".
//
// mercemay.top/src/httptap/
package statusbar

import (
	"fmt"

	"mercemay.top/httptap/internal/tui/theme"
)

// Model is the status bar state.
type Model struct {
	pal    theme.Palette
	width  int
	count  int
	source string
	hint   string
}

// New creates an empty status bar.
func New(pal theme.Palette) Model {
	return Model{pal: pal, source: "unix://httptap.sock"}
}

// SetWidth is called by App.relayout.
func (m *Model) SetWidth(w int) { m.width = w }

// SetCount updates the request count shown on the right.
func (m *Model) SetCount(n int) { m.count = n }

// SetSource updates the left-side indicator (e.g. "stdin", or a path).
func (m *Model) SetSource(s string) { m.source = s }

// Hint sets the transient message shown after an action succeeds. It is
// cleared on the next SetCount call so the UI returns to steady state.
func (m *Model) Hint(s string) { m.hint = s }

// View renders the bar.
func (m Model) View() string {
	left := fmt.Sprintf(" %s ", m.source)
	right := fmt.Sprintf(" %d messages ", m.count)
	middle := ""
	if m.hint != "" {
		middle = "  " + m.hint
	}
	pad := m.width - len(left) - len(right) - len(middle)
	if pad < 1 {
		pad = 1
	}
	return m.pal.StatusBar.Render(left + middle + spaces(pad) + right)
}

func spaces(n int) string {
	out := make([]byte, n)
	for i := range out {
		out[i] = ' '
	}
	return string(out)
}