mirror of
https://github.com/junegunn/fzf.git
synced 2026-05-19 23:10:06 +08:00
Add generic utils constraint function
This commit is contained in:
committed by
Junegunn Choi
parent
14b5e1d88c
commit
6eb4b41e34
+1
-1
@@ -530,7 +530,7 @@ func Run(opts *Options) (int, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if delay && reading {
|
if delay && reading {
|
||||||
dur := util.DurWithin(
|
dur := util.Constrain(
|
||||||
time.Duration(ticks-startTick)*coordinatorDelayStep,
|
time.Duration(ticks-startTick)*coordinatorDelayStep,
|
||||||
0, coordinatorDelayMax)
|
0, coordinatorDelayMax)
|
||||||
time.Sleep(dur)
|
time.Sleep(dur)
|
||||||
|
|||||||
+2
-2
@@ -3859,8 +3859,8 @@ func (t *Terminal) printColoredString(window tui.Window, text []rune, offsets []
|
|||||||
maxOffset := int32(len(text))
|
maxOffset := int32(len(text))
|
||||||
var url *url
|
var url *url
|
||||||
for _, offset := range offsets {
|
for _, offset := range offsets {
|
||||||
b := util.Constrain32(offset.offset[0], index, maxOffset)
|
b := util.Constrain(offset.offset[0], index, maxOffset)
|
||||||
e := util.Constrain32(offset.offset[1], index, maxOffset)
|
e := util.Constrain(offset.offset[1], index, maxOffset)
|
||||||
if url != nil && offset.url != url {
|
if url != nil && offset.url != url {
|
||||||
url = nil
|
url = nil
|
||||||
window.LinkEnd()
|
window.LinkEnd()
|
||||||
|
|||||||
+2
-20
@@ -1,11 +1,11 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
"github.com/rivo/uniseg"
|
"github.com/rivo/uniseg"
|
||||||
@@ -55,13 +55,7 @@ func Truncate(input string, limit int) ([]rune, int) {
|
|||||||
return runes, width
|
return runes, width
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constrain32 limits the given 32-bit integer with the upper and lower bounds
|
func Constrain[T cmp.Ordered](val, minimum, maximum T) T {
|
||||||
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 {
|
|
||||||
return max(min(val, maximum), minimum)
|
return max(min(val, maximum), minimum)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,18 +68,6 @@ func AsUint16(val int) uint16 {
|
|||||||
return uint16(val)
|
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
|
// IsTty returns true if the file is a terminal
|
||||||
func IsTty(file *os.File) bool {
|
func IsTty(file *os.File) bool {
|
||||||
fd := file.Fd()
|
fd := file.Fd()
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConstrain(t *testing.T) {
|
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) {
|
func TestAsUint16(t *testing.T) {
|
||||||
if AsUint16(5) != 5 {
|
if AsUint16(5) != 5 {
|
||||||
t.Error("Expected", 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) {
|
func TestOnce(t *testing.T) {
|
||||||
o := Once(false)
|
o := Once(false)
|
||||||
if o() {
|
if o() {
|
||||||
|
|||||||
Reference in New Issue
Block a user