summaryrefslogtreecommitdiff
path: root/internal/display/display.go
blob: 94b4c76c249e8b302695a905d70957a89ee7b554 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
package display

import (
	"context"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"codeberg.org/snonux/loadbars/internal/collector"
	"codeberg.org/snonux/loadbars/internal/config"
	"codeberg.org/snonux/loadbars/internal/constants"
	"codeberg.org/snonux/loadbars/internal/stats"
	"codeberg.org/snonux/loadbars/internal/version"
	"github.com/veandco/go-sdl2/sdl"
)

// smoothFactor controls how quickly bars blend toward their target values each frame.
// Lower values produce smoother animations.
const smoothFactor = 0.12

// linkScales lists the supported network link speeds in ascending order,
// used by the f/v hotkeys to cycle through link scale values.
var linkScales = []string{"mbit", "10mbit", "100mbit", "gbit", "10gbit"}

// runState holds mutable state across the display loop (hotkeys, window size, smoothed data).
type runState struct {
	showAvgLine    bool
	showIOAvgLine  bool
	cpuMode        int // constants.CPUModeAverage / CPUModeCores / CPUModeOff
	showMem        bool
	showNet        bool
	showLoad       bool
	loadPeak       float64 // global max load1 across all hosts (for bar scaling)
	showSeparators bool
	extended       bool
	winW           int32
	winH           int32
	prevCPU        map[string]collector.CPULine
	smoothedCPU    map[string]*[10]float64
	smoothedMem    map[string]*struct{ ramUsed, swapUsed float64 }
	smoothedNet    map[string]*struct{ rxPct, txPct float64 }
	prevNet        map[string]stats.NetStamp // aggregated (summed) previous net stamp per host
	peakHistory    map[string][]float64
	diskMode       int       // constants.DiskModeAggregate / DiskModeDevices / DiskModeOff
	diskPeak       float64   // auto-scale peak (bytes/sec) for disk bars
	prevDisk       map[string]stats.DiskStamp  // previous disk stamp per host+device key
	smoothedDisk   map[string]*struct{ readPct, writePct float64 }
	mouseX         int32     // last known mouse X position (for tooltip hit testing)
	mouseY         int32     // last known mouse Y position (for tooltip hit testing)
	mouseLastMove  time.Time // timestamp of last mouse movement; tooltip hidden after 3s idle
}

// newRunState builds initial run state from config.
// When cfg.LoadMax > 0 the load bar uses a fixed scale; otherwise it
// starts at the auto-scale floor of 2.0 and tracks the live maximum.
func newRunState(cfg *config.Config, winW, winH int32) *runState {
	initLoadPeak := 2.0
	if cfg.LoadMax > 0 {
		initLoadPeak = cfg.LoadMax
	}
	initDiskPeak := 1048576.0 // 1 MB/s floor for auto-scale
	if cfg.DiskMax > 0 {
		initDiskPeak = cfg.DiskMax
	}
	return &runState{
		showAvgLine:    cfg.ShowAvgLine,
		showIOAvgLine:  cfg.ShowIOAvgLine,
		cpuMode:        cfg.CPUMode,
		showMem:        cfg.ShowMem,
		showNet:        cfg.ShowNet,
		showLoad:       cfg.ShowLoad,
		loadPeak:       initLoadPeak,
		showSeparators: cfg.ShowSeparators,
		extended:       cfg.Extended,
		winW:           winW,
		winH:           winH,
		prevCPU:        make(map[string]collector.CPULine),
		smoothedCPU:    make(map[string]*[10]float64),
		smoothedMem:    make(map[string]*struct{ ramUsed, swapUsed float64 }),
		smoothedNet:    make(map[string]*struct{ rxPct, txPct float64 }),
		prevNet:        make(map[string]stats.NetStamp),
		peakHistory:    make(map[string][]float64),
		diskMode:       cfg.DiskMode,
		diskPeak:       initDiskPeak,
		prevDisk:       make(map[string]stats.DiskStamp),
		smoothedDisk:   make(map[string]*struct{ readPct, writePct float64 }),
		mouseX:         -1, // off-screen until first mouse move
		mouseY:         -1,
	}
}

// Run runs the SDL display loop until ctx is cancelled or user presses 'q'.
func Run(ctx context.Context, cfg *config.Config, src stats.Source) error {
	if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
		return fmt.Errorf("sdl init: %w", err)
	}
	defer sdl.Quit()

	const minWindowWidth = 800
	width := clampInt(cfg.BarWidth, minWindowWidth, cfg.MaxWidth)
	height := cfg.Height
	if height < 1 {
		height = 1
	}
	title := cfg.Title
	if title == "" {
		title = "Loadbars " + version.Version + " (press h for help on stdout)"
	}
	window, renderer, err := sdl.CreateWindowAndRenderer(int32(width), int32(height), sdl.WINDOW_RESIZABLE)
	if err != nil {
		return fmt.Errorf("create window: %w", err)
	}
	defer window.Destroy()
	defer renderer.Destroy()
	window.SetTitle(title)

	// On macOS, bring the window to the foreground
	activateWindow()

	state := newRunState(cfg, int32(width), int32(height))
	ticker := time.NewTicker(time.Duration(constants.IntervalSDL * float64(time.Second)))
	defer ticker.Stop()

	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		default:
		}
		if handleEvents(window, cfg, state) {
			return nil
		}
		drawFrame(renderer, src, cfg, state)
		renderer.Present()
		sdl.Delay(10)
		<-ticker.C
	}
}

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

// handleEvents processes all pending SDL events and updates state. Returns true if the user quit.
func handleEvents(window *sdl.Window, cfg *config.Config, state *runState) bool {
	for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
		switch ev := e.(type) {
		case *sdl.QuitEvent:
			return true
		case *sdl.KeyboardEvent:
			if ev.Type != sdl.KEYDOWN || ev.Repeat != 0 {
				continue
			}
			if handleKey(ev.Keysym.Sym, window, cfg, state) {
				return true
			}
		case *sdl.MouseMotionEvent:
			state.mouseX, state.mouseY = ev.X, ev.Y
			state.mouseLastMove = time.Now()
		case *sdl.WindowEvent:
			if ev.Event == sdl.WINDOWEVENT_RESIZED {
				state.winW, state.winH = ev.Data1, ev.Data2
			}
		}
	}
	return false
}

// handleKey handles one key press; returns true to quit.
// handleKey handles one key press; returns true to quit.
// It delegates to focused helpers for toggle, adjust/save, and resize keys.
func handleKey(sym sdl.Keycode, window *sdl.Window, cfg *config.Config, state *runState) bool {
	if sym == sdl.K_q {
		return true
	}
	handleToggleKeys(sym, cfg, state)
	handleAdjustAndSave(sym, cfg, state)
	handleResizeKeys(sym, window, cfg, state)
	return false
}

// handleToggleKeys processes display-toggle hotkeys (1, 2/m, 3/n, 4/l, r, e, g, i, s).
func handleToggleKeys(sym sdl.Keycode, cfg *config.Config, state *runState) {
	switch sym {
	case sdl.K_1:
		// Cycle through three CPU display modes: average → all cores → off → average
		state.cpuMode = (state.cpuMode + 1) % constants.CPUModeCount
		switch state.cpuMode {
		case constants.CPUModeAverage:
			fmt.Println("==> CPU: average bar only")
		case constants.CPUModeCores:
			fmt.Println("==> CPU: individual cores")
		case constants.CPUModeOff:
			fmt.Println("==> CPU: off")
		}
	case sdl.K_2, sdl.K_m:
		state.showMem = !state.showMem
		fmt.Println("==> Toggled show mem:", state.showMem)
	case sdl.K_3, sdl.K_n:
		state.showNet = !state.showNet
		fmt.Println("==> Toggled show net:", state.showNet)
	case sdl.K_4, sdl.K_l:
		state.showLoad = !state.showLoad
		fmt.Println("==> Toggled show load:", state.showLoad)
	case sdl.K_5:
		// Cycle through three disk display modes: aggregate → devices → off → aggregate
		state.diskMode = (state.diskMode + 1) % constants.DiskModeCount
		switch state.diskMode {
		case constants.DiskModeAggregate:
			fmt.Println("==> Disk: aggregate (all devices)")
		case constants.DiskModeDevices:
			fmt.Println("==> Disk: per-device")
		case constants.DiskModeOff:
			fmt.Println("==> Disk: off")
		}
	case sdl.K_r:
		// Reset load auto-scale peak to the floor so the bar rescales immediately.
		// Has no effect when loadmax is fixed (cfg.LoadMax > 0).
		if cfg.LoadMax == 0 {
			state.loadPeak = 2.0
			fmt.Println("==> Load peak reset to auto-scale floor (2.0)")
		} else {
			fmt.Println("==> Load peak reset ignored (fixed loadmax =", cfg.LoadMax, ")")
		}
	case sdl.K_e:
		state.extended = !state.extended
		fmt.Println("==> Toggled extended (peak line):", state.extended)
	case sdl.K_g:
		state.showAvgLine = !state.showAvgLine
		fmt.Println("==> Toggled global avg line:", state.showAvgLine)
	case sdl.K_i:
		state.showIOAvgLine = !state.showIOAvgLine
		fmt.Println("==> Toggled global I/O avg line:", state.showIOAvgLine)
	case sdl.K_s:
		state.showSeparators = !state.showSeparators
		fmt.Println("==> Toggled host separators:", state.showSeparators)
	}
}

// handleAdjustAndSave processes sampling-adjust and config-write hotkeys (a, y, d, c, f, v, h, w).
func handleAdjustAndSave(sym sdl.Keycode, cfg *config.Config, state *runState) {
	switch sym {
	case sdl.K_a:
		cfg.CPUAverage++
		fmt.Println("==> CPU average samples:", cfg.CPUAverage)
	case sdl.K_y:
		if cfg.CPUAverage > 1 {
			cfg.CPUAverage--
		}
		fmt.Println("==> CPU average samples:", cfg.CPUAverage)
	case sdl.K_d:
		cfg.NetAverage++
		fmt.Println("==> Net average samples:", cfg.NetAverage)
	case sdl.K_c:
		if cfg.NetAverage > 1 {
			cfg.NetAverage--
		}
		fmt.Println("==> Net average samples:", cfg.NetAverage)
	case sdl.K_b:
		cfg.DiskAverage++
		fmt.Println("==> Disk average samples:", cfg.DiskAverage)
	case sdl.K_x:
		if cfg.DiskAverage > 1 {
			cfg.DiskAverage--
		}
		fmt.Println("==> Disk average samples:", cfg.DiskAverage)
	case sdl.K_f:
		scaleLinkUp(cfg)
	case sdl.K_v:
		scaleLinkDown(cfg)
	case sdl.K_h:
		printHotkeys()
	case sdl.K_w:
		// Copy mutable display state back to config before persisting.
		cfg.ShowAvgLine = state.showAvgLine
		cfg.ShowIOAvgLine = state.showIOAvgLine
		cfg.CPUMode = state.cpuMode
		cfg.ShowMem = state.showMem
		cfg.ShowNet = state.showNet
		cfg.ShowLoad = state.showLoad
		cfg.ShowSeparators = state.showSeparators
		cfg.DiskMode = state.diskMode
		cfg.Extended = state.extended
		if err := cfg.Write(); err != nil {
			fmt.Fprintf(os.Stderr, "!!! Write config: %v\n", err)
		} else {
			fmt.Println("==> Config written to ~/.loadbarsrc")
		}
	}
}

// handleResizeKeys processes window-resize hotkeys (arrow keys).
// window may be nil in tests; the guard prevents a nil-pointer panic.
func handleResizeKeys(sym sdl.Keycode, window *sdl.Window, cfg *config.Config, state *runState) {
	if window == nil {
		return
	}
	switch sym {
	case sdl.K_LEFT:
		state.winW -= 100
		if state.winW < 1 {
			state.winW = 1
		}
		window.SetSize(state.winW, state.winH)
	case sdl.K_RIGHT:
		state.winW += 100
		if state.winW > int32(cfg.MaxWidth) {
			state.winW = int32(cfg.MaxWidth)
		}
		window.SetSize(state.winW, state.winH)
	case sdl.K_UP:
		state.winH -= 100
		if state.winH < 1 {
			state.winH = 1
		}
		window.SetSize(state.winW, state.winH)
	case sdl.K_DOWN:
		state.winH += 100
		window.SetSize(state.winW, state.winH)
	}
}

// barBounds calculates the x position and width for a bar at the given index.
// This distributes remainder pixels evenly, ensuring all bars fill the window width.
func barBounds(winW int32, numBars int, barIndex int) (x int32, width int32) {
	if numBars <= 0 {
		return 0, winW
	}
	// Calculate start and end positions using scaled division to distribute remainder pixels
	startX := (winW * int32(barIndex)) / int32(numBars)
	endX := (winW * int32(barIndex+1)) / int32(numBars)
	return startX, endX - startX
}

// barRect computes the x, y, width, and height for a bar in a multi-row layout.
// When maxPerRow <= 0 or maxPerRow >= numBars, all bars fit in a single row (full height).
// Otherwise, bars wrap into multiple rows of equal height. The last row may have
// fewer bars, which become wider to fill the full window width.
func barRect(winW, winH int32, numBars, maxPerRow, barIndex int) (x, y, w, h int32) {
	if maxPerRow <= 0 || maxPerRow >= numBars {
		// Single row: full window height
		bx, bw := barBounds(winW, numBars, barIndex)
		return bx, 0, bw, winH
	}
	numRows := (numBars + maxPerRow - 1) / maxPerRow // ceil(numBars / maxPerRow)
	row := barIndex / maxPerRow
	col := barIndex % maxPerRow
	// Count how many bars are in this row (last row may have fewer)
	barsInRow := maxPerRow
	if row == numRows-1 {
		barsInRow = numBars - row*maxPerRow
	}
	// Divide window height evenly across rows
	rowY := (winH * int32(row)) / int32(numRows)
	rowH := (winH*int32(row+1))/int32(numRows) - rowY
	bx, bw := barBounds(winW, barsInRow, col)
	return bx, rowY, bw, rowH
}

// drawFrame updates state from snapshot, clears if layout changed, and draws all bars.
// When showAvgLine/showIOAvgLine are enabled, global average lines are drawn on top.
func drawFrame(renderer *sdl.Renderer, src stats.Source, cfg *config.Config, state *runState) {
	snap := src.Snapshot()
	numBars := countBars(snap, state.cpuMode, state.showMem, state.showNet, state.showLoad, state.diskMode)
	// Always clear the entire window before drawing. SDL2 uses double-buffering,
	// so skipping clear leaves stale content in the back buffer.
	renderer.SetDrawColor(0, 0, 0, 255)
	renderer.Clear()
	if state.showLoad {
		// Update the global load peak before drawing so bar scale is current.
		updateLoadPeak(snap, state, cfg.LoadMax)
	}
	if state.diskMode != constants.DiskModeOff {
		// Update the global disk peak before drawing so bar scale is current.
		updateDiskPeak(snap, state, cfg.DiskMax)
	}
	drawBars(renderer, snap, cfg, state, numBars)
	if state.showAvgLine {
		drawGlobalAvgLine(renderer, snap, state, numBars, cfg.MaxBarsPerRow)
	}
	if state.showIOAvgLine {
		drawGlobalIOAvgLine(renderer, snap, state, numBars, cfg.MaxBarsPerRow)
	}
	// Draw mouse-over tooltip and host highlight inversion on top of all bars
	drawOverlay(renderer, snap, cfg, state)
}

func countBars(snap map[string]*stats.HostStats, cpuMode int, showMem, showNet, showLoad bool, diskMode int) int {
	n := 0
	for _, host := range sortedHosts(snap) {
		if h := snap[host]; h != nil {
			n += len(sortedCPUNames(h.CPU, cpuMode))
			if showMem {
				n++
			}
			if showNet {
				n++
			}
			if showLoad {
				n++
			}
			n += len(sortedDiskNames(h.Disk, diskMode))
		}
	}
	if n == 0 {
		n = 1
	}
	return n
}

// drawBars draws CPU, memory, and network bars for all hosts in snap.
// Bars wrap into multiple rows when cfg.MaxBarsPerRow is set.
func drawBars(renderer *sdl.Renderer, snap map[string]*stats.HostStats, cfg *config.Config, state *runState, numBars int) {
	barIndex := 0
	hosts := sortedHosts(snap)
	maxPerRow := cfg.MaxBarsPerRow
	// Track separator rects (position + row height) for drawing after all bars
	type sepRect struct{ x, y, h int32 }
	var separators []sepRect
	for i, host := range hosts {
		h := snap[host]
		if h == nil {
			continue
		}
		drawHostBars(renderer, h, host, cfg, state, numBars, maxPerRow, &barIndex)
		// Record separator position between hosts (not after the last one)
		if state.showSeparators && i < len(hosts)-1 {
			sx, sy, _, sh := barRect(state.winW, state.winH, numBars, maxPerRow, barIndex)
			separators = append(separators, sepRect{sx, sy, sh})
		}
	}
	// Draw 1px red vertical separators on top of all bars (same color as CPU steal)
	for _, sep := range separators {
		renderer.SetDrawColor(constants.Red.R, constants.Red.G, constants.Red.B, 255)
		renderer.FillRect(&sdl.Rect{X: sep.x, Y: sep.y, W: 1, H: sep.h})
	}
}

// drawGlobalAvgLine draws a 1px red horizontal line at the Y position
// corresponding to the mean CPU usage across all hosts. When bars are
// split into multiple rows, one line is drawn per row at the correct
// proportional position within that row.
func drawGlobalAvgLine(renderer *sdl.Renderer, snap map[string]*stats.HostStats, state *runState, numBars, maxPerRow int) {
	var totalUsage float64
	var hostCount int
	for _, host := range sortedHosts(snap) {
		h := snap[host]
		if h == nil {
			continue
		}
		key := host + ";cpu"
		s := state.smoothedCPU[key]
		if s == nil {
			continue
		}
		// Sum all segments except idle (index 3) to get total CPU usage
		var usage float64
		for i := 0; i < 10; i++ {
			if i != 3 {
				usage += (*s)[i]
			}
		}
		totalUsage += usage
		hostCount++
	}
	if hostCount == 0 {
		return
	}
	avgPct := totalUsage / float64(hostCount)
	renderer.SetDrawColor(constants.Red.R, constants.Red.G, constants.Red.B, 255)
	// Draw one line per row, positioned proportionally within each row's height
	numRows := 1
	if maxPerRow > 0 && maxPerRow < numBars {
		numRows = (numBars + maxPerRow - 1) / maxPerRow
	}
	for row := 0; row < numRows; row++ {
		rowY := (state.winH * int32(row)) / int32(numRows)
		rowH := (state.winH*int32(row+1))/int32(numRows) - rowY
		lineY := rowY + rowH - int32(avgPct*float64(rowH)/100)
		if lineY < rowY {
			lineY = rowY
		}
		if lineY >= rowY+rowH {
			lineY = rowY + rowH - 1
		}
		renderer.FillRect(&sdl.Rect{X: 0, Y: lineY, W: state.winW, H: 1})
	}
}

// drawGlobalIOAvgLine draws a 1px pink horizontal line from the top of the window
// at the Y position corresponding to the mean I/O overhead (iowait + IRQ + softIRQ,
// indices 4, 5, 6 in the smoothed CPU array) across all hosts. When bars are split
// into multiple rows, one line is drawn per row.
func drawGlobalIOAvgLine(renderer *sdl.Renderer, snap map[string]*stats.HostStats, state *runState, numBars, maxPerRow int) {
	var totalIO float64
	var hostCount int
	for _, host := range sortedHosts(snap) {
		h := snap[host]
		if h == nil {
			continue
		}
		key := host + ";cpu"
		s := state.smoothedCPU[key]
		if s == nil {
			continue
		}
		// Sum iowait (4) + IRQ (5) + softIRQ (6) for I/O overhead
		totalIO += (*s)[4] + (*s)[5] + (*s)[6]
		hostCount++
	}
	if hostCount == 0 {
		return
	}
	avgPct := totalIO / float64(hostCount)
	renderer.SetDrawColor(constants.Pink.R, constants.Pink.G, constants.Pink.B, 255)
	// Draw one line per row, positioned proportionally from the top of each row
	numRows := 1
	if maxPerRow > 0 && maxPerRow < numBars {
		numRows = (numBars + maxPerRow - 1) / maxPerRow
	}
	for row := 0; row < numRows; row++ {
		rowY := (state.winH * int32(row)) / int32(numRows)
		rowH := (state.winH*int32(row+1))/int32(numRows) - rowY
		lineY := rowY + int32(avgPct*float64(rowH)/100)
		if lineY < rowY {
			lineY = rowY
		}
		if lineY >= rowY+rowH {
			lineY = rowY + rowH - 1
		}
		renderer.FillRect(&sdl.Rect{X: 0, Y: lineY, W: state.winW, H: 1})
	}
}

// drawHostBars draws CPU, mem, and net bars for one host and advances barIndex.
// maxPerRow controls multi-row wrapping (0 = single row).
func drawHostBars(renderer *sdl.Renderer, h *stats.HostStats, host string, cfg *config.Config, state *runState, numBars, maxPerRow int, barIndex *int) {
	cpuNames := sortedCPUNames(h.CPU, state.cpuMode)
	for _, name := range cpuNames {
		key := host + ";" + name
		cur := h.CPU[name]
		prev := state.prevCPU[key]
		state.prevCPU[key] = cur
		target, ok := cpuBarTargetPcts(cur, prev)
		s := state.smoothedCPU[key]
		if s == nil {
			s = &[10]float64{}
			state.smoothedCPU[key] = s
			if ok {
				*s = target
			}
		} else if ok {
			for i := 0; i < 10; i++ {
				(*s)[i] += (target[i] - (*s)[i]) * smoothFactor
			}
			normalizePcts(s)
		}
		peakPct := peakPctForBar(state, key, cfg.CPUAverage, s)
		x, y, barW, barH := barRect(state.winW, state.winH, numBars, maxPerRow, *barIndex)
		*barIndex++
		drawCPUBarFromPcts(renderer, s, barW, x, y, barH, state.extended, peakPct)
	}
	if state.showMem {
		if state.smoothedMem[host] == nil {
			state.smoothedMem[host] = &struct{ ramUsed, swapUsed float64 }{}
		}
		x, y, barW, barH := barRect(state.winW, state.winH, numBars, maxPerRow, *barIndex)
		*barIndex++
		drawMemBarSmoothed(renderer, h, state.smoothedMem[host], smoothFactor, barW, x, y, barH)
	}
	if state.showNet {
		if state.smoothedNet[host] == nil {
			state.smoothedNet[host] = &struct{ rxPct, txPct float64 }{}
		}
		x, y, barW, barH := barRect(state.winW, state.winH, numBars, maxPerRow, *barIndex)
		*barIndex++
		state.prevNet[host] = drawNetBarSmoothed(renderer, h, cfg, state.smoothedNet[host], state.prevNet[host], smoothFactor, barW, x, y, barH)
	}
	if state.showLoad {
		x, y, barW, barH := barRect(state.winW, state.winH, numBars, maxPerRow, *barIndex)
		*barIndex++
		drawLoadAvgBar(renderer, h, state.loadPeak, barW, x, y, barH)
	}
	// Disk I/O bars: aggregate (one bar) or per-device based on diskMode
	diskNames := sortedDiskNames(h.Disk, state.diskMode)
	for _, dname := range diskNames {
		key := host + ";disk;" + dname
		var cur stats.DiskStamp
		if dname == "all" {
			cur = sumAllDisks(h.Disk)
		} else {
			cur = h.Disk[dname]
		}
		if state.smoothedDisk[key] == nil {
			state.smoothedDisk[key] = &struct{ readPct, writePct float64 }{}
		}
		x, y, barW, barH := barRect(state.winW, state.winH, numBars, maxPerRow, *barIndex)
		*barIndex++
		state.prevDisk[key] = drawDiskBarSmoothed(renderer, cur, state, state.smoothedDisk[key], state.prevDisk[key], smoothFactor, barW, x, y, barH, state.extended)
	}
}

func peakPctForBar(state *runState, key string, cpuAvg int, s *[10]float64) float64 {
	if !state.extended || s == nil {
		return 0
	}
	userSys := (*s)[0] + (*s)[1]
	hist := state.peakHistory[key]
	hist = append(hist, userSys)
	n := cpuAvg
	if n < 1 {
		n = 1
	}
	for len(hist) > n {
		hist = hist[1:]
	}
	state.peakHistory[key] = hist
	var max float64
	for _, v := range hist {
		if v > max {
			max = v
		}
	}
	return max
}

func sortedHosts(snap map[string]*stats.HostStats) []string {
	out := make([]string, 0, len(snap))
	for h := range snap {
		out = append(out, h)
	}
	sort.Strings(out)
	return out
}

func sortedCPUNames(cpu map[string]collector.CPULine, cpuMode int) []string {
	// CPUModeOff: hide all CPU bars
	if cpuMode == constants.CPUModeOff {
		return nil
	}
	var names []string
	for name := range cpu {
		if name == "cpu" {
			// Aggregate bar always shown unless CPUModeOff
			names = append(names, "cpu")
			continue
		}
		// Individual core bars only shown in CPUModeCores
		if cpuMode == constants.CPUModeCores {
			names = append(names, name)
		}
	}
	sort.Slice(names, func(i, j int) bool {
		if names[i] == "cpu" {
			return true
		}
		if names[j] == "cpu" {
			return false
		}
		return names[i] < names[j]
	})
	return names
}

// cpuBarTargetPcts returns the 9 segment percentages (system, user, nice, idle, iowait, irq, softirq, guest, steal) from cur/prev delta. ok is false if no valid sample.
func cpuBarTargetPcts(cur, prev collector.CPULine) (out [10]float64, ok bool) {
	totalCur := cur.Total()
	totalPrev := prev.Total()
	if totalPrev == 0 || totalCur <= totalPrev {
		return out, false
	}
	scale := float64(totalCur-totalPrev) / 100.0
	if scale <= 0 {
		return out, false
	}
	out[0] = float64(cur.System-prev.System) / scale
	out[1] = float64(cur.User-prev.User) / scale
	out[2] = float64(cur.Nice-prev.Nice) / scale
	out[3] = float64(cur.Idle-prev.Idle) / scale
	out[4] = float64(cur.Iowait-prev.Iowait) / scale
	out[5] = float64(cur.IRQ-prev.IRQ) / scale
	out[6] = float64(cur.SoftIRQ-prev.SoftIRQ) / scale
	out[7] = float64(cur.Guest-prev.Guest) / scale
	out[8] = float64(cur.Steal-prev.Steal) / scale
	out[9] = float64(cur.GuestNice-prev.GuestNice) / scale
	for i := range out {
		if out[i] < 0 {
			out[i] = 0
		}
		if out[i] > 100 {
			out[i] = 100
		}
	}
	return out, true
}

func normalizePcts(s *[10]float64) {
	var sum float64
	for i := 0; i < 10; i++ {
		sum += (*s)[i]
	}
	if sum <= 0 {
		return
	}
	for i := 0; i < 10; i++ {
		(*s)[i] = (*s)[i] * 100 / sum
	}
}

// drawCPUBarFromPcts draws one CPU bar from 10 smoothed segment percentages.
// The bar occupies the region (x, y) with dimensions (barW, barH).
// If s is nil, only clears the slot. When extended is true and peakPct > 0,
// draws a 1px peak line (max system+user over history).
func drawCPUBarFromPcts(renderer *sdl.Renderer, s *[10]float64, barW int32, x, y, barH int32, extended bool, peakPct float64) {
	// Clear this slot so we never leave previous (e.g. mem/net) content visible
	renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
	renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: barH})
	if s == nil {
		return
	}
	pxPerPct := float64(barH) / 100.0
	curY := float64(y + barH)
	fill := func(r, g, b uint8, pct float64) {
		hh := int32(pct * pxPerPct)
		if hh < 1 && pct > 0 {
			hh = 1
		}
		curY -= float64(hh)
		renderer.SetDrawColor(r, g, b, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: int32(curY), W: barW, H: hh})
	}
	fill(constants.Blue.R, constants.Blue.G, constants.Blue.B, (*s)[0])                // system
	fill(constants.Yellow.R, constants.Yellow.G, constants.Yellow.B, (*s)[1])          // user
	fill(constants.Green.R, constants.Green.G, constants.Green.B, (*s)[2])             // nice
	fill(constants.LimeGreen.R, constants.LimeGreen.G, constants.LimeGreen.B, (*s)[9]) // guestnice
	fill(constants.Black.R, constants.Black.G, constants.Black.B, (*s)[3])             // idle
	fill(constants.Purple.R, constants.Purple.G, constants.Purple.B, (*s)[4])          // iowait
	fill(constants.White.R, constants.White.G, constants.White.B, (*s)[5])             // irq
	fill(constants.White.R, constants.White.G, constants.White.B, (*s)[6])             // softirq
	fill(constants.Red.R, constants.Red.G, constants.Red.B, (*s)[7])                   // guest
	fill(constants.Red.R, constants.Red.G, constants.Red.B, (*s)[8])                   // steal
	// Extended: 1px peak line at max (system+user) over history
	if extended && peakPct > 0 {
		peakY := y + barH - int32(peakPct*pxPerPct)
		if peakY < y {
			peakY = y
		}
		if peakY >= y+barH {
			peakY = y + barH - 1
		}
		if peakPct > float64(constants.UserOrangeThreshold) {
			renderer.SetDrawColor(constants.Orange.R, constants.Orange.G, constants.Orange.B, 255)
		} else if peakPct > float64(constants.UserYellowThreshold) {
			renderer.SetDrawColor(constants.Yellow0.R, constants.Yellow0.G, constants.Yellow0.B, 255)
		} else {
			renderer.SetDrawColor(constants.Yellow.R, constants.Yellow.G, constants.Yellow.B, 255)
		}
		renderer.FillRect(&sdl.Rect{X: x, Y: peakY, W: barW, H: 1})
	}
}

// drawMemBarSmoothed blends mem stats toward target and draws one memory bar (RAM left, Swap right).
// The bar occupies the region (x, y) with dimensions (barW, barH).
func drawMemBarSmoothed(renderer *sdl.Renderer, h *stats.HostStats, smoothed *struct{ ramUsed, swapUsed float64 }, factor float64, barW int32, x, y, barH int32) {
	// Clear this slot so we never leave previous (e.g. CPU/net) content visible
	renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
	renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: barH})
	if h.Mem == nil {
		return
	}
	var targetRam, targetSwap float64
	if memTotal := h.Mem["MemTotal"]; memTotal > 0 {
		targetRam = 100 - 100*float64(h.Mem["MemFree"])/float64(memTotal)
		if targetRam < 0 {
			targetRam = 0
		}
		if targetRam > 100 {
			targetRam = 100
		}
	}
	if swapTotal := h.Mem["SwapTotal"]; swapTotal > 0 {
		targetSwap = 100 - 100*float64(h.Mem["SwapFree"])/float64(swapTotal)
		if targetSwap < 0 {
			targetSwap = 0
		}
		if targetSwap > 100 {
			targetSwap = 100
		}
	}
	smoothed.ramUsed += (targetRam - smoothed.ramUsed) * factor
	smoothed.swapUsed += (targetSwap - smoothed.swapUsed) * factor

	halfW := barW / 2
	pxPerPct := float64(barH) / 100.0

	// RAM: used (dark grey) from bottom, free (black) on top
	ramUsedH := int32(smoothed.ramUsed * pxPerPct)
	if ramUsedH > 0 {
		renderer.SetDrawColor(constants.DarkGrey.R, constants.DarkGrey.G, constants.DarkGrey.B, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: y + barH - ramUsedH, W: halfW, H: ramUsedH})
	}
	if ramFreeH := barH - ramUsedH; ramFreeH > 0 {
		renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: y, W: halfW, H: ramFreeH})
	}

	// Swap: used (grey) from bottom, free (black) on top
	swapUsedH := int32(smoothed.swapUsed * pxPerPct)
	if swapUsedH > 0 {
		renderer.SetDrawColor(constants.Grey.R, constants.Grey.G, constants.Grey.B, 255)
		renderer.FillRect(&sdl.Rect{X: x + halfW, Y: y + barH - swapUsedH, W: halfW, H: swapUsedH})
	}
	if swapFreeH := barH - swapUsedH; swapFreeH > 0 {
		renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
		renderer.FillRect(&sdl.Rect{X: x + halfW, Y: y, W: halfW, H: swapFreeH})
	}
}

func printHotkeys() {
	fmt.Println("=> Hotkeys: 1=cores 2/m=mem 3/n=net 4/l=load 5=disk r=reset load peak e=extended g=avg line i=io avg s=separators h=help q=quit w=write config a/y=cpu avg d/c=net avg b/x=disk avg f/v=link scale arrows=resize")
}

// scaleLinkUp moves cfg.NetLink to the next higher link speed in linkScales.
// Clamps at the maximum (10gbit).
func scaleLinkUp(cfg *config.Config) {
	idx := linkScaleIndex(cfg.NetLink)
	if idx < len(linkScales)-1 {
		cfg.NetLink = linkScales[idx+1]
	}
	fmt.Println("==> Link scale:", cfg.NetLink)
}

// scaleLinkDown moves cfg.NetLink to the next lower link speed in linkScales.
// Clamps at the minimum (mbit).
func scaleLinkDown(cfg *config.Config) {
	idx := linkScaleIndex(cfg.NetLink)
	if idx > 0 {
		cfg.NetLink = linkScales[idx-1]
	}
	fmt.Println("==> Link scale:", cfg.NetLink)
}

// linkScaleIndex returns the index of the current NetLink value in linkScales.
// Defaults to 3 (gbit) if the value is not recognized.
func linkScaleIndex(netLink string) int {
	s := strings.ToLower(strings.TrimSpace(netLink))
	for i, v := range linkScales {
		if s == v {
			return i
		}
	}
	return 3 // default: gbit
}

func netLinkBytesPerSec(cfg *config.Config) int64 {
	s := strings.ToLower(strings.TrimSpace(cfg.NetLink))
	switch s {
	case "gbit", "1gbit":
		return int64(constants.BytesGbit)
	case "10gbit":
		return int64(constants.Bytes10Gbit)
	case "mbit", "1mbit":
		return int64(constants.BytesMbit)
	case "10mbit":
		return int64(constants.Bytes10Mbit)
	case "100mbit":
		return int64(constants.Bytes100Mbit)
	case "":
		return int64(constants.BytesGbit)
	}
	if n, err := strconv.ParseInt(s, 10, 64); err == nil {
		return n * int64(constants.BytesMbit)
	}
	return int64(constants.BytesGbit)
}

// sumNonLoNet aggregates RX (B) and TX (Tb) bytes across all non-lo interfaces,
// using the latest timestamp from any interface.
func sumNonLoNet(h *stats.HostStats) (sum stats.NetStamp, hasIface bool) {
	if h.Net == nil {
		return sum, false
	}
	for iface, ns := range h.Net {
		if iface == "lo" {
			continue
		}
		hasIface = true
		sum.B += ns.B
		sum.Tb += ns.Tb
		if ns.Stamp > sum.Stamp {
			sum.Stamp = ns.Stamp
		}
	}
	return sum, hasIface
}

// drawNetBarSmoothed sums RX/TX across all non-lo interfaces, computes utilization
// vs link speed, smooths toward target, and draws one net bar (RX left from top, TX right from bottom).
// The bar occupies the region (x, y) with dimensions (barW, barH).
// Smoothed values and prevNet are only updated when new collector data arrives
// (cur.Stamp > prev.Stamp), so the bar holds steady between collector cycles
// instead of decaying toward zero on frames with no new data.
// drawNetBarSmoothed sums RX/TX across all non-lo interfaces, computes utilization
// vs link speed, smooths toward target, and draws one net bar (RX left from top, TX right from bottom).
// The bar occupies the region (x, y) with dimensions (barW, barH).
// Smoothed values and prevNet are only updated when new collector data arrives
// (cur.Stamp > prev.Stamp), so the bar holds steady between collector cycles
// instead of decaying toward zero on frames with no new data.
func drawNetBarSmoothed(renderer *sdl.Renderer, h *stats.HostStats, cfg *config.Config, smoothed *struct{ rxPct, txPct float64 }, prev stats.NetStamp, factor float64, barW int32, x, y, barH int32) stats.NetStamp {
	// Clear this slot so we never leave previous (e.g. CPU/mem) content visible
	renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
	renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: barH})
	cur, hasIface := sumNonLoNet(h)
	if !hasIface {
		// No non-lo interface: show red bar
		renderer.SetDrawColor(constants.Red.R, constants.Red.G, constants.Red.B, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: barH})
		return prev
	}
	// Only recompute and smooth when the collector has provided new data.
	// The collector updates net stamps every ~2.8s, but drawFrame runs every
	// ~0.14s. Without this guard, the 19 intermediate frames would set
	// target to 0 (no delta) and smooth the bar toward zero, making real
	// traffic invisible.
	if cur.Stamp > prev.Stamp && prev.Stamp > 0 {
		prev = smoothNetUtilization(cur, prev, cfg, smoothed, factor)
	} else if prev.Stamp == 0 {
		// First sample: record it but don't draw yet (no delta available)
		prev = cur
	}
	drawNetHalves(renderer, smoothed, x, y, barW, barH)
	return prev
}

// smoothNetUtilization computes RX/TX utilization deltas and blends them into smoothed.
// Returns the updated previous stamp (cur) so callers can advance the baseline.
func smoothNetUtilization(cur, prev stats.NetStamp, cfg *config.Config, smoothed *struct{ rxPct, txPct float64 }, factor float64) stats.NetStamp {
	linkBps := netLinkBytesPerSec(cfg)
	if linkBps <= 0 {
		linkBps = int64(constants.BytesGbit)
	}
	dt := cur.Stamp - prev.Stamp
	if dt > 0 {
		deltaB := cur.B - prev.B
		deltaTb := cur.Tb - prev.Tb
		if deltaB < 0 {
			deltaB = 0
		}
		if deltaTb < 0 {
			deltaTb = 0
		}
		targetRx := 100 * float64(deltaB) / (float64(linkBps) * dt)
		targetTx := 100 * float64(deltaTb) / (float64(linkBps) * dt)
		smoothed.rxPct += (targetRx - smoothed.rxPct) * factor
		smoothed.txPct += (targetTx - smoothed.txPct) * factor
	}
	return cur // advance the baseline to the consumed sample
}

// drawNetHalves renders the RX (left half, from top) and TX (right half, from bottom)
// filled rectangles for one network bar using pre-smoothed utilization percentages.
func drawNetHalves(renderer *sdl.Renderer, smoothed *struct{ rxPct, txPct float64 }, x, y, barW, barH int32) {
	halfW := barW / 2
	pxPerPct := float64(barH) / 100.0
	halfH := barH / 2
	// Left half: RX from top (light green = used)
	rxH := int32(smoothed.rxPct * pxPerPct)
	if rxH > halfH {
		rxH = halfH
	}
	if rxH > 0 {
		renderer.SetDrawColor(constants.LightGreen.R, constants.LightGreen.G, constants.LightGreen.B, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: y, W: halfW, H: rxH})
	}
	if halfW > 0 && halfH-rxH > 0 {
		renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: y + rxH, W: halfW, H: halfH - rxH})
	}
	// Right half: TX from bottom (light green = used)
	txH := int32(smoothed.txPct * pxPerPct)
	if txH > halfH {
		txH = halfH
	}
	if txH > 0 {
		renderer.SetDrawColor(constants.LightGreen.R, constants.LightGreen.G, constants.LightGreen.B, 255)
		renderer.FillRect(&sdl.Rect{X: x + halfW, Y: y + barH - txH, W: halfW, H: txH})
	}
	if halfW > 0 && (barH-txH) > 0 {
		renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
		renderer.FillRect(&sdl.Rect{X: x + halfW, Y: y, W: halfW, H: barH - txH})
	}
}

// updateLoadPeak maintains the load scale used by the bar renderer.
// When loadMax > 0, the scale is pinned to that fixed value every frame
// (no decay, no tracking). When loadMax == 0, auto-scale is used: the
// global peak decays slowly (× 0.9999 per frame) with a floor of 2.0,
// and is updated with the current maximum 1-min load across all hosts.
func updateLoadPeak(snap map[string]*stats.HostStats, state *runState, loadMax float64) {
	if loadMax > 0 {
		state.loadPeak = loadMax // fixed scale: override every frame, skip auto logic
		return
	}
	state.loadPeak *= 0.9999 // slow per-frame decay toward idle baseline
	if state.loadPeak < 2.0 {
		state.loadPeak = 2.0
	}
	for _, h := range snap {
		if h == nil {
			continue
		}
		if l1, err := strconv.ParseFloat(strings.TrimSpace(h.LoadAvg1), 64); err == nil {
			if l1 > state.loadPeak {
				state.loadPeak = l1
			}
		}
	}
}

// drawLoadAvgBar renders a load-average bar for one host.
// The teal fill extends from the top downward proportional to the smoothed 1-min
// load average relative to the global loadPeak scale.
// A yellow 1px line marks the 5-min average and a white 1px line marks the
// 15-min average, giving a visual indication of load trend direction:
// when load is rising the reference lines appear inside the fill;
// when load is falling they hang below it.
func drawLoadAvgBar(renderer *sdl.Renderer, h *stats.HostStats, loadPeak float64, barW int32, x, y, barH int32) {
	// Clear this slot to black before drawing.
	renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255)
	renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: barH})

	// Load averages are already kernel-computed time averages; no further smoothing needed.
	l1, err1 := strconv.ParseFloat(strings.TrimSpace(h.LoadAvg1), 64)
	l5, err5 := strconv.ParseFloat(strings.TrimSpace(h.LoadAvg5), 64)
	l15, err15 := strconv.ParseFloat(strings.TrimSpace(h.LoadAvg15), 64)
	if err1 != nil || err5 != nil || err15 != nil {
		return // no valid data yet
	}

	clamp := func(v, lo, hi float64) float64 {
		if v < lo {
			return lo
		}
		if v > hi {
			return hi
		}
		return v
	}

	// Teal fill from top downward for 1-min load.
	l1H := int32(clamp(l1/loadPeak, 0, 1) * float64(barH))
	if l1H > 0 {
		renderer.SetDrawColor(constants.Teal.R, constants.Teal.G, constants.Teal.B, 255)
		renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: l1H})
	}

	// Yellow 1px line for 5-min average.
	l5Y := y + int32(clamp(l5/loadPeak, 0, 1)*float64(barH))
	if l5Y < y+barH {
		renderer.SetDrawColor(constants.Yellow.R, constants.Yellow.G, constants.Yellow.B, 255)
		renderer.DrawLine(x, l5Y, x+barW-1, l5Y)
	}

	// White 1px line for 15-min average.
	l15Y := y + int32(clamp(l15/loadPeak, 0, 1)*float64(barH))
	if l15Y < y+barH {
		renderer.SetDrawColor(constants.White.R, constants.White.G, constants.White.B, 255)
		renderer.DrawLine(x, l15Y, x+barW-1, l15Y)
	}
}