2024-02-25 17:46:04 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
C_RESET="\x1b[0m"
|
|
|
|
C_GREEN="\x1b[32m"
|
|
|
|
C_RED="\x1b[31m"
|
|
|
|
C_MAGENTA="\x1b[35m"
|
|
|
|
C_CYAN="\x1b[36m"
|
|
|
|
|
|
|
|
C_HEADLINE="\x1b[30;44;1m"
|
|
|
|
C_SUBHEADING="\x1b[33;1m"
|
|
|
|
C_DEADLINE="\x1b[31;1m"
|
2024-03-07 09:17:29 +01:00
|
|
|
C_TIME="\x1b[36;3m"
|
2024-02-25 17:46:04 +01:00
|
|
|
|
|
|
|
year="$(date +%Y)"
|
|
|
|
|
|
|
|
# Just start helix if no arguments are given
|
|
|
|
if [ -z "${1+x}" ]; then
|
2024-03-03 11:12:28 +01:00
|
|
|
# Switch directory
|
|
|
|
pushd "$XDG_DATA_HOME/life.md" || exit 1
|
|
|
|
# Open Helix with the current year
|
|
|
|
hx "$year.md" +99999
|
|
|
|
# Commit changes if any exist
|
|
|
|
if ! git diff --quiet "$year.md"; then
|
|
|
|
git add "$year.md"
|
|
|
|
git commit -m "Auto Commit $(date +"%F %R")"
|
2024-03-07 15:33:56 +01:00
|
|
|
export GIT_SSH_COMMAND="ssh -o BatchMode=yes"
|
2024-03-11 19:11:07 +01:00
|
|
|
(git pull --rebase >/dev/null 2>&1 && git push >/dev/null 2>&1) || (echo "Sync failed" && exit 4) &
|
2024-03-03 11:12:28 +01:00
|
|
|
fi
|
|
|
|
# Exit
|
|
|
|
popd || exit 2
|
2024-02-25 17:46:04 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
DASH=$(printf '%0*s' "$(tput cols)" "" | tr ' ' '-')
|
|
|
|
|
|
|
|
function print_help_and_exit() {
|
|
|
|
printf "Usage: note [ACTION]\n"
|
|
|
|
printf "\n"
|
|
|
|
printf "Without arguments, will open helix with the note file\n"
|
|
|
|
printf "\n"
|
|
|
|
printf "Arguments:\n"
|
|
|
|
printf " todo | todos [TAG]\n"
|
|
|
|
printf " Show only todos, optionally filtered by #TAG\n"
|
2024-02-26 10:32:38 +01:00
|
|
|
printf " done | did\n"
|
|
|
|
printf " Show only todos that I did today\n"
|
2024-02-25 17:46:04 +01:00
|
|
|
printf " day | today\n"
|
|
|
|
printf " Show only todays entry\n"
|
|
|
|
printf " -1 | -2 | -3\n"
|
|
|
|
printf " Show only yesterday/the day before/etc\n"
|
2024-03-07 15:33:56 +01:00
|
|
|
printf " git GIT_COMMAND\n"
|
|
|
|
printf " Execute a git command in the note repository\n"
|
2024-03-11 19:09:50 +01:00
|
|
|
printf " dir\n"
|
|
|
|
printf " Print the note directory\n"
|
2024-03-23 14:34:05 +01:00
|
|
|
printf " log\n"
|
|
|
|
printf " Print the entire note file\n"
|
2024-02-25 17:46:04 +01:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2024-03-07 15:33:56 +01:00
|
|
|
function run_git_command_by_args_and_exit() {
|
|
|
|
# Switch directory
|
|
|
|
pushd "$XDG_DATA_HOME/life.md" || exit 1
|
|
|
|
# Run command
|
|
|
|
git "$@"
|
|
|
|
# Exit
|
|
|
|
popd || exit 2
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2024-02-25 17:46:04 +01:00
|
|
|
ONLY_TODOS=
|
2024-02-26 10:32:38 +01:00
|
|
|
ONLY_DONE=
|
2024-02-25 17:46:04 +01:00
|
|
|
ONLY_DAY=
|
|
|
|
FILTER_TAG=
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
todo | todos)
|
|
|
|
ONLY_TODOS=1
|
|
|
|
shift # past argument
|
|
|
|
if [ -n "${1+x}" ]; then
|
|
|
|
FILTER_TAG="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
;;
|
2024-02-26 10:32:38 +01:00
|
|
|
done | did)
|
|
|
|
ONLY_TODOS=1
|
|
|
|
ONLY_DONE=1
|
|
|
|
shift # past argument
|
|
|
|
;;
|
2024-02-25 17:46:04 +01:00
|
|
|
day | today)
|
|
|
|
ONLY_DAY=today
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
-1 | -2 | -3)
|
|
|
|
ONLY_DAY="$1days"
|
|
|
|
shift
|
|
|
|
;;
|
2024-03-07 15:33:56 +01:00
|
|
|
git)
|
|
|
|
shift
|
|
|
|
run_git_command_by_args_and_exit "$@"
|
|
|
|
;;
|
2024-03-11 19:09:50 +01:00
|
|
|
dir)
|
|
|
|
shift
|
|
|
|
echo "$XDG_DATA_HOME/life.md"
|
|
|
|
exit
|
|
|
|
;;
|
2024-03-23 14:34:05 +01:00
|
|
|
log)
|
|
|
|
shift
|
|
|
|
;;
|
2024-02-25 17:46:04 +01:00
|
|
|
-h | --help)
|
|
|
|
print_help_and_exit
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown option $1"
|
|
|
|
print_help_and_exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
bat "$XDG_DATA_HOME/life.md/$year.md" |
|
|
|
|
{
|
|
|
|
if [ -n "$ONLY_TODOS" ]; then
|
|
|
|
rg "^- \[.\] " |
|
|
|
|
sed 's/- \(\[.\]\) \(.*\) due:\([[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}\)\(.*\)/- \1 \3 \2\4/' |
|
2024-03-03 14:19:00 +01:00
|
|
|
sort --reverse --stable |
|
|
|
|
sort --key 2.3,2.3r --stable |
|
2024-02-25 17:46:04 +01:00
|
|
|
{
|
|
|
|
if [ -n "$FILTER_TAG" ]; then
|
|
|
|
rg "#$FILTER_TAG\b"
|
|
|
|
else
|
|
|
|
cat
|
|
|
|
fi
|
2024-02-26 10:32:38 +01:00
|
|
|
} |
|
|
|
|
{
|
|
|
|
if [ -n "$ONLY_DONE" ]; then
|
2024-03-03 14:19:00 +01:00
|
|
|
rg "^- \[[xXcC]\] " | rg "(done|closed):$(date +%Y-%m-%d)"
|
2024-02-26 10:32:38 +01:00
|
|
|
else
|
|
|
|
cat
|
|
|
|
fi
|
2024-02-25 17:46:04 +01:00
|
|
|
}
|
|
|
|
elif [ -n "$ONLY_DAY" ]; then
|
|
|
|
rg --multiline --multiline-dotall "# $(date -d "$ONLY_DAY" +%Y-%m-%d)(.*?\n#[^#]|.*)"
|
|
|
|
else
|
|
|
|
cat
|
|
|
|
fi
|
|
|
|
} |
|
2024-03-23 14:34:05 +01:00
|
|
|
sed $'s|\[\([^]]\+\)\](\(https\\?://[^)]\+\))|\e[34;3;4m\e]8;;\\2\e\\\\\\1\x1b[0m\e]8;;\e\\\\|g' |
|
2024-02-25 17:46:04 +01:00
|
|
|
sed "s/^# \([[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}\)/ $C_HEADLINE \1 $C_RESET\n/" |
|
|
|
|
sed "s/^## \(.*\)\$/ $C_SUBHEADING\1$C_RESET\n/" |
|
|
|
|
sed "s/- \[ \] \([[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}\) /- [ ] $C_DEADLINE \1$C_RESET/" |
|
|
|
|
sed "s/- \[ \]/ $C_RED$C_RESET/g" |
|
|
|
|
sed "s/- \[[xX]\]/ $C_GREEN$C_RESET/g" |
|
2024-03-03 14:19:00 +01:00
|
|
|
sed "s/- \[[cC]\]/ $C_MAGENTA$C_RESET/g" |
|
2024-02-25 17:46:04 +01:00
|
|
|
sed "s/^\(\s*\)- /\1 $C_CYAN$C_RESET /g" |
|
|
|
|
sed "s/^\(\s*\)+ /\1 $C_CYAN$C_RESET /g" |
|
2024-03-07 09:17:29 +01:00
|
|
|
sed "s/\([[:digit:]]\{2\}:[[:digit:]]\{2\}\)/$C_TIME\1$C_RESET/g" |
|
2024-02-25 17:46:04 +01:00
|
|
|
sed "s/\(#\S\+\)/$C_CYAN\1$C_RESET/g" |
|
2024-03-03 14:19:00 +01:00
|
|
|
sed "s/\(\(due\|done\|prio\|closed\):\S\+\)/$C_MAGENTA\1$C_RESET/g" |
|
2024-03-23 14:34:05 +01:00
|
|
|
sed "s/^---\$/$C_MAGENTA$DASH$C_RESET/" |
|
|
|
|
{
|
|
|
|
input=$(cat)
|
|
|
|
max_length=$(tput lines) # Terminal height
|
|
|
|
# Measure the length of the input
|
|
|
|
input_length=$(echo "$input" | wc -l)
|
|
|
|
# Compare the input length with the maximum allowed length
|
|
|
|
if ((input_length > max_length)); then
|
2024-04-04 09:40:17 +02:00
|
|
|
# Input exceeds maximum length - use an external pager like 'less', but jump to the last line
|
|
|
|
echo "$input" | less -r +G
|
2024-03-23 14:34:05 +01:00
|
|
|
else
|
|
|
|
# Input does not exceed the maximum length - print directly
|
|
|
|
echo "$input"
|
|
|
|
fi
|
|
|
|
}
|