Jump to content

Command Crate ¿about?

Shell journal with date filtering, timestamp and Todo.txt tags (Bash)

#!/bin/sh
# Simple logging script with date filtering, colorizing, and Todo.txt compatibility.

TIMESTAMP=$(date "+%Y-%m-%d [%H:%M:%S]")
LOG="${HOME}/.jnrl.log"
CONTEXT="${CONTEXT:-20}"
TODOTXT_TAG='+jnrl'

# Ensure the log file exists
touch "$LOG"

# Colorize timestamp matches in log output
grep_coloring() {
  grep --color=always -P "(\[[0-9]{2}:[0-9]{2}:[0-9]{2}|\])" "$@"
}

# Handle date range filtering based on OS type (BSD vs GNU date)
if [ "$1" = "-d" ] && [ "$2" -ge 0 ] 2>/dev/null; then
  DATE_RANGE=$(date --date="$2 day ago" "+%Y-%m-%d" 2>/dev/null || date -v "-$2d" "+%Y-%m-%d")
  grep "$DATE_RANGE" "$LOG" | grep_coloring
elif [ -z "$*" ]; then
  # Show last N lines if no arguments are provided
  tail -n "$CONTEXT" "$LOG" | grep_coloring
else
  # Log the message with timestamp and Todo.txt tag, escaping special characters
  echo "${TIMESTAMP} - $(printf '%q' "$*") $TODOTXT_TAG" >> "$LOG"
fi

Logs messages with timestamps, allows date range searches, highlights timestamps, adds Todo.txt tags (+jrnl), and supports BSD/GNU date formats.

utilities [kb:217]