-# Convert brains file path to url and vice versa. It copies the result to clipboard
-# usage: brains [URL_OR_PATH]
-brains() {
- local url path input repo_dir err path
- repo_dir=/a/f/brains
- err=false
- if ! type -p xclip &>/dev/null; then
- echo "missing dependency for brains, please do: sudo apt install xclip" >&2
- err=true
- fi
- if ! type -p pee &>/dev/null; then
- echo "missing dependency for brains, please do: sudo apt install moreutils" >&2
- err=true
- fi
- if $err; then
- return 1
- fi
- if [[ $1 ]]; then
- input="$*"
- else
- read -r -p "enter path or url"$'\n' input
- fi
- case $input in
- http*)
- path="$repo_dir/${input##https://brains.fsf.org/wiki/}"
- if [[ $path == */ ]]; then
- path=${path%/}.mdwn
- fi
- printf "%s\n" "$path" |& pee "xclip -r -selection clipboard" cat
- ;;
- *)
- path=$(realpath -s "$input")
- url="https://brains.fsf.org/wiki/${path#$repo_dir/}"
- url="${url%.mdwn}/"
- echo "$url" |& pee "xclip -r -selection clipboard" cat
- ;;
- esac
-}
-