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
|
|
|
// #include <unistd.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-02-06 16:49:29 +00:00
|
|
|
"os/exec"
|
2015-01-12 03:56:17 +00:00
|
|
|
"time"
|
2015-08-02 05:25:57 +00:00
|
|
|
"unicode/utf8"
|
2015-01-12 03:56:17 +00:00
|
|
|
)
|
2015-01-08 13:07:04 +00:00
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Max returns the largest integer
|
2015-01-01 19:49:30 +00:00
|
|
|
func Max(first int, items ...int) int {
|
|
|
|
max := first
|
|
|
|
for _, item := range items {
|
|
|
|
if item > max {
|
|
|
|
max = item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
|
2015-08-02 14:54:53 +00:00
|
|
|
// Min32 returns the smallest 32-bit integer
|
2015-04-16 03:56:01 +00:00
|
|
|
func Min32(first int32, second int32) int32 {
|
|
|
|
if first <= second {
|
|
|
|
return first
|
|
|
|
}
|
|
|
|
return second
|
|
|
|
}
|
|
|
|
|
2015-01-11 18:01:24 +00:00
|
|
|
// Max32 returns the largest 32-bit integer
|
2015-01-08 17:37:08 +00:00
|
|
|
func Max32(first int32, second int32) int32 {
|
|
|
|
if first > second {
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
return int(C.isatty(C.int(os.Stdin.Fd()))) != 0
|
|
|
|
}
|
2015-04-17 13:23:52 +00:00
|
|
|
|
2015-10-02 09:40:20 +00:00
|
|
|
// TrimRight returns rune array with trailing white spaces cut off
|
2015-08-02 05:00:18 +00:00
|
|
|
func TrimRight(runes []rune) []rune {
|
2015-04-17 13:23:52 +00:00
|
|
|
var i int
|
2015-08-02 05:00:18 +00:00
|
|
|
for i = len(runes) - 1; i >= 0; i-- {
|
|
|
|
char := runes[i]
|
2015-04-17 13:23:52 +00:00
|
|
|
if char != ' ' && char != '\t' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-08-02 05:00:18 +00:00
|
|
|
return runes[0 : i+1]
|
2015-04-17 13:23:52 +00:00
|
|
|
}
|
2015-08-02 05:25:57 +00:00
|
|
|
|
2015-10-02 09:40:20 +00:00
|
|
|
// BytesToRunes converts byte array into rune array
|
2015-08-02 05:25:57 +00:00
|
|
|
func BytesToRunes(bytea []byte) []rune {
|
|
|
|
runes := make([]rune, 0, len(bytea))
|
|
|
|
for i := 0; i < len(bytea); {
|
|
|
|
if bytea[i] < utf8.RuneSelf {
|
|
|
|
runes = append(runes, rune(bytea[i]))
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
r, sz := utf8.DecodeRune(bytea[i:])
|
|
|
|
i += sz
|
|
|
|
runes = append(runes, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runes
|
|
|
|
}
|
2015-10-02 09:40:20 +00:00
|
|
|
|
|
|
|
// TrimLen returns the length of trimmed rune array
|
|
|
|
func TrimLen(runes []rune) int {
|
|
|
|
var i int
|
|
|
|
for i = len(runes) - 1; i >= 0; i-- {
|
|
|
|
char := runes[i]
|
|
|
|
if char != ' ' && char != '\t' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Completely empty
|
|
|
|
if i < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var j int
|
|
|
|
for j = 0; j < len(runes); j++ {
|
|
|
|
char := runes[j]
|
|
|
|
if char != ' ' && char != '\t' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i - j + 1
|
|
|
|
}
|
2016-02-06 16:49:29 +00:00
|
|
|
|
|
|
|
// ExecCommand executes the given command with $SHELL
|
|
|
|
func ExecCommand(command string) *exec.Cmd {
|
|
|
|
shell := os.Getenv("SHELL")
|
|
|
|
if len(shell) == 0 {
|
|
|
|
shell = "sh"
|
|
|
|
}
|
|
|
|
return exec.Command(shell, "-c", command)
|
|
|
|
}
|