From 6eb4b41e343a5eaba5aa02d521b88e28b203b099 Mon Sep 17 00:00:00 2001 From: Marcel Meyer Date: Fri, 19 Dec 2025 23:23:37 +0100 Subject: [PATCH] Add generic utils constraint function --- src/core.go | 2 +- src/terminal.go | 4 ++-- src/util/util.go | 22 ++-------------------- src/util/util_test.go | 29 ----------------------------- 4 files changed, 5 insertions(+), 52 deletions(-) diff --git a/src/core.go b/src/core.go index 024c6378..debcc3f1 100644 --- a/src/core.go +++ b/src/core.go @@ -530,7 +530,7 @@ func Run(opts *Options) (int, error) { break } if delay && reading { - dur := util.DurWithin( + dur := util.Constrain( time.Duration(ticks-startTick)*coordinatorDelayStep, 0, coordinatorDelayMax) time.Sleep(dur) diff --git a/src/terminal.go b/src/terminal.go index 6b8d3715..be97a686 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -3859,8 +3859,8 @@ func (t *Terminal) printColoredString(window tui.Window, text []rune, offsets [] maxOffset := int32(len(text)) var url *url for _, offset := range offsets { - b := util.Constrain32(offset.offset[0], index, maxOffset) - e := util.Constrain32(offset.offset[1], index, maxOffset) + b := util.Constrain(offset.offset[0], index, maxOffset) + e := util.Constrain(offset.offset[1], index, maxOffset) if url != nil && offset.url != url { url = nil window.LinkEnd() diff --git a/src/util/util.go b/src/util/util.go index 99506387..cf892398 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -1,11 +1,11 @@ package util import ( + "cmp" "math" "os" "strconv" "strings" - "time" "github.com/mattn/go-isatty" "github.com/rivo/uniseg" @@ -55,13 +55,7 @@ func Truncate(input string, limit int) ([]rune, int) { return runes, width } -// Constrain32 limits the given 32-bit integer with the upper and lower bounds -func Constrain32(val int32, minimum int32, maximum int32) int32 { - return max(min(val, maximum), minimum) -} - -// Constrain limits the given integer with the upper and lower bounds -func Constrain(val int, minimum int, maximum int) int { +func Constrain[T cmp.Ordered](val, minimum, maximum T) T { return max(min(val, maximum), minimum) } @@ -74,18 +68,6 @@ func AsUint16(val int) uint16 { return uint16(val) } -// DurWithin limits the given time.Duration with the upper and lower bounds -func DurWithin( - val time.Duration, min time.Duration, max time.Duration) time.Duration { - if val < min { - return min - } - if val > max { - return max - } - return val -} - // IsTty returns true if the file is a terminal func IsTty(file *os.File) bool { fd := file.Fd() diff --git a/src/util/util_test.go b/src/util/util_test.go index e989dce8..5a609587 100644 --- a/src/util/util_test.go +++ b/src/util/util_test.go @@ -4,7 +4,6 @@ import ( "math" "strings" "testing" - "time" ) func TestConstrain(t *testing.T) { @@ -20,22 +19,6 @@ func TestConstrain(t *testing.T) { } } -func TestConstrain32(t *testing.T) { - if Constrain32(-3, -1, 3) != -1 { - t.Error("Expected", -1) - } - if Constrain32(2, -1, 3) != 2 { - t.Error("Expected", 2) - } - - if Constrain32(5, -1, 3) != 3 { - t.Error("Expected", 3) - } - if Constrain32(0, math.MinInt32, math.MaxInt32) != 0 { - t.Error("Expected", 0) - } -} - func TestAsUint16(t *testing.T) { if AsUint16(5) != 5 { t.Error("Expected", 5) @@ -57,18 +40,6 @@ func TestAsUint16(t *testing.T) { } } -func TestDurWithIn(t *testing.T) { - if DurWithin(time.Duration(5), time.Duration(1), time.Duration(8)) != time.Duration(5) { - t.Error("Expected", time.Duration(0)) - } - if DurWithin(time.Duration(0)*time.Second, time.Second, time.Duration(3)*time.Second) != time.Second { - t.Error("Expected", time.Second) - } - if DurWithin(time.Duration(10)*time.Second, time.Duration(0), time.Second) != time.Second { - t.Error("Expected", time.Second) - } -} - func TestOnce(t *testing.T) { o := Once(false) if o() {