#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --dir PATH [--days N] [--include-overdue]" } target="" days=30 include_overdue=0 while [[ $# -gt 0 ]]; do case "$1" in --dir) target="${2:-}"; shift 2 ;; --days) days="${2:-}"; shift 2 ;; --include-overdue) include_overdue=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$target" || ! -d "$target" ]]; then echo "Укажите существующую папку через --dir" >&2 exit 2 fi if ! [[ "$days" =~ ^[0-9]+$ ]]; then echo "--days должен быть целым числом" >&2 exit 2 fi parse_epoch() { local value="$1" if date -d "$value" +%s >/dev/null 2>&1; then date -d "$value" +%s return 0 fi if date -j -f "%Y-%m-%d" "$value" +%s >/dev/null 2>&1; then date -j -f "%Y-%m-%d" "$value" +%s return 0 fi return 1 } matches="$(grep -RHnoE '@due\([0-9]{4}-[0-9]{2}-[0-9]{2}\)' "$target" --include='*.md' || true)" if [[ -z "$matches" ]]; then echo "Дедлайнов не найдено." exit 0 fi today_start="$(parse_epoch "$(date +%F)")" rows="" while IFS=: read -r file line match; do [[ -z "$file" ]] && continue day="$(printf '%s' "$match" | sed -E 's/.*\(([0-9]{4}-[0-9]{2}-[0-9]{2})\).*/\1/')" epoch="$(parse_epoch "$day" || true)" [[ -z "$epoch" ]] && continue diff_days=$(( (epoch - today_start) / 86400 )) if (( diff_days < 0 && include_overdue == 0 )); then continue fi if (( diff_days > days )); then continue fi text="$(sed -n "${line}p" "$file" | sed -E 's/^[[:space:]]*[-*][[:space:]]*\[[ xX]\][[:space:]]*//')" rows+="${epoch}|${diff_days}|${file}:${line}|${text}"$'\n' done <<< "$matches" if [[ -z "$rows" ]]; then echo "В выбранном горизонте дедлайнов нет." exit 0 fi printf '%s' "$rows" | sort -t'|' -k1,1n | while IFS='|' read -r epoch diff_days where text; do [[ -z "$epoch" ]] && continue if (( diff_days < 0 )); then status="просрочено на $((-diff_days)) дн." elif (( diff_days == 0 )); then status="сегодня" else status="через ${diff_days} дн." fi printf '%s %-18s %s\n' "$status" "$where" "$text" done