2015-01-12 03:56:17 +00:00
|
|
|
package util
|
2015-01-01 19:49:30 +00:00
|
|
|
|
2015-01-12 03:56:17 +00:00
|
|
|
import (
|
2016-08-18 17:39:32 +00:00
|
|
|
"math"
|
2015-01-12 03:56:17 +00:00
|
|
|
"os"
|
2021-06-01 07:55:51 +00:00
|
|
|
"strings"
|
2015-01-12 03:56:17 +00:00
|
|
|
"time"
|
2016-10-24 00:44:56 +00:00
|
|
|
|
2017-06-01 23:27:17 +00:00
|
|
|
"github.com/mattn/go-isatty"
|
|
|
|
"github.com/mattn/go-runewidth"
|
2021-05-14 02:43:32 +00:00
|
|
|
"github.com/rivo/uniseg"
|
2015-01-12 03:56:17 +00:00
|
|
|
)
|
2015-01-08 13:07:04 +00:00
|
|
|
|
2021-05-14 02:43:32 +00:00
|
|
|
// RunesWidth returns runes width
|
|
|
|
func RunesWidth(runes []rune, prefixWidth int, tabstop int, limit int) (int, int) {
|
|
|
|
width := 0
|
|
|
|
gr := uniseg.NewGraphemes(string(runes))
|
|
|
|
idx := 0
|
|
|
|
for gr.Next() {
|
|
|
|
rs := gr.Runes()
|
|
|
|
var w int
|
|
|
|
if len(rs) == 1 && rs[0] == '\t' {
|
|
|
|
w = tabstop - (prefixWidth+width)%tabstop
|
|
|
|
} else {
|
2021-06-01 07:55:51 +00:00
|
|
|
s := string(rs)
|
|
|
|
w = runewidth.StringWidth(s) + strings.Count(s, "\n")
|
2021-05-14 02:43:32 +00:00
|
|
|
}
|
|
|
|
width += w
|
|
|
|
if limit > 0 && width > limit {
|
|
|
|
return width, idx
|
|
|
|
}
|
|
|
|
idx += len(rs)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2021-05-14 02:43:32 +00:00
|
|
|
return width, -1
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Max returns the largest integer
|
2016-08-14 02:58:47 +00:00
|
|
|
func Max(first int, second int) int {
|
|
|
|
if first >= second {
|
|
|
|
return first
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2016-08-14 02:58:47 +00:00
|
|
|
return second
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
// Max16 returns the largest integer
|
|
|
|
func Max16(first int16, second int16) int16 {
|
|
|
|
if first >= second {
|
2016-03-01 18:06:21 +00:00
|
|
|
return first
|
|
|
|
}
|
|
|
|
return second
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
// Max32 returns the largest 32-bit integer
|
|
|
|
func Max32(first int32, second int32) int32 {
|
|
|
|
if first > second {
|
|
|
|
return first
|
|
|
|
}
|
|
|
|
return second
|
|
|
|
}
|
|
|
|
|
|
|
|
// Min returns the smallest integer
|
|
|
|
func Min(first int, second int) int {
|
2015-04-16 03:56:01 +00:00
|
|
|
if first <= second {
|
|
|
|
return first
|
|
|
|
}
|
|
|
|
return second
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
// Min32 returns the smallest 32-bit integer
|
|
|
|
func Min32(first int32, second int32) int32 {
|
|
|
|
if first <= second {
|
2015-01-08 17:37:08 +00:00
|
|
|
return first
|
|
|
|
}
|
|
|
|
return second
|
|
|
|
}
|
|
|
|
|
2015-03-18 16:59:14 +00:00
|
|
|
// Constrain32 limits the given 32-bit integer with the upper and lower bounds
|
|
|
|
func Constrain32(val int32, min int32, max int32) int32 {
|
|
|
|
if val < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if val > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2015-01-12 03:56:17 +00:00
|
|
|
// Constrain limits the given integer with the upper and lower bounds
|
|
|
|
func Constrain(val int, min int, max int) int {
|
|
|
|
if val < min {
|
|
|
|
return min
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-01-12 03:56:17 +00:00
|
|
|
if val > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return val
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-01-08 13:07:04 +00:00
|
|
|
|
2016-08-18 17:39:32 +00:00
|
|
|
func AsUint16(val int) uint16 {
|
|
|
|
if val > math.MaxUint16 {
|
|
|
|
return math.MaxUint16
|
|
|
|
} else if val < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return uint16(val)
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// DurWithin limits the given time.Duration with the upper and lower bounds
|
2015-01-08 13:07:04 +00:00
|
|
|
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
|
|
|
|
}
|
2015-01-12 03:56:17 +00:00
|
|
|
|
|
|
|
// IsTty returns true is stdin is a terminal
|
|
|
|
func IsTty() bool {
|
2016-10-24 00:44:56 +00:00
|
|
|
return isatty.IsTerminal(os.Stdin.Fd())
|
2016-02-06 16:49:29 +00:00
|
|
|
}
|
2019-12-09 12:32:58 +00:00
|
|
|
|
|
|
|
// Once returns a function that returns the specified boolean value only once
|
|
|
|
func Once(nextResponse bool) func() bool {
|
|
|
|
state := nextResponse
|
|
|
|
return func() bool {
|
|
|
|
prevState := state
|
|
|
|
state = false
|
|
|
|
return prevState
|
|
|
|
}
|
|
|
|
}
|