gitstack

grindlemire/go-tui code browser

3.9 KB 162 lines 2026-03-15 · e5befc1 raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main

import (
	"fmt"
	"math"
	"time"
	tui "github.com/grindlemire/go-tui"
)

type streamingApp struct {
	dataCh        <-chan string
	lines         *tui.State[[]string]
	scrollY       *tui.State[int]
	stickToBottom *tui.State[bool]
	elapsed       *tui.State[int]
	content       *tui.Ref
}

func Streaming(dataCh <-chan string) *streamingApp {
	return &streamingApp{
		dataCh:        dataCh,
		lines:         tui.NewState([]string{}),
		scrollY:       tui.NewState(0),
		stickToBottom: tui.NewState(true),
		elapsed:       tui.NewState(0),
		content:       tui.NewRef(),
	}
}

func (s *streamingApp) scrollBy(delta int) {
	el := s.content.El()
	if el == nil {
		return
	}
	_, maxY := el.MaxScroll()
	newY := s.scrollY.Get() + delta
	if newY < 0 {
		newY = 0
	} else if newY > maxY {
		newY = maxY
	}
	s.scrollY.Set(newY)
	s.stickToBottom.Set(newY >= maxY)
}

func (s *streamingApp) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.On(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('j'), func(ke tui.KeyEvent) { s.scrollBy(1) }),
		tui.On(tui.Rune('k'), func(ke tui.KeyEvent) { s.scrollBy(-1) }),
		tui.On(tui.KeyUp, func(ke tui.KeyEvent) { s.scrollBy(-1) }),
		tui.On(tui.KeyDown, func(ke tui.KeyEvent) { s.scrollBy(1) }),
		tui.On(tui.KeyPageUp, func(ke tui.KeyEvent) { s.scrollBy(-10) }),
		tui.On(tui.KeyPageDown, func(ke tui.KeyEvent) { s.scrollBy(10) }),
		tui.On(tui.KeyHome, func(ke tui.KeyEvent) {
			s.scrollY.Set(0)
			s.stickToBottom.Set(false)
		}),
		tui.On(tui.KeyEnd, func(ke tui.KeyEvent) {
			s.scrollY.Set(math.MaxInt)
			s.stickToBottom.Set(true)
		}),
		tui.On(tui.Rune(' '), func(ke tui.KeyEvent) {
			if s.stickToBottom.Get() {
				s.stickToBottom.Set(false)
			} else {
				s.scrollY.Set(math.MaxInt)
				s.stickToBottom.Set(true)
			}
		}),
	}
}

func (s *streamingApp) HandleMouse(me tui.MouseEvent) bool {
	switch me.Button {
	case tui.MouseWheelUp:
		s.scrollBy(-1)
		return true
	case tui.MouseWheelDown:
		s.scrollBy(1)
		return true
	}
	return false
}

func (s *streamingApp) Watchers() []tui.Watcher {
	return []tui.Watcher{
		tui.OnTimer(time.Second, s.tick),
		tui.Watch(s.dataCh, s.addLine),
	}
}

func (s *streamingApp) tick() {
	s.elapsed.Set(s.elapsed.Get() + 1)
}

func (s *streamingApp) addLine(line string) {
	current := s.lines.Get()
	s.lines.Set(append(current, line))
	if s.stickToBottom.Get() {
		s.scrollY.Set(math.MaxInt)
	}
}

func lineColor(line string) string {
	if len(line) < 20 {
		return ""
	}
	// Color based on metric type
	for i := 0; i < len(line)-3; i++ {
		sub := line[i : i+3]
		if sub == "cpu" {
			return "text-cyan"
		}
		if sub == "mem" {
			return "text-magenta"
		}
		if sub == "net" {
			return "text-green"
		}
		if sub == "dis" {
			return "text-yellow"
		}
		if sub == "io:" {
			return "text-blue"
		}
	}
	return ""
}

templ (s *streamingApp) Render() {
	<div class="flex-col gap-1 p-1 h-full border-rounded border-cyan">
		<div class="flex justify-between shrink-0">
			<span class="text-gradient-cyan-magenta font-bold shrink-0">Live Stream</span>
			<span class="text-cyan font-bold" minWidth={0}>{fmt.Sprintf("%d lines", len(s.lines.Get()))}</span>
		</div>
		<div
			ref={s.content}
			class="flex-col flex-grow border-single p-1"
			scrollable={tui.ScrollVertical}
			scrollOffset={0, s.scrollY.Get()}
		>
			for _, line := range s.lines.Get() {
				<span class={lineColor(line)}>{line}</span>
			}
		</div>

		<div class="flex gap-2 shrink-0 justify-center">
			<span class="font-dim">Elapsed:</span>
			<span class="text-cyan font-bold">{fmt.Sprintf("%ds", s.elapsed.Get())}</span>
			<span class="font-dim">Auto-scroll:</span>
			if s.stickToBottom.Get() {
				<span class="text-green font-bold">ON</span>
			} else {
				<span class="text-yellow">OFF</span>
			}
		</div>

		<span class="font-dim shrink-0">j/k scroll | Space toggle auto-scroll | q quit</span>
	</div>
}