commit f56778a8ae8643074dff639ca8978e642e484657 Author: Alex Selimov Date: Thu Jul 9 23:08:42 2026 -0400 Initial version of habits diff --git a/habits.sh b/habits.sh new file mode 100755 index 0000000..0b81fb7 --- /dev/null +++ b/habits.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +set -euo pipefail + +HABITS_FILE="${HABITS_FILE:-$HOME/.local/share/os/habits.md}" + +usage() { + cat <<'EOF' +Usage: habits + +Commands: + open Open the habits file in $EDITOR + new Add a new day to the habits file +EOF +} + +ensure_file() { + mkdir -p "$(dirname "$HABITS_FILE")" + touch "$HABITS_FILE" +} + +open_habits() { + ensure_file + "${EDITOR:-vi}" "$HABITS_FILE" +} + +new_day() { + ensure_file + + local date + date="$(date +%F)" + + if grep -q "^# $date$" "$HABITS_FILE"; then + echo "A habits entry for $date already exists: $HABITS_FILE" >&2 + exit 1 + fi + + local tmp + tmp="$(mktemp)" + + { + printf '# %s\n\n' "$date" + printf '## Habits\n\n' + while IFS= read -r habit; do + [ -n "$habit" ] && printf -- '- [ ] %s\n' "$habit" + done <<< "$HABITS" + printf '\n## Metrics \n\n' + while IFS= read -r metric; do + [ -n "$metric" ] && printf -- '- %s\n' "$metric" ":" + done <<< "$METRICS" + printf '\n' + cat "$HABITS_FILE" + } > "$tmp" + + mv "$tmp" "$HABITS_FILE" + echo "Added $date to $HABITS_FILE" +} + +case "${1:-}" in + open) + open_habits + ;; + new) + new_day + ;; + -h|--help|help) + usage + ;; + *) + usage >&2 + exit 1 + ;; +esac