英文:
How do you left align a printf on Docker format
问题
我正在尝试使用printf将一组docker service ps调用对齐在一起,但是当我使用printf时,它似乎将内容右对齐。
for service in $(command docker service ls --format "{{.Name}}")
do
  command docker service ps \
    --format "table {{ .Name | printf \"%20.20s\" }}\t{{ .Image | printf \"%40.40s\" }}\t{{ .Node | printf \"%20.20s\" }}\t{{.CurrentState}}\t{{.Error}}" \
    $service
done
如何使printf左对齐?
英文:
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 \"%20.20s\" }}\t{{ .Image | printf \"%40.40s\" }}\t{{ .Node | printf \"%20.20s\" }}\t{{.CurrentState}}\t{{.Error}}" \
    $service
done
How do I make the printf left align?
答案1
得分: 1
模板的printf函数使用了fmt包。fmt包中有一个-标志的文档:
> - 在右侧填充空格而不是左侧(左对齐字段)
所以,不要使用
{{ printf "%20.20s" }}
而是使用
{{ printf "%-20.20s" }}
英文:
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 "%20.20s" }}
Simply use
{{ printf "%-20.20s" }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论