Created
March 13, 2023 04:39
-
-
Save mfischr/f1ee2967ec0181b934639c30f4e68f17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
# pj-append.bash is a timestamped log file for you, a human. Set up a cron job to launch it every | |
# hour to note what you were working on, or append lines from the terminal whenever you're chewing | |
# on a hard problem. | |
# | |
# Use the data to build a picture of what you worked on during the last week, or grep | |
# last quarter's log to find out why you decided to use library A instead of library B. | |
# | |
# (pj = personal journal. Using alias pj="pj-append.bash" makes it quicker to use from the terminal) | |
# | |
# Writes timestamped lines of text to quarterly log files like pj-2020Q3.log. The log files | |
# are automatically placed in the same directory as the script. | |
# | |
# Crontab looks like this: | |
# 0 * * * * <whatever-directory>/pj-append.bash | |
# | |
# Run without arguments from the terminal. Whatever you type gets appended to the current | |
# log file. You can also type "edit" to open a window to edit the current log file, or "tail" | |
# to print the last 10 lines. | |
# Change these to suit your preferences. Note that the exact syntax needed here can vary | |
# from terminal to term | |
TERMINAL=/usr/bin/gnome-terminal | |
EDITOR=gedit | |
# Regex that gets matched against date "+%u.%H" to determine whether we're within working hours | |
# (if you only want the window to pop up during working hours) | |
WORKING_HOURS_REGEX="[1-5]\.(0[89]|1[0-7])" | |
# Following is a bunch of crap to detect if we're not in an interactive terminal | |
# (like, if we're in a cron job) and, if not, re-launch from inside an interactive terminal. | |
# | |
# 1 is stdin; -t tests if it's a vaild file descriptor. If not, then that | |
# means we're not in an interactive terminal. | |
# http://stackoverflow.com/questions/911168/how-to-detect-if-my-shell-script-is-running-through-a-pipe | |
if [ ! -t 1 ]; then | |
if ! [[ $(date "+%u.%H") =~ $WORKING_HOURS_REGEX ]]; then | |
exit 0 | |
fi | |
# Make sure there's a display to talk to | |
# http://askubuntu.com/questions/202873/cron-cannot-run-gnome-terminal | |
if [[ -z "$DISPLAY" ]]; then | |
# :0 is usually the default display, except when it's not... so here's how we get it | |
_user=$(id -un) | |
export DISPLAY=$(w | sed -E "s/^${_user}[[:space:]]+(tty|:)[[:digit:]]+[[:space:]]+(:[[:digit:]]+) .*/\2/;t;d") | |
if [[ -z "$DISPLAY" ]]; then | |
echo "no currently active session; aborting" | |
exit 1 | |
fi | |
fi | |
# Launching X apps needs DBUS_SESSION_BUS_ADDRESS too. Luckily the format is very predictable | |
# This might not always work, later on we might need | |
# http://unix.stackexchange.com/questions/111188/using-notify-send-with-cron | |
# ps -u 1000 x -o pid | while read p; do grep -az "^DBUS" /proc/$p/environ; done | |
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus" | |
# relaunch in new environment. Note that the exact syntax needed here can vary from | |
# terminal to terminal | |
$TERMINAL -- "$0" "$@" | |
exit | |
fi | |
_mydir=$(dirname $(readlink -f "$0")) | |
month=$(date +%m) | |
month=${month#0} # remove the leading zero so it's not interpreted as octal | |
quarter=$((($month - 1) / 3 + 1)) | |
year=$(date +%Y) | |
logfile=$_mydir/pj-${year}Q${quarter}.log | |
timestamp=$(date +"%Y-%m-%d %H:%M:%S") | |
echo "What's up?" | |
# -e: allow editing (arrow keys, ctrl-backspace) | |
# -t: set timeout | |
read -e -t 300 what_is_up | |
if [[ "$what_is_up" = "edit" ]]; then | |
setsid $EDITOR $logfile > /dev/null 2>&1 | |
sleep .5 | |
elif [[ "$what_is_up" = "tail" ]]; then | |
echo "" | |
tail -n 10 $logfile | |
else | |
if [[ "$what_is_up" == "" ]]; then | |
echo "You really should write something." | |
read -t 300 what_is_up | |
fi | |
if [[ "$what_is_up" != "" ]]; then | |
echo "$timestamp $what_is_up" >> $logfile | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment