Skip to content

Commit 68a94e4

Browse files
committed
wip
1 parent ba806a9 commit 68a94e4

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

.github/actions/gitcoverage/action.yml

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,26 @@ runs:
8888
set -euo pipefail
8989
mkdir -p "coverage/${BRANCH}"
9090
91-
- name: Parse coverage input and generate SVG badge
91+
- name: Parse coverage input and generate SVG badge (variable width, 25% opacity text)
9292
shell: bash
9393
env:
9494
BRANCH: ${{ steps.branch.outputs.branch }}
9595
INPUT_COVERAGE: ${{ inputs.coverage }}
9696
run: |
9797
set -euo pipefail
98-
# Trim trailing % and whitespace
98+
99+
# --- 1) Normalize and validate percentage ---
99100
RAW="$INPUT_COVERAGE"
100101
PCT="$(echo "$RAW" | sed 's/[[:space:]]//g; s/%$//')"
101-
# Validate numeric (integer or float)
102102
if ! [[ "$PCT" =~ ^([0-9]+([.][0-9]+)?)$ ]]; then
103103
echo "Invalid coverage value: '$RAW' (expected a number like 83 or 83%)." >&2
104104
exit 1
105105
fi
106-
# Clamp to [0,100]
106+
# Clamp and standardize to 0..100 with 2 decimals; display as integer
107107
PCT=$(awk -v v="$PCT" 'BEGIN{ if (v<0) v=0; if (v>100) v=100; printf("%.2f", v) }')
108-
# Convert percentage -> hue (0 = red, 120 = green) for smooth red->orange->yellow->green
109-
# HSL: h in [0,120], s=1, l=0.4 (nice contrast). Convert to RGB then hex.
108+
VALUE="$(printf "%.0f" "$PCT")%"
109+
110+
# --- 2) Compute color (smooth red->green via HSL) ---
110111
read R G B HEX < <(awk -v p="$PCT" '
111112
function hsl2rgb(h,s,l, c,x,m,r,g,b,R,G,B) {
112113
c=(1 - ((2*l-1)<0?-(2*l-1):(2*l-1))) * s
@@ -127,13 +128,44 @@ runs:
127128
h=120.0*(p/100.0); s=1.0; l=0.40;
128129
print hsl2rgb(h,s,l);
129130
}')
130-
echo "Computed color $HEX for ${PCT}%"
131-
# Badge layout (simple, fixed width). Left label black, right value colored.
131+
132+
# --- 3) Variable width calculation (approx text widths) ---
132133
LABEL="coverage"
133-
VALUE="$(printf "%.0f" "$PCT")%"
134-
LEFT_W=76
135-
RIGHT_W=66
136-
WIDTH=$((LEFT_W+RIGHT_W))
134+
135+
# crude width table (px) for Verdana/DejaVu Sans at 11px; good enough for badges
136+
calc_width() {
137+
local s="$1"
138+
awk -v s="$s" '
139+
BEGIN{
140+
# baseline
141+
for(i=0;i<=9;i++) w[sprintf("%d",i)]=7
142+
w["%"]=8; w["."]=3; w["-"]=5; w["_"]=7; w[" "]=4
143+
split("abcdefghijklmnopqrstuvwxyz",a,""); for(i in a) w[a[i]]=7
144+
split("ABCDEFGHIJKLMNOPQRSTUVWXYZ",b,""); for(i in b) w[b[i]]=8
145+
total=0
146+
for(i=1;i<=length(s);i++){
147+
ch=substr(s,i,1)
148+
if (ch in w) total+=w[ch]; else total+=7
149+
}
150+
print total
151+
}'
152+
}
153+
154+
# paddings for each side
155+
PAD_L=6
156+
PAD_R=6
157+
158+
LW=$(calc_width "$LABEL")
159+
RW=$(calc_width "$VALUE")
160+
161+
LEFT_W=$((LW + PAD_L + PAD_R))
162+
RIGHT_W=$((RW + PAD_L + PAD_R))
163+
WIDTH=$((LEFT_W + RIGHT_W))
164+
165+
LABEL_X=$(( LEFT_W / 2 ))
166+
VALUE_X=$(( LEFT_W + RIGHT_W / 2 ))
167+
168+
# --- 4) Emit SVG (texts at 25% opacity) ---
137169
SVG="$(cat <<EOF
138170
<svg xmlns="http://www.w3.org/2000/svg" width="${WIDTH}" height="20" role="img" aria-label="${LABEL}: ${VALUE}">
139171
<linearGradient id="s" x2="0" y2="100%">
@@ -146,13 +178,14 @@ runs:
146178
<rect x="${LEFT_W}" width="${RIGHT_W}" height="20" fill="${HEX}"/>
147179
<rect width="${WIDTH}" height="20" fill="url(#s)"/>
148180
</g>
149-
<g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11">
150-
<text x="$((LEFT_W/2))" y="15">${LABEL}</text>
151-
<text x="$((LEFT_W + RIGHT_W/2))" y="15">${VALUE}</text>
181+
<g fill="#fff" opacity="0.25" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11">
182+
<text x="${LABEL_X}" y="15">${LABEL}</text>
183+
<text x="${VALUE_X}" y="15">${VALUE}</text>
152184
</g>
153185
</svg>
154186
EOF
155187
)"
188+
156189
echo "$SVG" > "coverage/${BRANCH}/badge.svg"
157190
158191
- name: Copy optional report.html

0 commit comments

Comments
 (0)