-
Notifications
You must be signed in to change notification settings - Fork 24
/
bash-draw.sh
138 lines (109 loc) · 2.51 KB
/
bash-draw.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
#
# Public Functions:
#
# drawClear()
# drawColour(colour = DRAW_COL_DEF, bgColour = DRAW_COL_DEF)
#
# drawPlain(text, newLine = 0)
# drawSpecial(text, newLine = 0)
# drawHighlight(text, newLine = 0)
# drawPlainAt(left, top, text, newLine = 0)
# drawHighlightAt(left, top, text, newLine = 0)
#
#
# Colours
#
# DRAW_COL_DEF # Default colour
# DRAW_COL_BLACK
# DRAW_COL_WHITE
# DRAW_COL_RED
# DRAW_COL_GREEN
# DRAW_COL_YELLOW
# DRAW_COL_BLUE
# DRAW_COL_GRAY # Light gray (grey?)
#
# Ensure we are running under bash (will not work under sh or dash etc)
if [ "$BASH_SOURCE" = "" ]; then
echo "ERROR: bash-draw requires to be running under bash"
exit 1
fi
DRAW_COL_DEF=39
DRAW_COL_BLACK=30
DRAW_COL_WHITE=97
DRAW_COL_RED=31
DRAW_COL_GREEN=32
DRAW_COL_YELLOW=33
DRAW_COL_BLUE=34
DRAW_COL_GRAY=37
# drawClear()
drawClear() {
$ESC_WRITE "\033c"
}
# drawColour(colour = DRAW_COL_DEF, bgColour = DRAW_COL_DEF)
drawColour() {
local colour=$DRAW_COL_DEF
local bgColour=$((DRAW_COL_DEF+10))
if [[ ! -z "$1" && "$1" != "" ]]; then
colour="$1"
fi
if [[ ! -z "$2" && "$2" != "" ]]; then
bgColour="$2"
fi
$ESC_ECHO "\033c\033[H\033[J\033[${colour};${bgColour}m\033[J"
}
# drawPlain(text, newLine = 0)
drawPlain() {
if [[ -z "$2" || "$2" -eq 0 ]]; then
$ESC_WRITE "$1"
else
$ESC_ECHO "$1"
fi
}
# drawSpecial(text, newLine = 0)
drawSpecial() {
[[ -z "$2" ]] && newLine=0 || newLine="$2"
draw_SetDrawMode
drawPlain "$1" "$newLine"
draw_SetWriteMode
}
# drawHighlight(text, newLine = 0)
drawHighlight() {
[[ -z "$2" ]] && newLine=0 || newLine="$2"
draw_StartHighlight
drawPlain "$1" "$newLine"
draw_EndHighlight
}
# drawPlainAt(left, top, text, newLine = 0)
drawPlainAt() {
[[ -z "$4" ]] && newLine=0 || newLine="$4"
draw_MoveTo $1 $2
drawPlain "$3" "$newLine"
}
# drawHighlightAt(left, top, text, newLine = 0)
drawHighlightAt() {
[[ -z "$4" ]] && newLine=0 || newLine="$4"
draw_StartHighlight
drawPlainAt "$1" "$2" "$3" "$newLine"
draw_EndHighlight
}
# Write escape sequence with no newline
ESC_WRITE='echo -en'
# Write escape sequence adding newline
ESC_ECHO='echo -e'
# Move cursor to specified location
draw_MoveTo() {
$ESC_WRITE "\033[${1};${2}H"
}
draw_StartHighlight() {
$ESC_WRITE "\033[7m"
}
draw_EndHighlight() {
$ESC_WRITE "\033[27m"
}
draw_SetDrawMode() {
$ESC_WRITE "\033%@\033(0"
}
draw_SetWriteMode() {
$ESC_WRITE "\033(B"
}