bash-tpl icon indicating copy to clipboard operation
bash-tpl copied to clipboard

Feature request: pass format to printf

Open nkh opened this issue 2 years ago • 3 comments

Hi, nice work

Would it be possible to pass a format string to printf? Here's an example without

.DELIMS stmt-block="<% %>" tag="{{ }}"
<%
ENV_VARIABLE="$(printf "%17s" "$ENV_VARIABLE")"
name1="$(printf "%-13s" "$1")"
name2="$(printf "%-13s" "$2")"
%>


+---------------+---------+-------------------+
|               |         |                   |
+---------------+---------+-------------------+
| {{ $name1  }} | ....... | {{$ENV_VARIABLE}} |
| {{ $name2  }} | ....... | {{$ENV_VARIABLE}} |
+---------------+---------+-------------------+

and with,

.DELIMS tag="{{ }}"

+------------------+---------+-------------------+
|                  |         |                   |
+------------------+---------+-------------------+
| {{%-13s $1    }} | ....... | {{%17s $ENV_VARIABLE}} |
| {{%-13s $2    }} | ....... | {{%17s $ENV_VARIABLE}} |
+---------------+---------+-------------------+

nkh avatar Aug 30 '23 12:08 nkh

Greetings!

This is really great idea, and I wish I had thought of it earlier.

A challenge is determining a parsable pattern that works for all current tag types:

  • Normal Tags: <% ... %>
  • Quote Tags: <%"..."%>
  • Statement Tags: <%% ... %>

Of course we'll also want the delimiter(s) to be configurable.

I tend to want to put the formatting at the end of the tag vs the beginning to keep the focus on the data, but having it at the beginning will be easier to parse.

When using the default delimiters <% %>, I think we need to wrap the formatting in something like |%xx| so that the % doesn't conflict, especially if the default statement delimiter % is configured.

Something like:

  • Normal Tags: <%|%xx| ... %>
  • Quote Tags: <%|%xx|"..."%>
  • Statement Tags: <%|%xx|% ... %>

I do see value in a more verbose version, so maybe when the tag and statement delimiters are configured to be something other than <% %> and % we could support something like:

  • Normal Tags: {{%xx ... }}
  • Quote Tags: {{%xx "..."}}
  • Statement Tags: {{%xx $ ... }}

Essentially I see the default delimiters as |% and | and being able to configure them as % and (space) as long as they don't cause conflicts.

NOTE: I'm considering the % to be a configurable delimiter here so you could even remove it, ie:

  • Normal Tags: <%|xx| ... %>
  • Quote Tags: <%|xx|"..."%>
  • Statement Tags: <%|xx|% ... %>

I'm interested in your thoughts on these

Thanks for taking the time to create the discussion and for your interest in my project!

-TW

TekWizely avatar Oct 09 '23 21:10 TekWizely

hi, what about

{{ '%s %03d' $text $figure }}

that can be passed as is to printf, no need for a new format if an old one works and in this case it fits right in.

it would also make the template below possible

<%
color=green
%>

{{"$color %s" $some_variable}}

And maybe even

{{"%$format" $variable}}

it would have been nice to dynamically generate tables but the code below can't repeat dash signs

printf '=%.0s' {1..100}

nkh avatar Oct 09 '23 21:10 nkh

after testing a bit more I found out that the code below would work

printf -- '-%.0s' {1..13}

which means accepting

{{  -- '-%.0s' {1..13} }}
+------------------+---------+-------------------+
|                  |         |                   |
+------------------+---------+-------------------+
| {{%-13s $1    }} | ....... | {{%17s $ENV_VARIABLE}} |
| {{%-13s $2    }} | ....... | {{%17s $ENV_VARIABLE}} |
+---------------+---------+-------------------+

# ugly but parameterized
+-{{  -- '-%.0s' {1..13} }}-+---------+-{{  -- '-%.0s' {1..17} }}-+
| {{  -- '-%.0s' {1..13} }}  |           | {{  -- '-%.0s' {1..17} }} |
+-{{  -- '-%.0s' {1..13} }}-+---------+-{{  -- '-%.0s' {1..17} }}-+
| {{ '%-13s' $1            }} | ....... | {{ '%17s' $ENV_VARIABLE}} |
| {{ '%-13s' $1            }} | ....... | {{ '%17s' $ENV_VARIABLE}} |
+-{{  -- '-%.0s' {1..13} }}-+---------+-{{  -- '-%.0s' {1..17} }}-+

# and with some  pre-computation
{{ $HEADER }}
| {{ '%-13s' $1 }} | ....... | {{ '%17s' $ENV_VARIABLE}} |
| {{ '%-13s' $1 }} | ....... | {{ '%17s' $ENV_VARIABLE}} |
{{ $FOOTER}}

# and with column formats in variables

{{ $HEADER }}
| $cf1 $1 }} | ....... | {{ $cf2 $ENV_VARIABLE}} |
| $cf1 $1 }} | ....... | {{ $cf2 $ENV_VARIABLE}} |
{{ $FOOTER}}

nkh avatar Oct 09 '23 22:10 nkh