|
1 | 1 | {# Overview template macros #}
|
2 |
| - |
3 |
| -{% macro partial_emoji_overview(ns) %} |
4 |
| - |
5 |
| - # get the overview string for "❌", "✅", "❓". |
6 |
| - {%- for emoji in ["❌", "✅", "❓"] -%} |
7 |
| - {%- if emoji in varargs[0] %} |
8 |
| - {%- if emoji in varargs[1] %} |
9 |
| - # if "emoji" exists in each of the emoji count dictionaries, |
10 |
| - # generate a formatting string from the length of the longest |
11 |
| - # count between them. |
12 |
| - |
13 |
| - {% set _ = ns.elements.extend([emoji, varargs[0][emoji]]) -%} |
14 |
| - {% if varargs[0][emoji] | length > varargs[1][emoji] | length %} |
15 |
| - {% set index = 0 %} |
16 |
| - {% else %} |
17 |
| - {% set index = 1 %} |
18 |
| - {% endif %} |
19 |
| - {% set ns.format_string = ns.format_string + "%s " + "%" + |
20 |
| - varargs[index][emoji] | length | string + "s " %} |
21 |
| - {% else %} |
22 |
| - # if "emoji" exists in the first but not latter of the |
23 |
| - # emoji count dictionaries, generate formatting string |
24 |
| - # from the length of first count. |
25 |
| - |
26 |
| - {% set _ = ns.elements.extend([emoji, varargs[0][emoji]]) -%} |
27 |
| - {% set ns.format_string = ns.format_string + "%s " + "%" + |
28 |
| - varargs[0][emoji] | length | string + "s " %} |
29 |
| - {% endif %} |
30 |
| - |
31 |
| - {% else %} |
32 |
| - {%- if emoji in varargs[1] %} |
33 |
| - # if "emoji" exist in the latter but not first of the |
34 |
| - # emoji count dictionaries, insert "➖" instead. |
35 |
| - |
36 |
| - {% set _ = ns.elements.extend(["➖", ""]) %} |
37 |
| - {% set ns.format_string = ns.format_string + "%s " + "%" + |
38 |
| - varargs[1][emoji] | length | string + "s " %} |
39 |
| - {% endif %} |
40 |
| - |
41 |
| - {% endif -%} |
| 2 | +{% import "test.j2" as test_macros %} |
| 3 | + |
| 4 | +{%- macro emoji_counts() -%} |
| 5 | + {%- set ns = namespace( |
| 6 | + max_emojis={}, |
| 7 | + pieces=[], |
| 8 | + first=test_macros.WAIVED_STATUS_EMOJIS | length, |
| 9 | + last=-1 |
| 10 | + ) -%} |
| 11 | + |
| 12 | + {# Find maximum emoji counts accross all arguments #} |
| 13 | + {%- for emoji in test_macros.WAIVED_STATUS_EMOJIS.values() -%} |
| 14 | + {%- set _ = ns.max_emojis.__setitem__( |
| 15 | + emoji, varargs | map(attribute=emoji) | max |
| 16 | + ) -%} |
42 | 17 | {%- endfor -%}
|
43 |
| -{% endmacro %} |
44 |
| - |
45 |
| -{% macro emoji_overview() -%} |
46 |
| - {% set ns = namespace(elements=[], format_string='') %} |
47 |
| - {%- set _ = partial_emoji_overview(ns, varargs[0], varargs[1]) -%} |
48 | 18 |
|
49 |
| - {# generate formatting string for other emoji(s) in TESTS. #} |
50 |
| - {% for emoji in varargs[0] -%} |
51 |
| - {% if emoji not in ["❌", "✅", '❓'] %} |
52 |
| - {% set _ = ns.elements.extend([emoji, varargs[0][emoji]]) %} |
53 |
| - {% set ns.format_string = ns.format_string + "%" + |
54 |
| - emoji | length | string + "s %" + |
55 |
| - varargs[0][emoji] | length | string + "s " %} |
56 |
| - {% endif %} |
| 19 | + {# Build a list of "<emoji> <count>" pieces for all emojis #} |
| 20 | + {%- for emoji, count in varargs[0].items() -%} |
| 21 | + {%- if ns.max_emojis[emoji] > 0 -%} |
| 22 | + {# Remember the first non-zero emoji across all arguments #} |
| 23 | + {%- set ns.first = [ ns.first, loop.index0 ] | min -%} |
| 24 | + {# Add an "<emoji> <count>" piece #} |
| 25 | + {%- set _ = ns.pieces.extend([ |
| 26 | + ( |
| 27 | + "%s %" + |
| 28 | + (ns.max_emojis[emoji] | string | length | string) + |
| 29 | + "s" |
| 30 | + ) | |
| 31 | + format( |
| 32 | + "➖" if count == 0 else emoji, |
| 33 | + "" if count == 0 else (count | string) |
| 34 | + ) |
| 35 | + ]) -%} |
| 36 | + {# Remember the last non-zero emoji in this argument #} |
| 37 | + {%- if count > 0 -%} |
| 38 | + {%- set ns.last = loop.index0 -%} |
| 39 | + {%- endif -%} |
| 40 | + {%- endif -%} |
57 | 41 | {%- endfor -%}
|
58 | 42 |
|
59 |
| - {{- ns.format_string | format(*ns.elements) -}} |
| 43 | + {# Output the pieces of interest #} |
| 44 | + {{- ns.pieces[ns.first:ns.last + 1] | join(" ") -}} |
60 | 45 | {%- endmacro -%}
|
0 commit comments