Project

General

Profile

« Previous | Next » 

Revision 14712

lib/sh/util.sh: terminal: moved before errors so it can be used by it

View differences:

trunk/lib/sh/util.sh
225 225
}
226 226

  
227 227

  
228
#### terminal
229

  
230
### formatting
231

  
232
has_bg()
233
{
234
	# inverse (black background)/set background (normal colors)/set background
235
	# (bright colors) (xfree86.org/current/ctlseqs.html#Character_Attributes)
236
	starts_with 7 "$1" || starts_with 4 "$1" || starts_with 10 "$1"
237
}
238

  
239
format() # usage: format format_expr msg
240
# format_expr: the #s in xfree86.org/current/ctlseqs.html#Character_Attributes
241
{
242
	local format="$1" msg="$2"
243
	if starts_with '[' "$msg"; then format=; fi #don't add padding if formatted
244
	if has_bg "$format"; then msg=" $msg "; fi # auto-add padding if has bg
245
	echo "${format:+[0;${format}m}$msg${format:+}"
246
}
247

  
248
plain() { echo "$1"; }
249

  
250
fade() { format 90 "$@"; } # medium gray fades on white *&* black backgrounds
251

  
252

  
228 253
#### errors
229 254

  
230 255
err_fd=2 # stderr
......
478 503
alias split_lines='declare lines; "split_lines"'
479 504

  
480 505

  
481
#### terminal
482

  
483
### formatting
484

  
485
has_bg()
486
{
487
	# inverse (black background)/set background (normal colors)/set background
488
	# (bright colors) (xfree86.org/current/ctlseqs.html#Character_Attributes)
489
	starts_with 7 "$1" || starts_with 4 "$1" || starts_with 10 "$1"
490
}
491

  
492
format() # usage: format format_expr msg
493
# format_expr: the #s in xfree86.org/current/ctlseqs.html#Character_Attributes
494
{
495
	local format="$1" msg="$2"
496
	if starts_with '[' "$msg"; then format=; fi #don't add padding if formatted
497
	if has_bg "$format"; then msg=" $msg "; fi # auto-add padding if has bg
498
	echo "${format:+[0;${format}m}$msg${format:+}"
499
}
500

  
501
plain() { echo "$1"; }
502

  
503
fade() { format 90 "$@"; } # medium gray fades on white *&* black backgrounds
504

  
505

  
506 506
#### paths
507 507

  
508 508
strip/() { echo "${1%/}"; } # strips trailing /s

Also available in: Unified diff