dotfiles/config/todo/actions/url (view raw)
| 1 | #!/usr/bin/env bash |
| 2 | action=$1 |
| 3 | shift |
| 4 | |
| 5 | if [[ $action == "usage" ]]; then |
| 6 | echo "url [task_number] - opens link in the task" |
| 7 | exit 0 |
| 8 | fi |
| 9 | |
| 10 | item=$1 |
| 11 | if [[ $item == "" ]]; then |
| 12 | TODOTXT_VERBOSE=0 $TODO_FULL_SH ls @url |
| 13 | exit 0 |
| 14 | fi |
| 15 | |
| 16 | if [[ ! "$item" =~ ^[0-9]+$ ]]; then |
| 17 | echo "provided [task_number] is invalid: $item" |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | URL_REGEX="\b(?:https?:\/\/)?(?:[\w-]+\.)+[a-z]{2,}(?:\/[\w\-./?%&=:#@+]*)?" |
| 22 | URL=$(sed "$item!d" "$TODO_FILE" | grep -Po "$URL_REGEX") |
| 23 | |
| 24 | if [[ -z "$URL" ]]; then |
| 25 | echo "No URL found in task $item" |
| 26 | exit 1 |
| 27 | fi |
| 28 | |
| 29 | if [[ ! "$URL" =~ ^(?!https?:\/\/) ]]; then |
| 30 | URL="https://$URL" |
| 31 | fi |
| 32 | |
| 33 | echo "$URL" |
| 34 | xdg-open "$URL" &>/dev/null |