Add generic utils constraint function

This commit is contained in:
Marcel Meyer
2025-12-23 09:14:33 +09:00
committed by Junegunn Choi
parent 14b5e1d88c
commit 6eb4b41e34
4 changed files with 5 additions and 52 deletions
+2 -20
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()