util.go
This file
The walk stopped after 200 commits: lines older than d4ad4a2 point at it.
[bash-completion] Fix default alias/variable completionJunegunn Choibefore adoptiond4ad4a2
lines 1–124why, one step back1package util23import (4 "math"5 "os"6 "time"78 "github.com/mattn/go-isatty"9 "github.com/mattn/go-runewidth"10)1112var _runeWidths = make(map[rune]int)1314// RuneWidth returns rune width15func RuneWidth(r rune, prefixWidth int, tabstop int) int {16 if r == '\t' {17 return tabstop - prefixWidth%tabstop18 } else if w, found := _runeWidths[r]; found {19 return w20 } else if r == '\n' || r == '\r' {21 return 122 }23 w := runewidth.RuneWidth(r)24 _runeWidths[r] = w25 return w26}2728// Max returns the largest integer29func Max(first int, second int) int {30 if first >= second {31 return first32 }33 return second34}3536// Max16 returns the largest integer37func Max16(first int16, second int16) int16 {38 if first >= second {39 return first40 }41 return second42}4344// Max32 returns the largest 32-bit integer45func Max32(first int32, second int32) int32 {46 if first > second {47 return first48 }49 return second50}5152// Min returns the smallest integer53func Min(first int, second int) int {54 if first <= second {55 return first56 }57 return second58}5960// Min32 returns the smallest 32-bit integer61func Min32(first int32, second int32) int32 {62 if first <= second {63 return first64 }65 return second66}6768// Constrain32 limits the given 32-bit integer with the upper and lower bounds69func Constrain32(val int32, min int32, max int32) int32 {70 if val < min {71 return min72 }73 if val > max {74 return max75 }76 return val77}7879// Constrain limits the given integer with the upper and lower bounds80func Constrain(val int, min int, max int) int {81 if val < min {82 return min83 }84 if val > max {85 return max86 }87 return val88}8990func AsUint16(val int) uint16 {91 if val > math.MaxUint16 {92 return math.MaxUint1693 } else if val < 0 {94 return 095 }96 return uint16(val)97}9899// DurWithin limits the given time.Duration with the upper and lower bounds100func DurWithin(101 val time.Duration, min time.Duration, max time.Duration) time.Duration {102 if val < min {103 return min104 }105 if val > max {106 return max107 }108 return val109}110111// IsTty returns true is stdin is a terminal112func IsTty() bool {113 return isatty.IsTerminal(os.Stdin.Fd())114}115116// Once returns a function that returns the specified boolean value only once117func Once(nextResponse bool) func() bool {118 state := nextResponse119 return func() bool {120 prevState := state121 state = false122 return prevState123 }124}