Files
cburn/internal/config/config.go
teernisse 93e343f657 feat: add TUI auto-refresh with configurable interval and manual refresh
Introduce background data refresh so the dashboard stays current without
restarting. This touches four layers:

Config (config.go):
- New TUIConfig struct with AutoRefresh (bool) and RefreshIntervalSec
  (int) fields, defaulting to enabled at 30-second intervals.
- Minimum interval floor of 10 seconds enforced at load time.

App core (app.go):
- RefreshDataMsg type for background refresh completion signaling.
- Auto-refresh state: interval timer, refreshing flag, lastRefresh
  timestamp. Checked on every tick; fires refreshDataCmd when elapsed.
- refreshDataCmd: background goroutine that loads session data via cache
  (with uncached fallback) and posts RefreshDataMsg on completion.
- Manual refresh keybind: 'r' triggers immediate refresh.
- Auto-refresh toggle keybind: 'R' toggles auto-refresh and persists
  the preference to config.toml.
- Help text updated with r/R keybind documentation.

Status bar (statusbar.go):
- Shows spinning refresh indicator during active refresh.
- Shows auto-refresh icon when auto-refresh is enabled.

Settings tab (tab_settings.go):
- Two new editable fields: Auto Refresh (bool) and Refresh Interval
  (seconds with 10s minimum).
- Settings display reads live App state to stay consistent with the
  R toggle keybind (avoids stale config-file reads).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 09:36:50 -05:00

162 lines
4.3 KiB
Go

// Package config handles cburn configuration loading, saving, and pricing.
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
// Config holds all cburn configuration.
type Config struct {
General GeneralConfig `toml:"general"`
AdminAPI AdminAPIConfig `toml:"admin_api"`
ClaudeAI ClaudeAIConfig `toml:"claude_ai"`
Budget BudgetConfig `toml:"budget"`
Appearance AppearanceConfig `toml:"appearance"`
TUI TUIConfig `toml:"tui"`
Pricing PricingOverrides `toml:"pricing"`
}
// GeneralConfig holds general preferences.
type GeneralConfig struct {
DefaultDays int `toml:"default_days"`
IncludeSubagents bool `toml:"include_subagents"`
ClaudeDir string `toml:"claude_dir,omitempty"`
}
// AdminAPIConfig holds Anthropic Admin API settings.
type AdminAPIConfig struct {
APIKey string `toml:"api_key,omitempty"` //nolint:gosec // config field, not a secret
BaseURL string `toml:"base_url,omitempty"`
}
// ClaudeAIConfig holds claude.ai session key settings for subscription data.
type ClaudeAIConfig struct {
SessionKey string `toml:"session_key,omitempty"` //nolint:gosec // config field, not a secret
OrgID string `toml:"org_id,omitempty"` // auto-cached after first fetch
}
// BudgetConfig holds budget tracking settings.
type BudgetConfig struct {
MonthlyUSD *float64 `toml:"monthly_usd,omitempty"`
}
// AppearanceConfig holds theme settings.
type AppearanceConfig struct {
Theme string `toml:"theme"`
}
// TUIConfig holds TUI-specific settings.
type TUIConfig struct {
AutoRefresh bool `toml:"auto_refresh"`
RefreshIntervalSec int `toml:"refresh_interval_sec"`
}
// PricingOverrides allows user-defined pricing for specific models.
type PricingOverrides struct {
Overrides map[string]ModelPricingOverride `toml:"overrides,omitempty"`
}
// ModelPricingOverride holds per-model pricing overrides.
type ModelPricingOverride struct {
InputPerMTok *float64 `toml:"input_per_mtok,omitempty"`
OutputPerMTok *float64 `toml:"output_per_mtok,omitempty"`
CacheWrite5mPerMTok *float64 `toml:"cache_write_5m_per_mtok,omitempty"`
CacheWrite1hPerMTok *float64 `toml:"cache_write_1h_per_mtok,omitempty"`
CacheReadPerMTok *float64 `toml:"cache_read_per_mtok,omitempty"`
}
// DefaultConfig returns the default configuration.
func DefaultConfig() Config {
return Config{
General: GeneralConfig{
DefaultDays: 30,
IncludeSubagents: true,
},
Appearance: AppearanceConfig{
Theme: "flexoki-dark",
},
TUI: TUIConfig{
AutoRefresh: true,
RefreshIntervalSec: 30,
},
}
}
// Dir returns the XDG-compliant config directory.
func Dir() string {
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "cburn")
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "cburn")
}
// Path returns the full path to the config file.
func Path() string {
return filepath.Join(Dir(), "config.toml")
}
// Load reads the config file, returning defaults if it doesn't exist.
func Load() (Config, error) {
cfg := DefaultConfig()
data, err := os.ReadFile(Path())
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
}
return cfg, fmt.Errorf("reading config: %w", err)
}
if err := toml.Unmarshal(data, &cfg); err != nil {
return cfg, fmt.Errorf("parsing config: %w", err)
}
return cfg, nil
}
// Save writes the config to disk.
func Save(cfg Config) error {
dir := Dir()
if err := os.MkdirAll(dir, 0o750); err != nil {
return fmt.Errorf("creating config dir: %w", err)
}
f, err := os.OpenFile(Path(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("creating config file: %w", err)
}
enc := toml.NewEncoder(f)
if err := enc.Encode(cfg); err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// GetAdminAPIKey returns the API key from env var or config, in that order.
func GetAdminAPIKey(cfg Config) string {
if key := os.Getenv("ANTHROPIC_ADMIN_KEY"); key != "" {
return key
}
return cfg.AdminAPI.APIKey
}
// GetSessionKey returns the session key from env var or config, in that order.
func GetSessionKey(cfg Config) string {
if key := os.Getenv("CLAUDE_SESSION_KEY"); key != "" {
return key
}
return cfg.ClaudeAI.SessionKey
}
// Exists returns true if a config file exists on disk.
func Exists() bool {
_, err := os.Stat(Path())
return err == nil
}