TekWizely / bash-tpl

A smart, lightweight shell script templating engine, written in Bash
MIT License
65 stars 4 forks source link

Feature request: pass format to printf #21

Open nkh opened 1 year ago

nkh commented 1 year ago

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}} |
+---------------+---------+-------------------+
TekWizely commented 11 months ago

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:

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:

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:

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:

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

nkh commented 11 months ago

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 commented 11 months ago

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}}