Add generic utils constraint function

This commit is contained in:
Marcel Meyer
2025-12-19 23:23:37 +01:00
committed by Junegunn Choi
parent 14b5e1d88c
commit 6eb4b41e34
4 changed files with 5 additions and 52 deletions

View File

@@ -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)

View File

@@ -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()

View File

@@ -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()

View File

@@ -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() {