Home > database >  How do you left align a printf on Docker format
How do you left align a printf on Docker format

Time:01-29

I am trying to align a set of docker service ps calls together using printf but it seems to align things on the right when I use printf

for service in $(command docker service ls --format "{{.Name}}")
do
  command docker service ps \
    --format "table {{ .Name | printf \" .20s\" }}\t{{ .Image | printf \"@.40s\" }}\t{{ .Node | printf \" .20s\" }}\t{{.CurrentState}}\t{{.Error}}" \
    $service
done

How do I make the printf left align?

CodePudding user response:

The template's printf function uses the fmt package. The fmt package documents there's a - flag:

- pad with spaces on the right rather than the left (left-justify the field)

So instead of

{{ printf " .20s" }}

Simply use

{{ printf "%-20.20s" }}
  •  Tags:  
  • Related