Skip to content

Instantly share code, notes, and snippets.

@stuart-warren
Last active September 20, 2021 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuart-warren/48769b7fc934002116e41833e27f4bf2 to your computer and use it in GitHub Desktop.
Save stuart-warren/48769b7fc934002116e41833e27f4bf2 to your computer and use it in GitHub Desktop.
log pomodoro task and turn on do-not-disturb (mac os)
#!/bin/bash
# xbar script https://github.com/matryer/xbar
# put in "${HOME}/Library/Application Support/xbar/plugins"
${HOME}/bin/start-pomodoro --countdown
#!/bin/bash
# put in "${HOME}/bin"
# macos: brew install coreutils
export PATH="${PATH}:/usr/local/bin"
POMODORO_MINS="25"
TASKDIR="${HOME}/.pomodoro"
TASKSLOG="${TASKDIR}/tasks"
mkdir -p "${TASKDIR}"
notify() {
local title="$1"
local body="$2"
osascript -e "display notification \"${body}\" with title \"${title}\""
}
endofpomodoro() {
local task="${@}"
setDoNotDisturb false
notify "end of pomodoro" "${task}\ntake a 5m break!"
}
log() {
local task="${@}"
echo "ts=\"$(gdate -u --rfc-3339=seconds)\" task=\"${task}\"" >>${TASKSLOG}
}
setDoNotDisturb() {
local bool="${@}"
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date "$(gdate -u +'%Y-%m-%dT%H:%M:%SZ')"
defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean ${bool}
killall NotificationCenter
}
closeApp() {
local app="${1}"
osascript -e "tell application \"${app}\" to close every window" || true
}
closeTab() {
local url="${1}"
osascript -e "tell application \"Google Chrome\"
set windowList to every tab of every window whose URL starts with \"${url}\"
repeat with tabList in windowList
set tabList to tabList as any
repeat with tabItr in tabList
set tabItr to tabItr as any
delete tabItr
end repeat
end repeat
end tell" | true
}
countdown() {
task="$(head -n1 ${TASKDIR}/enddate)"
secs="$(($(tail -n1 ${TASKDIR}/enddate)-$(gdate -u +%s)))"
if [[ ${secs:0:1} == "-" ]]; then # check for negative
secs="0"
fi
mins="$(((${secs}%3600)/60))"
secs="$((${secs}%60))"
printf " %s - %02dm %02ds\n" "$task" "$mins" "$secs"
}
start() {
if [[ "$1" == "--countdown" ]]; then
countdown
exit 0
fi
local task="${@}"
log "$task"
closeApp Slack
closeTab https://mail.google.com
setDoNotDisturb true
{
echo "$task" >${TASKDIR}/enddate
gdate -d "+${POMODORO_MINS} minutes" -u +%s >>${TASKDIR}/enddate
sleep ${POMODORO_MINS}m && endofpomodoro "$task"
}&
}
start "${@}"
# "run applescript" automator quick action
set task_details to text returned of (display dialog "What are you doing?" default answer "")
do shell script ("~/bin/start-pomodoro " & task_details)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment