Skip to content

Instantly share code, notes, and snippets.

@nexus166
Last active May 18, 2020 08:02
Show Gist options
  • Select an option

  • Save nexus166/2a413f3c648b32f1fe125849c31dd253 to your computer and use it in GitHub Desktop.

Select an option

Save nexus166/2a413f3c648b32f1fe125849c31dd253 to your computer and use it in GitHub Desktop.
bash note taking
#!/usr/bin/env bash
set +xv -eo pipefail;
MODULE="${1:-$(id -nu)}";
LOGFILE="${2:-$(date +%Y-%b-%d_%H%M%S)}";
LOGDIR=${LOGDIR:-~/.log};
mkdir -vp "${LOGDIR}";
if [[ "${MODULE}" != "$(id -nu)" ]]; then
mkdir -vp "${LOGDIR}/${MODULE}";
LOGDIR="${LOGDIR}/${MODULE}";
fi;
LOGFILE="${LOGDIR}/${LOGFILE}";
touch "${LOGFILE}";
TTY="$(tty)";
TAILN="$(($(tput lines || printf 20)/4))";
clear > "${TTY}";
tail -v -n "${TAILN}" "${LOGFILE}";
while read -rp "$(printf '\n\n$> ')"; do
case "${REPLY}" in
c|clear)
printf '' > "${LOGFILE}";
;;
x*|exec*)
CMD=$(cut -d' ' -f2- <<< "${REPLY}");
printf '\n%s@%s\t%s\n' "${MODULE}" "$(date -u '+%FT%TZ')" "${CMD}" >> "${LOGFILE}";
${CMD} 2>&1 | tee -a "${LOGFILE}" || true;
;;
h|help)
printf '%s\n' "$(awk -v ORS=" " '/\|.*\)/ && !/printf/ {print $1}' ${0} | tr -d ')')";
;;
l|list)
find "${LOGDIR}";
;;
p|print)
clear > "${TTY}";
cat "${LOGFILE}";
;;
q|quit)
exit;
;;
*)
if [[ -n "${REPLY}" ]]; then
printf '\n%s@%s\t%s\n' "${MODULE}" "$(date -u '+%FT%TZ')" "${REPLY}" >> "${LOGFILE}";
clear > "${TTY}";
tail -v -n "${TAILN}" "${LOGFILE}";
fi;
;;
esac;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment