Proper method of padding text

55 views
Skip to first unread message

Rick Kasten

unread,
Dec 20, 2014, 3:16:07 PM12/20/14
to ansible...@googlegroups.com
I am trying to print a line of text to a file such that field1 is separated from field2 by 16 characters. I have a template like this working:

{% for item in collection %}
{{ item.name }}{{ "                " |truncate(16-(item.name|length)) }}{{ name.value }}
{% endfor %}


So the "  " field is 16 spaces, and the longest item.name is 15 chars, so that line should have only one space between it and its item.value, while other lines will have their item.value's lined up in the same column. This is mostly working, but this is my exact output:

item1           ...v01
item02          ...v2
the_15-chr_item ...v03
item0004        ...v04


Where are all the '...'s coming from? When I changed the 'truncate' line to 'truncate(13-(item.name|length)', I got this:

item1        ...v01
item02       ...v2
the_15-chr_item...v03
item0004     ...v04


Technically correct, but again still with '...'. What am I doing wrong?

Rick Kasten

unread,
Dec 20, 2014, 3:38:57 PM12/20/14
to ansible...@googlegroups.com
Furthermore, I can show that it is the 'truncate' part itself that is adding the '...':


{% for item in collection %}
{{ item.name }}{{ "                " |truncate(16-(item.name|length)) }}:{{ name.value }}
{% endfor %}


Produces the following output:

item1           ...:v01
item02          ...:v2
the_15-chr_item ...:v03
item0004        ...:v04

Matt Martz

unread,
Dec 20, 2014, 6:19:17 PM12/20/14
to ansible...@googlegroups.com
You should check out the jinja2 docs:


truncate will add "..." unless you specify end=''

Also you might be able to use the "indent" filter for adding the spacing for you:

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e194803b-537d-4421-a3e5-3a005aeceb12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Rick Kasten

unread,
Dec 20, 2014, 8:09:13 PM12/20/14
to ansible...@googlegroups.com
Thank you. I did read the documentation on truncate, but obviously not closely enough.....
Reply all
Reply to author
Forward
0 new messages