1
- echo " Pipe name: $1 "
2
- page_pipe=$1
1
+ # Constants
2
+ PGLET_VER=" 0.1.12" # Pglet version required by this script
3
+
4
+ # Default session variables:
5
+ PGLET_EXE=" " # full path to Pglet executable
6
+ PGLET_CONNECTION_ID=" " # the last page connection ID.
7
+ PGLET_PAGE_URL=" " # the last page URL.
8
+ PGLET_EVENT_TARGET=" " # the last received event target (control ID).
9
+ PGLET_EVENT_NAME=" " # the last received event name.
10
+ PGLET_EVENT_DATA=" " # the last received event data.
11
+
12
+ # Parameters:
13
+ # $1 - page name
14
+ # Variables:
15
+ # PGLET_WEB - makes the page available as public at pglet.io service or a self-hosted Pglet server
16
+ # PGLET_PRIVATE - makes the page available as private at pglet.io service or a self-hosted Pglet server
17
+ # PGLET_SERVER - connects to the page on a self-hosted Pglet server
18
+ # PGLET_TOKEN - authentication token for pglet.io service or a self-hosted Pglet server
19
+
20
+ function pglet_page() {
21
+ local pargs=(page)
22
+
23
+ if [[ " $1 " != " " ]]; then
24
+ pargs+=($1 )
25
+ fi
26
+
27
+ if [[ " $PGLET_WEB " == " true" ]]; then
28
+ pargs+=(--web)
29
+ fi
30
+
31
+ if [[ " $PGLET_SERVER " != " " ]]; then
32
+ pargs+=(--server $PGLET_SERVER )
33
+ fi
34
+
35
+ if [[ " $PGLET_TOKEN " != " " ]]; then
36
+ pargs+=(--token $PGLET_TOKEN )
37
+ fi
38
+
39
+ if [[ " $PGLET_NO_WINDOW " != " " ]]; then
40
+ pargs+=(--no-window)
41
+ fi
42
+
43
+ # execute pglet and get page connection ID
44
+ local page_results=` $PGLET_EXE " ${pargs[@]} " `
45
+ IFS=' ' read -r PGLET_CONNECTION_ID PGLET_PAGE_URL <<< " $page_results"
46
+
47
+ echo " Page URL: $PGLET_PAGE_URL "
48
+ }
49
+
50
+ function __pglet_start_session() {
51
+ echo " Started session: $1 "
52
+ PGLET_CONNECTION_ID=$1
53
+ local fn=$2
54
+
55
+ eval " $fn "
56
+ }
57
+
58
+ function pglet_app() {
59
+ local pargs=(app)
60
+
61
+ if [[ $# -eq 1 ]]; then
62
+ # only hander function specified
63
+ local fn=$1
64
+ elif [[ $# -eq 2 ]]; then
65
+ # page name and hander function specified
66
+ pargs+=($1 )
67
+ local fn=$2
68
+ else
69
+ echo " Error: wrong number of arguments"
70
+ exit 1
71
+ fi
72
+
73
+ if [[ " $PGLET_WEB " == " true" ]]; then
74
+ pargs+=(--web)
75
+ fi
76
+
77
+ if [[ " $PGLET_SERVER " != " " ]]; then
78
+ pargs+=(--server $PGLET_SERVER )
79
+ fi
80
+
81
+ if [[ " $PGLET_TOKEN " != " " ]]; then
82
+ pargs+=(--token $PGLET_TOKEN )
83
+ fi
84
+
85
+ if [[ " $PGLET_NO_WINDOW " != " " ]]; then
86
+ pargs+=(--no-window)
87
+ fi
88
+
89
+ # reset vars
90
+ PGLET_PAGE_URL=" "
91
+
92
+ # execute pglet
93
+ $PGLET_EXE " ${pargs[@]} " |
94
+ {
95
+ while read -r session_id
96
+ do
97
+ if [[ " $PGLET_PAGE_URL " == " " ]]; then
98
+ PGLET_PAGE_URL=" $session_id "
99
+ echo " Page URL: $PGLET_PAGE_URL "
100
+ else
101
+ __pglet_start_session $session_id $fn &
102
+ fi
103
+ done
104
+ }
105
+ }
106
+
107
+ function pglet_send() {
108
+ if [[ $# -eq 1 ]]; then
109
+ local conn_id=$PGLET_CONNECTION_ID
110
+ local cmd=$1
111
+ elif [[ $# -eq 2 ]]; then
112
+ local conn_id=$1
113
+ local cmd=$2
114
+ else
115
+ echo " Error: wrong number of arguments"
116
+ exit 1
117
+ fi
3
118
4
- function pglet() {
5
119
# send command
6
- echo " $1 " > " $page_pipe "
120
+ echo " $cmd " > " $conn_id "
121
+
122
+ # take result if command doesn't end with "f" (fire-and-forget)
123
+ if [[ " $cmd " =~ ^[[:space:]]* [A-Za-z]+f ]]; then
124
+ return
125
+ fi
7
126
8
127
# read result
9
- IFS=' ' read result_status result_value < " $page_pipe "
10
- echo $result_value
128
+ local firstLine=" true"
129
+ local result_value=" "
130
+ IFS=' '
131
+ while read -r line; do
132
+ if [[ $firstLine == " true" ]]; then
133
+ IFS=' ' read -r result_status result_value <<< " $line"
134
+ firstLine=" false"
135
+ if [[ " $result_status " == " error" ]]; then
136
+ echo " Error: $result_value "
137
+ exit 2
138
+ fi
139
+ else
140
+ result_value=" $line "
141
+ fi
142
+ echo " $result_value "
143
+ done < " $conn_id "
144
+ }
145
+
146
+ function pglet_add() {
147
+ pglet_send " add $1 "
148
+ }
149
+
150
+ function pglet_addf() {
151
+ pglet_send " addf $1 "
152
+ }
153
+
154
+ function pglet_set() {
155
+ pglet_send " set $1 "
156
+ }
157
+
158
+ function pglet_setf() {
159
+ pglet_send " setf $1 "
160
+ }
161
+
162
+ function pglet_set_value() {
163
+ pglet_send " set $1 value=\" ${2// \" / \\\" } \" "
11
164
}
12
165
13
- function pglet_event() {
166
+ function pglet_set_valuef() {
167
+ pglet_send " setf $1 value=\" ${2// \" / \\\" } \" "
168
+ }
169
+
170
+ function pglet_get_value() {
171
+ pglet_send " get $1 value"
172
+ }
173
+
174
+ function pglet_show() {
175
+ pglet_send " set $1 visible=true"
176
+ }
177
+
178
+ function pglet_hide() {
179
+ pglet_send " set $1 visible=false"
180
+ }
181
+
182
+ function pglet_enable() {
183
+ pglet_send " set $1 enabled=true"
184
+ }
185
+
186
+ function pglet_disable() {
187
+ pglet_send " set $1 enabled=false"
188
+ }
189
+
190
+ function pglet_clean() {
191
+ pglet_send " clean $1 "
192
+ }
193
+
194
+ function pglet_remove() {
195
+ pglet_send " remove $1 "
196
+ }
197
+
198
+ function pglet_wait_event() {
199
+ if [[ " $1 " != " " ]]; then
200
+ local conn_id=$1
201
+ else
202
+ local conn_id=$PGLET_CONNECTION_ID
203
+ fi
204
+
205
+ IFS=' ' read PGLET_EVENT_TARGET PGLET_EVENT_NAME PGLET_EVENT_DATA < " $conn_id .events"
206
+ }
207
+
208
+ function pglet_dispatch_events() {
14
209
# https://askubuntu.com/questions/992439/bash-pass-both-array-and-non-array-parameter-to-function
210
+
211
+ # echo "count: $#"
212
+
15
213
arr=(" $@ " )
16
214
IFS=' '
17
215
while true
18
216
do
19
- read eventTarget eventName eventData < " $page_pipe .events "
217
+ pglet_wait_event
20
218
for evt in " ${arr[@]} " ;
21
219
do
22
220
IFS=' ' read -r et en fn <<< " $evt"
23
- if [[ " $eventTarget " == " $et " && " $eventName " == " $en " ]]; then
221
+ if [[ " $PGLET_EVENT_TARGET " == " $et " && " $PGLET_EVENT_NAME " == " $en " ]]; then
24
222
eval " $fn "
25
223
return
26
224
fi
27
225
# echo "$et - $en - $fn"
28
226
done
29
227
done
30
- }
228
+ }
229
+
230
+ function __pglet_install() {
231
+ if [ " $( uname -m) " != " x86_64" ]; then
232
+ echo " Error: Unsupported architecture $( uname -m) . Only x64 binaries are available." 1>&2
233
+ exit 1
234
+ fi
235
+
236
+ if [ " $OS " = " Windows_NT" ]; then
237
+ echo " Error: Bash for Windows is not supported." 1>&2
238
+ exit 1
239
+ else
240
+ case $( uname -s) in
241
+ Darwin) target=" darwin-amd64.tar.gz" ;;
242
+ * ) target=" linux-amd64.tar.gz" ;;
243
+ esac
244
+ fi
245
+
246
+ # check if pglet.exe is in PATH already (development mode)
247
+ if command -v pglet & > /dev/null
248
+ then
249
+ PGLET_EXE=` which pglet`
250
+ return
251
+ fi
252
+
253
+ # check if there is Pglet aready installed
254
+ local pglet_dir=" $HOME /.pglet"
255
+ local pglet_bin=" $pglet_dir /bin"
256
+ PGLET_EXE=" $pglet_bin /pglet"
257
+
258
+ local ver=" $PGLET_VER "
259
+ local installed_ver=" "
260
+
261
+ if [ -f " $PGLET_EXE " ]; then
262
+ installed_ver=$( $PGLET_EXE --version)
263
+ fi
264
+
265
+ # echo "Installed version: $installed_ver"
266
+
267
+ if [[ " $installed_ver " != " $ver " ]]; then
268
+ printf " Installing Pglet v$ver ..."
269
+
270
+ if [ ! -d " $pglet_bin " ]; then
271
+ mkdir -p " $pglet_bin "
272
+ fi
273
+
274
+ local pglet_url=" https://github.com/pglet/pglet/releases/download/v${ver} /pglet-${target} "
275
+ local tempTar=" $HOME /.pglet/pglet.tar.gz"
276
+ curl -fsSL $pglet_url -o $tempTar
277
+ tar zxf $tempTar -C $pglet_bin
278
+ rm $tempTar
279
+
280
+ # echo "OK"
281
+ fi
282
+ }
283
+
284
+ __pglet_install
0 commit comments