60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / tui / netindicator.go
3.8 KB
1// Package tui provides the terminal user interface for Zoea Nova. 2package tui 3 4import ( 5 "time" 6 7 tea "github.com/charmbracelet/bubbletea" 8 "github.com/charmbracelet/lipgloss" 9) 10 11// NetActivity represents the type of network activity. 12type NetActivity int 13 14const ( 15 NetActivityIdle NetActivity = iota 16 NetActivityLLM // Talking to LLM provider 17 NetActivityMCP // Talking to MCP/game server 18) 19 20// NetIndicator is a bouncing progress indicator for network activity. 21type NetIndicator struct { 22 activity NetActivity 23 position int // Current position of the "ball" (0-width) 24 direction int // 1 = right, -1 = left 25 width int // Width of the indicator bar 26 lastTick time.Time // For animation timing 27} 28 29// NetIndicatorTickMsg is sent to animate the indicator. 30type NetIndicatorTickMsg time.Time 31 32// NewNetIndicator creates a new network activity indicator. 33func NewNetIndicator() NetIndicator { 34 return NetIndicator{ 35 activity: NetActivityIdle, 36 position: 0, 37 direction: 1, 38 width: 12, // Width of the bouncing area 39 } 40} 41 42// SetActivity sets the current network activity type. 43func (n *NetIndicator) SetActivity(activity NetActivity) { 44 n.activity = activity 45} 46 47// Activity returns the current activity type. 48func (n NetIndicator) Activity() NetActivity { 49 return n.activity 50} 51 52// Update handles tick messages for animation. 53func (n NetIndicator) Update(msg tea.Msg) (NetIndicator, tea.Cmd) { 54 switch msg.(type) { 55 case NetIndicatorTickMsg: 56 if n.activity != NetActivityIdle { 57 // Move the position 58 n.position += n.direction 59 60 // Bounce at edges 61 if n.position >= n.width-1 { 62 n.position = n.width - 1 63 n.direction = -1 64 } else if n.position <= 0 { 65 n.position = 0 66 n.direction = 1 67 } 68 } 69 return n, n.tick() 70 } 71 return n, nil 72} 73 74// tick returns a command that sends a tick after a delay. 75func (n NetIndicator) tick() tea.Cmd { 76 return tea.Tick(time.Millisecond*80, func(t time.Time) tea.Msg { 77 return NetIndicatorTickMsg(t) 78 }) 79} 80 81// Init starts the indicator animation. 82func (n NetIndicator) Init() tea.Cmd { 83 return n.tick() 84} 85 86// View renders the network indicator. 87func (n NetIndicator) View() string { 88 // Define the bar characters - use block elements for CRT feel 89 const ( 90 barEmpty = "░" 91 barFilled = "█" 92 barLeft = "▐" 93 barRight = "▌" 94 ) 95 96 // Get style based on activity 97 var style lipgloss.Style 98 var label string 99 100 switch n.activity { 101 case NetActivityIdle: 102 style = lipgloss.NewStyle().Foreground(colorMuted) 103 label = "⬦ IDLE" 104 // For idle, show a static dim bar 105 bar := barLeft 106 for i := 0; i < n.width; i++ { 107 bar += barEmpty 108 } 109 bar += barRight 110 return style.Render(label + " " + bar) 111 112 case NetActivityLLM: 113 style = lipgloss.NewStyle().Foreground(colorAssistant).Bold(true) 114 label = " ⬥ LLM " 115 case NetActivityMCP: 116 // MCP activity - diamond with MCP label 117 label = " ⬥ MCP " 118 } 119 120 // Build the bouncing bar 121 bar := barLeft 122 for i := 0; i < n.width; i++ { 123 // Create a 3-char wide "ball" that bounces 124 if i >= n.position-1 && i <= n.position+1 { 125 bar += barFilled 126 } else { 127 bar += barEmpty 128 } 129 } 130 bar += barRight 131 132 return style.Render(label + " " + bar) 133} 134 135// ViewCompact renders a compact version for small screens. 136func (n NetIndicator) ViewCompact() string { 137 // Simpler animation frames for compact view - hexagonal theme 138 frames := []string{"⬡", "⬢", "⬡", "⬢"} 139 140 var style lipgloss.Style 141 var label string 142 143 switch n.activity { 144 case NetActivityIdle: 145 style = lipgloss.NewStyle().Foreground(colorMuted) 146 return style.Render("⬦ IDLE") 147 148 case NetActivityLLM: 149 style = lipgloss.NewStyle().Foreground(colorAssistant).Bold(true) 150 label = "LLM" 151 152 case NetActivityMCP: 153 style = lipgloss.NewStyle().Foreground(colorTeal).Bold(true) 154 label = "MCP" 155 } 156 157 // Use position to select frame 158 frameIdx := n.position % len(frames) 159 return style.Render(frames[frameIdx] + " " + label) 160}