package jsh import ( "math" "unicode" "os" "os/exec" ) // FieldsN slices s into substrings after each instance of a whitespace // character (according to unicode.IsSpace) and returns a slice of those // substrings. The slice's length is guaranteed to be at most maxN, and // all leftover fields are put into the last substring. func FieldsN(s string, maxN int) []string { // First count the fields. n := 0 inField := false for _, rune := range s { wasInField := inField inField = !unicode.IsSpace(rune) if inField && !wasInField { n++ } } n = int(math.Min(float64(n), float64(maxN))) // Now create them. a := make([]string, n) na := 0 fieldStart := -1 // Set to -1 when looking for start of field. for i, rune := range s { if unicode.IsSpace(rune) && na+1 < maxN { if fieldStart >= 0 { a[na] = s[fieldStart:i] na++ fieldStart = -1 } } else if fieldStart == -1 { fieldStart = i } } if fieldStart >= 0 { // Last field might end at EOF. a[na] = s[fieldStart:] } return a } // Falls back to procps-ng passing the space-separated arguments in the given // args slice. If args is nil, we default to the arguments passed to the command // line using os.Args func FallbackWithArgs(program string, args []string) *[]byte { if args == nil { args = os.Args[1:] } out, err := exec.Command(program, args...).Output() if err != nil { panic(err) } return &out } func Fallback(program string) *[]byte { return FallbackWithArgs(program, nil) }