This identifier will calculate what percentage N is of C and display it in a coloured bar format, useful for rating scripts, it uses 20 characters, the majority are spaces but the middle shows the percentage, and if "text" exists, it will appear after the bar. If the percentage is more than 5 digits long it is displayed at the end of the bar after "text"
Usage: $bar(C,N,text)
alias bar {
set -u0 %i 1
set -u0 %p 5
while (( %i <= 20 )) {
if (( $calc( (100 / $$1) * $$2 ) >= %p )) set -u0 $+(%,c.,%i) 4,09
else set -u0 $+(%,c.,%i) 9,04
inc %i
inc %p 5
}
set %bar.per $round($calc( (100 / $$1) * $$2 ),0)
if (( $len(%bar.per) >= 6 )) { goto dig.more }
goto dig. $+ $len(%bar.per)
:dig.1
return $+(%c.1,$chr(32),%c.2,$chr(32),%c.3,$chr(32),%c.4,$chr(32),%c.5,$chr(32),%c.6,$chr(32),%c.7,$chr(32),%c.8,$chr(32),%c.9,$chr(32),%c.10,%bar.per,%c.11,%,%c.12,$chr(32),%c.13,$chr(32),%c.14,$chr(32),%c.15,$chr(32),%c.16,$chr(32),%c.17,$chr(32),%c.18,$chr(32),%c.19,$chr(32),%c.20,$chr(32),,$iif($3 != $null, $3 ))
:dig.2
return $+(%c.1,$chr(32),%c.2,$chr(32),%c.3,$chr(32),%c.4,$chr(32),%c.5,$chr(32),%c.6,$chr(32),%c.7,$chr(32),%c.8,$chr(32),%c.9,$left(%bar.per,1),%c.10,$right(%bar.per,1),%c.11,%,%c.12,$chr(32),%c.13,$chr(32),%c.14,$chr(32),%c.15,$chr(32),%c.16,$chr(32),%c.17,$chr(32),%c.18,$chr(32),%c.19,$chr(32),%c.20,$chr(32),,$iif($3 != $null, $3 ))
:dig.3
return $+(%c.1,$chr(32),%c.2,$chr(32),%c.3,$chr(32),%c.4,$chr(32),%c.5,$chr(32),%c.6,$chr(32),%c.7,$chr(32),%c.8,$chr(32),%c.9,$left(%bar.per,1),%c.10,$mid(%bar.per,2,1),%c.11,$right(%bar.per,1),%c.12,%,%c.13,$chr(32),%c.14,$chr(32),%c.15,$chr(32),%c.16,$chr(32),%c.17,$chr(32),%c.18,$chr(32),%c.19,$chr(32),%c.20,$chr(32),,$iif($3 != $null, $3 ))
:dig.4
return $+(%c.1, $chr(32),%c.2,$chr(32),%c.3,$chr(32),%c.4,$chr(32),%c.5,$chr(32),%c.6,$chr(32),%c.7,$chr(32),%c.8,$left(%bar.per,1),%c.9,$mid(%bar.per,2,1),%c.10,$mid(%bar.per,3,1),%c.11,$right(%bar.per,1),%c.12,%,%c.13,$chr(32),%c.14,$chr(32),%c.15,$chr(32),%c.16,$chr(32),%c.17,$chr(32),%c.18,$chr(32),%c.19,$chr(32),%c.20,$chr(32),,$iif($3 != $null, $3 ))
:dig.5
return $+(%c.1,$chr(32),%c.2,$chr(32),%c.3,$chr(32),%c.4,$chr(32),%c.5,$chr(32),%c.6,$chr(32),%c.7,$chr(32),%c.8,$left(%bar.per,1),%c.9,$mid(%bar.per,2,1),%c.10,$mid(%bar.per,3,1),%c.11,$mid(%bar.per,4,1),%c.12,$right(%bar.per,1),%c.13,%,%c.14,$chr(32),%c.15,$chr(32),%c.16,$chr(32),%c.17,$chr(32),%c.18,$chr(32),%c.19,$chr(32),%c.20,$chr(32),,$iif($3 != $null, $3 ))
:dig.more
return $+(%c.1,$chr(32),%c.2,$chr(32),%c.3,$chr(32),%c.4,$chr(32),%c.5,$chr(32),%c.6,$chr(32),%c.7,$chr(32),%c.8,$chr(32),%c.9,$chr(32),%c.10,$chr(32),%c.11,$chr(32),%c.12,$chr(32),%c.13,$chr(32),%c.14,$chr(32),%c.15,$chr(32),%c.16,$chr(32),%c.17,$chr(32),%c.18,$chr(32),%c.19,$chr(32),%c.20,$chr(32),,$iif($3 != $null,$3 $+ $chr(32)),$chr(32),%bar.per,%)
}
Here\'s what I came up now and includes the update that you did. I tested it & seems to works fine. This is just to give you an idea on how to shorten it. =)
alias bar {
var %l = 20, %a = $round($calc((100 / $$1) * $$2 ),0), %o = $round($calc(%l * (%a / 100)),0), %d = $round($calc(%l / 2),0), %b1 = $str($chr(160),%l)
var %p = $round($calc(($len(%a) + 1) / 2),0), %p1 = $calc(%d - %p), %p2 = $calc(%l - %p1 - $len(%a $+ %)), %b2 = $mid(%b1,1,%p1) $+ %a $+ % $+ $right(%b1,%p2)
return 08,12 $+ $iif($len(%a) > 5,%b1 $+ %a $+ %,$left(%b2,%o) $+ 12 $+ $chr(44) $+ 08 $+ $right(%b2,$calc(%l - %o)) $+ ) $3-
}
You can still shorten this & get rid of the while loop and the long last line.
Here\'s what I came up:
alias bar {
var %l = 20, %c = $calc($2 / $1), %d = $int($calc(%l * %c)), %p = $calc(%c * 100) $+ %, %ps = $int($calc($len(%p) / 2))
var %bar = $str($chr(160),%l), %p1 = $calc((%l / 2) - %ps), %p2 = $calc((%l / 2) - %ps + $len(%p))
%bar = $mid(%bar,1,%p1) $+ %p $+ $mid(%bar,%p2,$calc(%l - %p2))
return 12,08 $+ $left(%bar,%d) $+ 08,12 $+ $right(%bar,$calc(%l - %d)) $+ $3-
}
Same usage: $bar(C,N,text)