diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2015-01-12 12:56:17 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2015-01-12 12:56:17 +0900 |
| commit | cd847affb79ea6438c9721635724efc6f58e2215 (patch) | |
| tree | d1e631e3dca8832ee4c495924789f6697c3629cf /src/util/util.go | |
| parent | 7a2bc2cada971c7a390d09b0afda34780ff56fb6 (diff) | |
| download | fzf-cd847affb79ea6438c9721635724efc6f58e2215.tar.gz | |
Reorganize source code
Diffstat (limited to 'src/util/util.go')
| -rw-r--r-- | src/util/util.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/util/util.go b/src/util/util.go new file mode 100644 index 00000000..14833c04 --- /dev/null +++ b/src/util/util.go @@ -0,0 +1,56 @@ +package util + +// #include <unistd.h> +import "C" + +import ( + "os" + "time" +) + +// Max returns the largest integer +func Max(first int, items ...int) int { + max := first + for _, item := range items { + if item > max { + max = item + } + } + return max +} + +// Max32 returns the largest 32-bit integer +func Max32(first int32, second int32) int32 { + if first > second { + return first + } + return second +} + +// 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 + } + if val > max { + return max + } + return 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 is stdin is a terminal +func IsTty() bool { + return int(C.isatty(C.int(os.Stdin.Fd()))) != 0 +} |
