Skip to content

Commit 241d221

Browse files
committed
Add notifications to some operations
- Add `displayAlert()` for failure notifications so it doesn't disappear - Add `displayNotification()` that uses the macOS Notification Manager - Add check for `lima` in `$PATH` - Add notifications to image pull operation - Add notifications to VM start/stop Signed-off-by: Joe Block <[email protected]>
1 parent 0d383d6 commit 241d221

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

lima-plugin

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ function has() {
5050
which "$@" > /dev/null 2>&1
5151
}
5252

53+
function displayAlert() {
54+
alertCommand="display alert \"$1\" message \"$2\""
55+
osascript -e "$alertCommand"
56+
}
57+
58+
function displayNotification() {
59+
if [[ $# -eq 1 ]]; then
60+
message_command="display notification \"$1\""
61+
osascript -e "$message_command"
62+
fi
63+
64+
if [[ $# -eq 2 ]]; then
65+
message_command="display notification \"$2\" with title \"$1\""
66+
osascript -e "$message_command"
67+
fi
68+
69+
if [[ $# -eq 3 ]]; then
70+
message_command="display notification \"$3\" with title \"$1\" subtitle \"$2\""
71+
echo "message_command: $message_command"
72+
osascript -e "$message_command"
73+
fi
74+
75+
if [[ $# -eq 4 ]]; then
76+
message_command="display notification \"$4\" with title \"$1\" subtitle \"$2\" sound name \"$3\""
77+
osascript -e "$message_command"
78+
fi
79+
}
80+
5381
# Set up a working scratch directory
5482
SCRATCH_D=$(mktemp -d)
5583

@@ -63,10 +91,6 @@ trap cleanup EXIT
6391
export PATH="$PATH:/usr/local/bin"
6492
XBAR_PLUGIN="$0"
6593

66-
logger -t 'limamenu' "RUNNING_VM_COLOR=$RUNNING_VM_COLOR"
67-
logger -t 'limamenu' "STOPPED_VM_COLOR=$STOPPED_VM_COLOR"
68-
logger -t 'limamenu' "XBAR_PLUGIN=$XBAR_PLUGIN"
69-
7094
# shellcheck disable=SC2059
7195
function warnIfMissingDependencies() {
7296
local depsOK=1
@@ -75,6 +99,10 @@ function warnIfMissingDependencies() {
7599
warnings=$(printf "${warnings}\njq is not in your PATH! - click for installation instructions | color=$STOPPED_VM_COLOR href=https://stedolan.github.io/jq/download/\n")
76100
depsOK=0
77101
fi
102+
if ! has lima; then
103+
warnings=$(printf "${warnings}lima is not in your PATH! - click to get started | color=$STOPPED_VM_COLOR href=https://github.com/lima-vm/lima#getting-started\n")
104+
depsOK=0
105+
fi
78106
if ! has limactl; then
79107
warnings=$(printf "${warnings}limactl is not in your PATH! - click to get started | color=$STOPPED_VM_COLOR href=https://github.com/lima-vm/lima#getting-started\n")
80108
depsOK=0
@@ -166,7 +194,12 @@ function pullImage() {
166194
if [[ "$VM" != 'default' ]]; then
167195
export LIMA_INSTANCE="$VM"
168196
fi
169-
lima nerdctl image pull "$imageName"
197+
displayNotification lima "Pulling ${imageName}..."
198+
if lima nerdctl image pull "$imageName"; then
199+
displayNotification Lima "Pulled $imageName"
200+
else
201+
displayAlert Lima "Failed to pull $imageName"
202+
fi
170203
}
171204

172205
function rmImage() {
@@ -194,12 +227,20 @@ function processMenuCommand() {
194227
rmImage "$2" "$3" # pull imagename vmname
195228
;;
196229
start)
197-
logger -t 'limamenu' "Starting $2"
198-
limactl start "$2"
230+
displayNotification 'Lima' "Starting $2 VM"
231+
if limactl start "$2"; then
232+
displayNotification "Lima" "Started $2 VM successfully"
233+
else
234+
displayAlert "Lima" "Failed to start $2 VM"
235+
fi
199236
;;
200237
stop)
201-
logger -t 'limamenu' "Stopping $2"
202-
limactl stop "$2"
238+
displayNotification 'Lima' "Stopping $2 VM"
239+
if limactl stop "$2"; then
240+
displayNotification "Lima" "Stopped $2 VM successfully"
241+
else
242+
displayAlert "Lima" "Failed to stop $2 VM"
243+
fi
203244
;;
204245
esac
205246
}

0 commit comments

Comments
 (0)