Revision 12857
Added by Aaron Marcuse-Kubitza almost 11 years ago
trunk/lib/sh/util.sh | ||
---|---|---|
368 | 368 |
alias split_lines='declare lines; "split_lines"' |
369 | 369 |
|
370 | 370 |
|
371 |
#### terminal |
|
372 |
|
|
373 |
### formatting |
|
374 |
|
|
375 |
has_bg() |
|
376 |
{ |
|
377 |
# inverse (black background)/set background (normal colors)/set background |
|
378 |
# (bright colors) (xfree86.org/current/ctlseqs.html#Character_Attributes) |
|
379 |
starts_with 7 "$1" || starts_with 4 "$1" || starts_with 10 "$1" |
|
380 |
} |
|
381 |
|
|
382 |
format() # usage: format format_expr msg |
|
383 |
# format_expr: the #s in xfree86.org/current/ctlseqs.html#Character_Attributes |
|
384 |
{ |
|
385 |
local format="$1" msg="$2" |
|
386 |
if starts_with '[' "$msg"; then format=; fi #don't add padding if formatted |
|
387 |
if has_bg "$format"; then msg=" $msg "; fi # auto-add padding if has bg |
|
388 |
echo "${format:+[0;${format}m}$msg${format:+[0m}" |
|
389 |
} |
|
390 |
|
|
391 |
|
|
371 | 392 |
#### paths |
372 | 393 |
|
373 | 394 |
strip/() { echo "${1%/}"; } # strips trailing /s |
... | ... | |
499 | 520 |
|
500 | 521 |
can_highlight_msg() { test "$log_level" -le 1; } |
501 | 522 |
|
502 |
has_bg() |
|
503 |
{ |
|
504 |
# inverse (black background)/set background (normal colors)/set background |
|
505 |
# (bright colors) (xfree86.org/current/ctlseqs.html#Character_Attributes) |
|
506 |
starts_with 7 "$1" || starts_with 4 "$1" || starts_with 10 "$1" |
|
507 |
} |
|
508 |
|
|
509 |
format() # usage: format format_expr msg |
|
510 |
# format_expr: the #s in xfree86.org/current/ctlseqs.html#Character_Attributes |
|
511 |
{ |
|
512 |
local format="$1" msg="$2" |
|
513 |
if starts_with '[' "$msg"; then format=; fi #don't add padding if formatted |
|
514 |
if has_bg "$format"; then msg=" $msg "; fi # auto-add padding if has bg |
|
515 |
echo "${format:+[0;${format}m}$msg${format:+[0m}" |
|
516 |
} |
|
517 |
|
|
518 | 523 |
highlight_msg() # usage: [format=...] highlight_msg msg |
519 | 524 |
# format: the # in xfree86.org/current/ctlseqs.html#Character_Attributes |
520 | 525 |
{ |
Also available in: Unified diff
lib/sh/util.sh: moved terminal formatting commands to own section