191 lines
5 KiB
Bash
Executable file
191 lines
5 KiB
Bash
Executable file
#!/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"
|
|
C_TIME="\x1b[36;3m"
|
|
|
|
year="$(date +%Y)"
|
|
|
|
# Just start helix if no arguments are given
|
|
if [ -z "${1+x}" ]; then
|
|
# 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")"
|
|
export GIT_SSH_COMMAND="ssh -o BatchMode=yes"
|
|
(git pull --rebase >/dev/null 2>&1 && git push >/dev/null 2>&1) || (echo "Sync failed" && exit 4) &
|
|
fi
|
|
# Exit
|
|
popd || exit 2
|
|
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"
|
|
printf " done | did\n"
|
|
printf " Show only todos that I did today\n"
|
|
printf " day | today\n"
|
|
printf " Show only todays entry\n"
|
|
printf " -1 | -2 | -3\n"
|
|
printf " Show only yesterday/the day before/etc\n"
|
|
printf " git GIT_COMMAND\n"
|
|
printf " Execute a git command in the note repository\n"
|
|
printf " dir\n"
|
|
printf " Print the note directory\n"
|
|
printf " bm | bookmark | bookmarks\n"
|
|
printf " List all bookmarks\n"
|
|
printf " link | links\n"
|
|
printf " List all links\n"
|
|
printf " log\n"
|
|
printf " Print the entire note file\n"
|
|
exit 1
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
ONLY_TODOS=
|
|
ONLY_DONE=
|
|
ONLY_DAY=
|
|
ONLY_LINKS=
|
|
ONLY_BOOKMARKS=
|
|
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
|
|
;;
|
|
done | did)
|
|
ONLY_TODOS=1
|
|
ONLY_DONE=1
|
|
shift # past argument
|
|
;;
|
|
day | today)
|
|
ONLY_DAY=today
|
|
shift # past argument
|
|
;;
|
|
-1 | -2 | -3)
|
|
ONLY_DAY="$1days"
|
|
shift
|
|
;;
|
|
git)
|
|
shift
|
|
run_git_command_by_args_and_exit "$@"
|
|
;;
|
|
dir)
|
|
shift
|
|
echo "$XDG_DATA_HOME/life.md"
|
|
exit
|
|
;;
|
|
bm | bookmark | bookmarks)
|
|
shift
|
|
ONLY_BOOKMARKS=1
|
|
;;
|
|
link | links)
|
|
shift
|
|
ONLY_LINKS=1
|
|
;;
|
|
log)
|
|
shift
|
|
;;
|
|
-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/' |
|
|
sort --reverse --stable |
|
|
sort --key 2.3,2.3r --stable |
|
|
{
|
|
if [ -n "$FILTER_TAG" ]; then
|
|
rg "#$FILTER_TAG\b"
|
|
else
|
|
cat
|
|
fi
|
|
} |
|
|
{
|
|
if [ -n "$ONLY_DONE" ]; then
|
|
rg "^- \[[xXcC]\] " | rg "(done|closed):$(date +%Y-%m-%d)"
|
|
else
|
|
cat
|
|
fi
|
|
}
|
|
elif [ -n "$ONLY_DAY" ]; then
|
|
rg --multiline --multiline-dotall "# $(date -d "$ONLY_DAY" +%Y-%m-%d)(.*?\n#[^#]|.*)"
|
|
elif [ -n "$ONLY_LINKS" ]; then
|
|
rg '\[[^]]+\]\([^)]+\)' --only-matching | sed 's/^/- /' | sort
|
|
elif [ -n "$ONLY_BOOKMARKS" ]; then
|
|
rg '[+-] bookmark: \[[^]]+\]\([^)]+\)' --only-matching | sed 's/+ bookmark: /+ /' | sort
|
|
else
|
|
cat
|
|
fi
|
|
} |
|
|
sed $'s|\[\([^]]\+\)\](\(https\\?://[^)]\+\))|\e[34;3;4m\e]8;;\\2\e\\\\\\1\x1b[0m\e]8;;\e\\\\|g' |
|
|
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" |
|
|
sed "s/- \[[cC]\]/ $C_MAGENTA$C_RESET/g" |
|
|
sed "s/^\(\s*\)- /\1 $C_CYAN$C_RESET /g" |
|
|
sed "s/^\(\s*\)+ /\1 $C_CYAN$C_RESET /g" |
|
|
sed "s/\([[:digit:]]\{2\}:[[:digit:]]\{2\}\)/$C_TIME\1$C_RESET/g" |
|
|
sed "s/\(#\S\+\)/$C_CYAN\1$C_RESET/g" |
|
|
sed "s/\(\(due\|done\|prio\|closed\):\S\+\)/$C_MAGENTA\1$C_RESET/g" |
|
|
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
|
|
# Input exceeds maximum length - use an external pager like 'less', but jump to the last line
|
|
echo "$input" | less -r +G
|
|
else
|
|
# Input does not exceed the maximum length - print directly
|
|
echo "$input"
|
|
fi
|
|
}
|