aboutsummaryrefslogtreecommitdiff
path: root/pb
blob: 3bbfe6665466a152f2c8e84aae179828526ee9c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash

# is int
pb_isint() { 
  if [[ $1 =~ ^[0-9]+$ ]] ; then
    return 0
  else
    return 1
  fi
}

# is number
pb_isnum() { 
  if [[ $1 =~ ^[+-]?([0-9]+([.][0-9]*)?|\.[0-9]+)$ ]] ; then
    return 0
  else
    return 1
  fi
}

# 256 colour functions 
# usage:
#  source ./pb
#  printf %b "$(pb_bg 196)$(pb_fg 254) RED $(pb_nc)\n"

pb_nc() {
  printf '\033[0;0m';
  exit;	
}

pb_fg() {

  if [ -z "$1" ] || ! pb_isint "$1" ; then
    return 1
  fi
  
  local x
  x=$(tput colors)

  if (("$1" < 256)) && (("$x" > 255)); then
    printf %s "\033[38;5;$1m"
  fi

  return 0

}

pb_bg() {

  if [ -z "$1" ] || ! pb_isint $1 ; then
    return 1
  fi

  local x
  x=$(tput colors)

  if (("$1" < 256)) && (("$x" > 255)); then
    printf %s "\033[48;5;$1m"
  fi

  return 0
}    
	
# end 256 colour functions