gitstack

grindlemire/go-tui code browser

7.5 KB 255 lines 2026-03-15 · 0f507e8 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main

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

type colorMixer struct {
	red   *tui.State[int]
	green *tui.State[int]
	blue  *tui.State[int]

	redUpBtn   *tui.Ref
	redDnBtn   *tui.Ref
	greenUpBtn *tui.Ref
	greenDnBtn *tui.Ref
	blueUpBtn  *tui.Ref
	blueDnBtn  *tui.Ref
	presetBtns   *tui.RefMap[string]
	activePreset *tui.State[string]

	resetBtn       *tui.Ref
	showResetModal *tui.State[bool]
}

func ColorMixer() *colorMixer {
	return &colorMixer{
		red:        tui.NewState(128),
		green:      tui.NewState(64),
		blue:       tui.NewState(200),
		redUpBtn:   tui.NewRef(),
		redDnBtn:   tui.NewRef(),
		greenUpBtn: tui.NewRef(),
		greenDnBtn: tui.NewRef(),
		blueUpBtn:  tui.NewRef(),
		blueDnBtn:  tui.NewRef(),
		presetBtns:   tui.NewRefMap[string](),
		activePreset: tui.NewState(""),

		resetBtn:       tui.NewRef(),
		showResetModal: tui.NewState(false),
	}
}

type preset struct {
	name string
	r, g, b int
}

var presets = []preset{
	{"Sunset", 255, 128, 0},
	{"Ocean", 0, 100, 255},
	{"Forest", 34, 180, 34},
	{"Rose", 255, 64, 128},
}

func clamp(v, min, max int) int {
	if v < min {
		return min
	}
	if v > max {
		return max
	}
	return v
}

func (c *colorMixer) adjustRed(delta int) {
	c.red.Set(clamp(c.red.Get()+delta, 0, 255))
	c.activePreset.Set("")
}

func (c *colorMixer) adjustGreen(delta int) {
	c.green.Set(clamp(c.green.Get()+delta, 0, 255))
	c.activePreset.Set("")
}

func (c *colorMixer) adjustBlue(delta int) {
	c.blue.Set(clamp(c.blue.Get()+delta, 0, 255))
	c.activePreset.Set("")
}

func (c *colorMixer) resetColors() {
	c.red.Set(128)
	c.green.Set(64)
	c.blue.Set(200)
	c.activePreset.Set("")
	c.showResetModal.Set(false)
}

func (c *colorMixer) cancelReset() {
	c.showResetModal.Set(false)
}

func (c *colorMixer) applyPreset(name string) {
	for _, p := range presets {
		if p.name == name {
			c.red.Set(p.r)
			c.green.Set(p.g)
			c.blue.Set(p.b)
			c.activePreset.Set(name)
			return
		}
	}
}

func (c *colorMixer) KeyMap() tui.KeyMap {
	return tui.KeyMap{
		tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),
		tui.On(tui.Rune('r'), func(ke tui.KeyEvent) { c.showResetModal.Set(true) }),
		tui.On(tui.Rune('R'), func(ke tui.KeyEvent) { c.adjustRed(-16) }),
		tui.On(tui.Rune('g'), func(ke tui.KeyEvent) { c.adjustGreen(16) }),
		tui.On(tui.Rune('G'), func(ke tui.KeyEvent) { c.adjustGreen(-16) }),
		tui.On(tui.Rune('b'), func(ke tui.KeyEvent) { c.adjustBlue(16) }),
		tui.On(tui.Rune('B'), func(ke tui.KeyEvent) { c.adjustBlue(-16) }),
	}
}

func (c *colorMixer) HandleMouse(me tui.MouseEvent) bool {
	// Check single-ref button clicks
	if tui.HandleClicks(me,
		tui.Click(c.redUpBtn, func() { c.adjustRed(16) }),
		tui.Click(c.redDnBtn, func() { c.adjustRed(-16) }),
		tui.Click(c.greenUpBtn, func() { c.adjustGreen(16) }),
		tui.Click(c.greenDnBtn, func() { c.adjustGreen(-16) }),
		tui.Click(c.blueUpBtn, func() { c.adjustBlue(16) }),
		tui.Click(c.blueDnBtn, func() { c.adjustBlue(-16) }),
		tui.Click(c.resetBtn, func() { c.showResetModal.Set(true) }),
	) {
		return true
	}

	// Check keyed-ref preset button clicks via RefMap
	if me.Button == tui.MouseLeft && me.Action == tui.MousePress {
		for name, el := range c.presetBtns.All() {
			if el != nil && el.ContainsPoint(me.X, me.Y) {
				c.applyPreset(name)
				return true
			}
		}
	}

	return false
}

func colorBar(value int) string {
	filled := value * 20 / 255
	bar := ""
	for i := 0; i < 20; i++ {
		if i < filled {
			bar += "█"
		} else {
			bar += "░"
		}
	}
	return bar
}

templ (c *colorMixer) Render() {
	<div class="flex-col p-1 border-rounded border-cyan">
		<span class="text-gradient-cyan-magenta font-bold">Color Mixer</span>

		// Color preview
		<div class="flex-col items-center border-rounded p-1">
			<span class="text-gradient-cyan-magenta font-bold">Preview</span>
			<div backgroundGradient={tui.NewGradient(tui.Black, tui.RGBColor(uint8(c.red.Get()), uint8(c.green.Get()), uint8(c.blue.Get())))} height={2} width={30}>
				<span>{" "}</span>
			</div>
			<div class="flex gap-2 justify-center">
				<span class="text-red font-bold">{fmt.Sprintf("R: %d", c.red.Get())}</span>
				<span class="text-green font-bold">{fmt.Sprintf("G: %d", c.green.Get())}</span>
				<span class="text-blue font-bold">{fmt.Sprintf("B: %d", c.blue.Get())}</span>
			</div>
		</div>

		// Color bars
		<div class="flex-col border-rounded p-1">
			<div class="flex gap-1">
				<span class="text-red font-bold w-5">Red</span>
				<span class="text-red">{colorBar(c.red.Get())}</span>
				<span class="text-red font-bold">{fmt.Sprintf("%3d", c.red.Get())}</span>
			</div>
			<div class="flex gap-1">
				<span class="text-green font-bold w-5">Grn</span>
				<span class="text-green">{colorBar(c.green.Get())}</span>
				<span class="text-green font-bold">{fmt.Sprintf("%3d", c.green.Get())}</span>
			</div>
			<div class="flex gap-1">
				<span class="text-blue font-bold w-5">Blu</span>
				<span class="text-blue">{colorBar(c.blue.Get())}</span>
				<span class="text-blue font-bold">{fmt.Sprintf("%3d", c.blue.Get())}</span>
			</div>
		</div>

		// Channel controls with refs
		<div class="flex gap-1">
			<div class="flex-col border-rounded p-1 items-center" flexGrow={1.0}>
				<span class="font-bold text-red">Red</span>
				<div class="flex gap-1 items-center">
					<button ref={c.redDnBtn} class="px-1">{"-"}</button>
					<span class="font-bold text-red">{fmt.Sprintf("%3d", c.red.Get())}</span>
					<button ref={c.redUpBtn} class="px-1">{"+"}</button>
				</div>
			</div>
			<div class="flex-col border-rounded p-1 items-center" flexGrow={1.0}>
				<span class="font-bold text-green">Green</span>
				<div class="flex gap-1 items-center">
					<button ref={c.greenDnBtn} class="px-1">{"-"}</button>
					<span class="font-bold text-green">{fmt.Sprintf("%3d", c.green.Get())}</span>
					<button ref={c.greenUpBtn} class="px-1">{"+"}</button>
				</div>
			</div>
			<div class="flex-col border-rounded p-1 items-center" flexGrow={1.0}>
				<span class="font-bold text-blue">Blue</span>
				<div class="flex gap-1 items-center">
					<button ref={c.blueDnBtn} class="px-1">{"-"}</button>
					<span class="font-bold text-blue">{fmt.Sprintf("%3d", c.blue.Get())}</span>
					<button ref={c.blueUpBtn} class="px-1">{"+"}</button>
				</div>
			</div>
		</div>

		// Preset colors using RefMap with key
		<div class="flex gap-1 border-rounded p-1 items-center">
			<span class="font-bold">Presets:</span>
			for _, p := range presets {
				if p.name == c.activePreset.Get() {
					<button ref={c.presetBtns} key={p.name} class="px-1 font-bold text-cyan">{p.name}</button>
				} else {
					<button ref={c.presetBtns} key={p.name} class="px-1 font-dim">{p.name}</button>
				}
			}
		</div>

		<div class="flex gap-2 justify-center">
			<button ref={c.resetBtn} class="px-1 text-red">Reset</button>
		</div>

		<div class="flex justify-center">
			<span class="font-dim">r reset | g/b increase | G/B decrease | click buttons/presets | q quit</span>
		</div>

		// Confirmation modal for resetting colors
		<modal open={c.showResetModal} class="justify-center items-center">
			<div class="border-rounded p-2 flex-col gap-1 w-36 items-center">
				<span class="font-bold text-yellow">Reset Colors?</span>
				<span class="font-dim">This will restore default values.</span>
				<div class="flex gap-2 justify-center">
					<button class="px-2 text-green font-bold border-single focusable" onActivate={c.cancelReset}>Cancel</button>
					<button class="px-2 text-red font-bold border-single focusable" onActivate={c.resetColors}>Yes, Reset</button>
				</div>
			</div>
		</modal>
	</div>
}