Skip to content

Commit bba5c9c

Browse files
committed
EasyBashGUI with notify functions...
1 parent c7c6c7c commit bba5c9c

File tree

7 files changed

+335
-176
lines changed

7 files changed

+335
-176
lines changed

Diff for: README

+37-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You can force widget usage through "supermode" variable ( possible values: "yad"
88
e.g.: >export supermode="kdialog" && source easybashgui && message Hello ).
99
NOTE on "none" mode: you have to make sure STDERR *is not* redirected, in order to see shell boxes in your terminal.
1010

11-
In EasyBashGUI "suite" there are a launcher ( "easybashgui" ), a launcher that toggles some debug options ( "easybashgui-debug" ), a widget library ( "easybashgui_X.X.X.lib" ), and a stand-alone script to create dialog boxes externally ( "easydialog.sh" ). Moreover there is an other library too ( "easybashlib" ) for optional ancillary functions ( thanks to it you don't need anymore to use "clean_temp()" function at the end of your EBG scripts ).
11+
In EasyBashGUI "suite" there are a launcher ( "easybashgui" ), a launcher that toggles some debug options ( "easybashgui-debug" ), a widget library ( "easybashgui_X.X.X.lib" ), and a stand-alone script (rather old nowadays) to create dialog boxes externally ( "easydialog.sh" ). Moreover there is an other library too ( "easybashlib" ) for optional ancillary functions ( thanks to it you don't need anymore to use "clean_temp()" function at the end of your EBG scripts ).
1212

1313

1414

@@ -21,6 +21,7 @@ Library functions.
2121
message
2222
ok_message
2323
alert_message
24+
notify_message
2425
text
2526
question
2627
input
@@ -34,9 +35,10 @@ wait_for
3435
terminate_wait_for
3536
progress
3637
adjust
38+
notify (*)
39+
notify_change (*)
3740

38-
39-
41+
(* => only if you have "yad" installed)
4042

4143

4244

@@ -95,6 +97,7 @@ question -> "[text]" => ( 1 argument, box output to exit c
9597
message -> "[text]" => ( 1 argument )
9698
alert_message -> "[text]" => ( 1 argument )
9799
ok_message -> "[text]" => ( 1 argument )
100+
notify_message -> "[text]" => ( 1 argument )
98101
text => ( STDIN, NO argument, box output to "${dir_tmp}/${file_tmp}" and STDERR ) (^)
99102
wait_seconds -> "[integer]" => ( 1 argument )
100103
wait_for -> "[text]" => ( 1 argument, PID to kill to "wait_for__PID" variable and STDERR ) (@)
@@ -110,6 +113,8 @@ list -> <+|->"[item 1]" ... <+|->"[item n]" => ( [n] arguments, optiona
110113
progress -> "[text]" => ( percent with or without '%' in STDIN, 1 argument )
111114
progress -> "[text]" "[elements number]" => ( "PROGRESS" string in STDIN, 2 arguments )
112115
adjust -> "[text]" "[min]" "[init]" "[max]" => ( 4 arguments, box output to "${dir_tmp}/${file_tmp}" and STDERR )
116+
notify -> <-c "[click command (for mouse left button)]"> <-i "[icon_good]#[icon_bad]"> <-t "[tooltip_good]#[tooltip_bad]"> "[menu item 1]" "[menu command 1]" ... "[menu item n]" "[menu command n]" => ( [n*2] arguments )
117+
notify_change -> "[good|bad]" => ( 1 argument )
113118

114119

115120
(*) = "0" exit status is "YES", "1" exit status is "NOT", other exit codes you should make program exit : normally in a script you have just to check exit status to know user choice ;
@@ -118,7 +123,7 @@ adjust -> "[text]" "[min]" "[init]" "[max]" => ( 4 arguments, box output to "${d
118123
(#) = take care that if you are in "console mode" or without X, throught cdialog, selection is done by SPACE key, and NOT by enter key : remember it ;
119124
(%) = "menu" and "list" functions differ about choices: menu allows single choice, list allows multiple choice ; since 7.1.0 version you can use tagged_menu(): it outputs tags (e.g.: "tagged_menu 1 A 2 B" -> if user selects tag "A" then function outputs item "1" );
120125

121-
Since EasyBashGUI v.1.2.4, all windows functions support options "<-w|-width> [integer]", and "<-h|-height> [integer]" for custom window size:
126+
Since EasyBashGUI v.1.2.4, all windows functions support options "<-w|-width> [integer]", and "<-h|-height> [integer]" for custom window size (note: not used for "notify_message"):
122127
E.g.: >alert_message -w 400 -h 340 "Error!"
123128

124129

@@ -184,7 +189,33 @@ for (( index=0 ; index < ${#women[@]} ; index++ }))
184189
done | progress "This is a _LOVE_ progress..." "${#women[@]}"
185190
# if you use "PROGRESS" string in STDIN do not forget second argument ( "[elements number]" )
186191

187-
( For easydialog.sh use and examples, you would launch it simply with "-h" option )
192+
9)
193+
notify -t "Good tooltip:OK#Bad tooltip:BAD" -i "/usr/local/share/pixmaps/nm-signal-100.png#gtk-fullscreen" "Xclock" "xclock" "Xcalc" "xcalc"
194+
#
195+
while :
196+
do
197+
menu GOOD BAD
198+
answer=$(0< "${dir_tmp}/${file_tmp}" )
199+
#
200+
if [ "${answer}" = "GOOD" ]
201+
then
202+
notify_message "Changed in \"good\" ..."
203+
notify_change "good"
204+
elif [ "${answer}" = "BAD" ]
205+
then
206+
notify_message "Changed in \"bad\" ..."
207+
notify_change "bad"
208+
else
209+
exit
210+
fi
211+
#
212+
done
213+
214+
215+
216+
217+
218+
( For old easydialog.sh use and examples, you would launch it simply with "-h" option )
188219

189220

190221

@@ -231,7 +262,7 @@ Thanks to Lucio Messina for "Debian policy" tips and support.
231262

232263

233264

234-
Please let me know if my work has been useful for you.
265+
Please, let me know if my work has been useful for you.
235266
Vittorio Cagnetta
236267
237268
https://github.com/BashGui/easybashgui

Diff for: lib/easybashgui.lib

100755100644
+249-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99
#########################
1010
#
11-
# Copyright (C) 2018 Vittorio Cagnetta
11+
# Copyright (C) 2019 Vittorio Cagnetta
1212
#
1313
# Author: Vittorio Cagnetta <[email protected]>
1414
#
@@ -35,7 +35,7 @@
3535
#####################################
3636
#####################################
3737
#
38-
LIB_VERSION="11.0.1"
38+
LIB_VERSION="12.0.0"
3939
LIB_NAME="easybashgui.lib"
4040
LIB_URL="https://github.com/BashGui/easybashgui"
4141
LIB_AUTHOR="Vittorio Cagnetta"
@@ -494,6 +494,24 @@ fi
494494
#
495495
###############
496496
#
497+
export notify_send_seconds=4
498+
export notify_check_loop_seconds=5
499+
#
500+
export notify_icon_default="gtk-stop"
501+
export notify_icon_good="gtk-home"
502+
export notify_icon_bad="gtk-help"
503+
#
504+
export notify_tooltip_default="${0##*/}-> starting..."
505+
export notify_tooltip_good="${0##*/}-> ok"
506+
export notify_tooltip_bad="${0##*/}-> bad!"
507+
#
508+
notify_send="NO"
509+
esistenza_notify_send="$(type 'notify-send' 2> /dev/null )"
510+
if [ ${#esistenza_notify_send} -gt 0 ]
511+
then
512+
notify_send="SI"
513+
fi
514+
#
497515
#####################################
498516
#
499517
# This is only for test, it's preferable that mode is set automatically...
@@ -868,6 +886,180 @@ exit_if_user_closes_window()
868886
}
869887
#
870888
##
889+
# notify(), notify_change()
890+
if [ "${yad}" = "SI" ]
891+
then
892+
# notify <-c "[click command (for mouse left button)]"> <-i "[icon_good]#[icon_bad]"> <-t "[tooltip_good]#[tooltip_bad]"> "[menu item 1]" "[menu command 1]" ... "[menu item n]" "[menu command n]"
893+
#
894+
notify()
895+
{
896+
local FUNCT_NAME="notify"
897+
local IFS=$' \t\n'
898+
#
899+
unset click_command
900+
unset icons
901+
unset tooltips
902+
#
903+
while getopts ":c:i:t:" function_options
904+
do
905+
case ${function_options} in
906+
#
907+
c ) export click_command="${OPTARG}" ;;
908+
i ) export icons="${OPTARG}" ;;
909+
t ) export tooltips="${OPTARG}" ;;
910+
#
911+
esac
912+
done
913+
shift $((OPTIND-1))
914+
#
915+
##############################
916+
#
917+
local cut_char="#"
918+
#
919+
: notify_icon_good
920+
: notify_icon_bad
921+
if [ ${#icons} -gt 0 ]
922+
then
923+
notify_icon_good="$(echo "${icons}" | cut -d "${cut_char}" -f 1 )"
924+
notify_icon_bad="$(echo "${icons}" | cut -d "${cut_char}" -f 2 )"
925+
fi
926+
unset icons
927+
#
928+
: notify_tooltip_good
929+
: notify_tooltip_bad
930+
if [ ${#tooltips} -gt 0 ]
931+
then
932+
notify_tooltip_good="$(echo "${tooltips}" | cut -d "${cut_char}" -f 1 )"
933+
notify_tooltip_bad="$(echo "${tooltips}" | cut -d "${cut_char}" -f 2 )"
934+
fi
935+
unset tooltips
936+
#
937+
unset notify_args
938+
unset notify_commands
939+
local IFS=$'\n'
940+
threshold=${#}
941+
for (( num=1,array_index=0 ; num <= ${threshold} ; num++,num++,array_index++ ))
942+
do
943+
notify_args[${array_index}]="${1}"
944+
notify_commands[${array_index}]="${2}"
945+
#
946+
shift ; shift
947+
done
948+
local IFS=$' \t\n'
949+
: notify_args[@]
950+
: notify_commands[@]
951+
#
952+
local IFS=$'\n'
953+
notify_items="$(for (( index=0 ; index < ${#notify_args[@]} ; index++ ))
954+
do
955+
echo -ne "|-> "
956+
echo -ne "${notify_args[${index}]}"
957+
echo -ne "!"
958+
echo -ne "${notify_commands[${index}]}"
959+
done )"
960+
local IFS=$' \t\n'
961+
#
962+
notify_menu=" ${supertitle}${notify_items}|-> Quit!quit"
963+
#
964+
##
965+
#
966+
# remove previously created FIFOs...
967+
for ebg_PIPE_file in $(ls "${dir_tmp}/${0##*/}".???????? 2>/dev/null )
968+
do
969+
#echo "Removing \"${ebg_PIPE_file}\" ..."
970+
rm "${ebg_PIPE_file}"
971+
done
972+
# create a new FIFO file, used to manage the I/O redirection from shell...
973+
export ebg_PIPE="$(mktemp -u "${dir_tmp}/${0##*/}.XXXXXXXX" )"
974+
mkfifo "${ebg_PIPE}"
975+
# eventually attach a file descriptor to the FIFO...
976+
exec 3<> "${ebg_PIPE}"
977+
#
978+
###
979+
#
980+
# add handler to manage process shutdown...
981+
notify_on_exit()
982+
{
983+
echo "quit" 1> "${ebg_PIPE}"
984+
rm -f "${ebg_PIPE}"
985+
clean_temp
986+
}
987+
trap notify_on_exit EXIT
988+
#
989+
# add handler for tray icon left click...
990+
notify_on_click()
991+
{
992+
echo "tooltip:${0##*/}-> wait..." 1> "${ebg_PIPE}"
993+
#
994+
if [ ${#click_command} -eq 0 ]
995+
then
996+
{
997+
export supericon="${notify_icon_good}"
998+
source easybashgui
999+
ok_message "\n ${supertitle}: powered by EasyBashGUI vers.${LIB_VERSION}"
1000+
clean_temp
1001+
} &>/dev/null
1002+
else
1003+
${click_command}
1004+
fi
1005+
#
1006+
}
1007+
export -f notify_on_click
1008+
#
1009+
#####################################
1010+
#####################################
1011+
#####################################
1012+
#
1013+
#
1014+
#########################
1015+
# finally create the notification...
1016+
#
1017+
yad --notification \
1018+
--no-middle \
1019+
--listen \
1020+
--kill-parent \
1021+
--image="${notify_icon_default}" \
1022+
--icon-size=48 \
1023+
--text="${notify_tooltip_default}" \
1024+
--menu="${notify_menu}" \
1025+
--command="bash -c notify_on_click" 0<&3 &
1026+
#
1027+
}
1028+
#
1029+
###
1030+
#
1031+
# notify_change "[good|bad]"
1032+
#
1033+
notify_change()
1034+
{
1035+
local FUNCT_NAME="notify_change"
1036+
local IFS=$' \t\n'
1037+
#
1038+
[ ${#ebg_PIPE} -eq 0 ] && break
1039+
[ ! -e "${ebg_PIPE}" ] && break
1040+
#
1041+
notify_change_how="${1}"
1042+
[ ${#notify_change_how} -eq 0 ] && notify_change_how="bad"
1043+
#
1044+
if [ "${notify_change_how}" = "good" -o "${notify_change_how}" = "GOOD" ]
1045+
then
1046+
#
1047+
echo "icon:${notify_icon_good}"
1048+
echo "tooltip:${notify_tooltip_good}"
1049+
#
1050+
elif [ "${notify_change_how}" = "bad" -o "${notify_change_how}" = "BAD" ]
1051+
then
1052+
#
1053+
echo "icon:${notify_icon_bad}"
1054+
echo "tooltip:${notify_tooltip_bad}"
1055+
#
1056+
fi 1> "${ebg_PIPE}"
1057+
#
1058+
}
1059+
#
1060+
fi #if [ "${yad}" = "SI" ]
1061+
# notify(), notify_change()
1062+
##
8711063
#
8721064
# question()
8731065
errexit_option="NO"
@@ -1166,6 +1358,61 @@ fi
11661358
#
11671359
##
11681360
#
1361+
# notify_message()
1362+
[ "${mode}" = "dialog" ] && notify_send="NO"
1363+
if [ "${notify_send}" = "SI" ]
1364+
then
1365+
notify_message()
1366+
{
1367+
local FUNCT_NAME="notify_message"
1368+
local IFS=$' \t\n'
1369+
#
1370+
notify_message_text="${@}"
1371+
#
1372+
notify_send_milliseconds=$(( ${notify_send_seconds} * 1000 ))
1373+
notify-send -t ${notify_send_milliseconds} -a "${0##*/}" -i "${notification_icon_default}" "${notify_message_text}"
1374+
#
1375+
}
1376+
elif [ "${notify_send}" = "NO" ]
1377+
then
1378+
notify_message()
1379+
{
1380+
local FUNCT_NAME="notify_message"
1381+
local IFS=$' \t\n'
1382+
#
1383+
export notify_message_text="${@}"
1384+
#
1385+
if [ "${mode}" = "dialog" ]
1386+
then
1387+
#
1388+
wait_for "${notify_message_text}"
1389+
sleep ${notify_send_seconds}
1390+
terminate_wait_for
1391+
#
1392+
elif [ "${mode}" != "dialog" ]
1393+
then
1394+
notify_dummy_var="$(
1395+
if [ "${kdialog}" = "SI" ]
1396+
then
1397+
export supermode=kdialog
1398+
fi
1399+
#
1400+
source easybashgui
1401+
#
1402+
wait_for "${notify_message_text}\n\n\n\n(Please install \"notify-send\" for a nicer notification)"
1403+
sleep ${notify_send_seconds}
1404+
terminate_wait_for
1405+
#
1406+
clean_temp
1407+
)" &>/dev/null &
1408+
fi
1409+
#
1410+
}
1411+
#
1412+
fi # if [ "${notify_send}" = "SI" ]
1413+
# notify_message()
1414+
##
1415+
#
11691416
# message()
11701417
if [ "${mode}" = "yad" ]
11711418
then

0 commit comments

Comments
 (0)