gitstack

grindlemire/go-tui code browser

4.1 KB 168 lines 2026-03-18 · 598ab5c 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
163
164
165
166
167
168
package main

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

type feedApp struct {
	messages      *tui.State[[]string]
	paused        *tui.State[bool]
	scrollY       *tui.State[int]
	stickToBottom *tui.State[bool]
	content       *tui.Ref
	mode          string
}

func NewFeedApp(mode string) *feedApp {
	return &feedApp{
		messages:      tui.NewState([]string{}),
		paused:        tui.NewState(false),
		scrollY:       tui.NewState(0),
		stickToBottom: tui.NewState(false),
		content:       tui.NewRef(),
		mode:          mode,
	}
}

func (f *feedApp) scrollBy(delta int) {
	el := f.content.El()
	if el == nil {
		return
	}
	// Read actual position from element, not from state (state may be math.MaxInt).
	_, curY := el.ScrollOffset()
	_, maxY := el.MaxScroll()
	newY := curY + delta
	if newY < 0 {
		newY = 0
	} else if newY > maxY {
		newY = maxY
	}
	f.scrollY.Set(newY)
	f.stickToBottom.Set(false)
}

func (f *feedApp) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.OnStop(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.OnStop(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.OnStop(tui.Rune('p'), func(ke tui.KeyEvent) {
			f.paused.Set(!f.paused.Get())
		}),
		tui.OnStop(tui.Rune('s'), func(ke tui.KeyEvent) {
			if f.stickToBottom.Get() {
				// Turning off: capture actual scroll position so scrollY
				// is no longer math.MaxInt (which would keep following the bottom).
				if el := f.content.El(); el != nil {
					_, y := el.ScrollOffset()
					f.scrollY.Set(y)
				}
				f.stickToBottom.Set(false)
			} else {
				f.stickToBottom.Set(true)
			}
		}),
		tui.On(tui.Rune('j'), func(ke tui.KeyEvent) { f.scrollBy(1) }),
		tui.On(tui.Rune('k'), func(ke tui.KeyEvent) { f.scrollBy(-1) }),
		tui.On(tui.KeyUp, func(ke tui.KeyEvent) { f.scrollBy(-1) }),
		tui.On(tui.KeyDown, func(ke tui.KeyEvent) { f.scrollBy(1) }),
		tui.On(tui.KeyPageUp, func(ke tui.KeyEvent) { f.scrollBy(-10) }),
		tui.On(tui.KeyPageDown, func(ke tui.KeyEvent) { f.scrollBy(10) }),
		tui.On(tui.KeyHome, func(ke tui.KeyEvent) {
			f.scrollY.Set(0)
			f.stickToBottom.Set(false)
		}),
	}
}

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

func (f *feedApp) AddMessage(msg string) {
	f.messages.Update(func(msgs []string) []string {
		return append(msgs, msg)
	})
	if f.stickToBottom.Get() {
		f.scrollY.Set(math.MaxInt)
	}
}

func (f *feedApp) IsPaused() bool {
	return f.paused.Get()
}

func pauseLabel(paused bool) string {
	if paused {
		return "PAUSED"
	}
	return "LIVE"
}

func pauseClass(paused bool) string {
	if paused {
		return "text-yellow font-bold"
	}
	return "text-green font-bold"
}

func stickyLabel(sticky bool) string {
	if sticky {
		return "STICKY"
	}
	return "FREE"
}

func stickyClass(sticky bool) string {
	if sticky {
		return "text-cyan font-bold"
	}
	return "text-yellow"
}

templ (f *feedApp) Render() {
	<div class="flex-col h-full border-rounded border-cyan">
		<div class="flex justify-between px-1 shrink-0">
			<span class="text-gradient-cyan-magenta font-bold">Event Loop Demo</span>
			<div class="flex gap-1">
				<span class="font-dim">mode:</span>
				<span class="text-cyan font-bold">{f.mode}</span>
			</div>
		</div>
		<hr />
		<div
			ref={f.content}
			class="flex-col flex-grow border-single p-1"
			scrollable={tui.ScrollVertical}
			scrollOffset={0, f.scrollY.Get()}
		>
			for _, msg := range f.messages.Get() {
				<span class="font-dim">{msg}</span>
			}
		</div>
		<hr />
		<div class="flex justify-between px-1 shrink-0">
			<div class="flex gap-2">
				<span class="font-dim">p: pause</span>
				<span class="font-dim">s: sticky</span>
				<span class="font-dim">j/k: scroll</span>
				<span class="font-dim">q: quit</span>
			</div>
			<div class="flex gap-2">
				<span class={pauseClass(f.paused.Get())}>{pauseLabel(f.paused.Get())}</span>
				<span class={stickyClass(f.stickToBottom.Get())}>{stickyLabel(f.stickToBottom.Get())}</span>
				<span class="font-dim">{fmt.Sprintf("%d msgs", len(f.messages.Get()))}</span>
			</div>
		</div>
	</div>
}