github.com/stevenmatthewt/agent@v3.5.4+incompatible/process/format.go (about)

     1  package process
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"unicode/utf8"
     7  )
     8  
     9  // FormatCommand formats a command amd arguments for human reading
    10  func FormatCommand(command string, args []string) string {
    11  	var truncate = func(s string, i int) string {
    12  		if len(s) < i {
    13  			return s
    14  		}
    15  
    16  		if utf8.ValidString(s[:i]) {
    17  			return s[:i] + "..."
    18  		}
    19  
    20  		return s[:i+1] + "..." // or i-1
    21  	}
    22  
    23  	s := []string{command}
    24  	for _, a := range args {
    25  		if strings.Contains(a, "\n") || strings.Contains(a, " ") {
    26  			s = append(s, fmt.Sprintf("%q", truncate(a, 120)))
    27  		} else {
    28  			s = append(s, a)
    29  		}
    30  	}
    31  
    32  	return strings.Join(s, " ")
    33  }