Created
October 16, 2018 20:49
-
-
Save kamontat/dd8cd62a9ad55fc1271dccc45f14dac3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# shellcheck disable=SC1000 | |
# generate by v3.0.2 | |
# link (https://github.com/Template-generator/script-genrating/tree/v3.0.2) | |
# set -x #DEBUG - Display commands and their arguments as they are executed. | |
# set -v #VERBOSE - Display shell input lines as they are read. | |
# set -n #EVALUATE - Check syntax of the script but don't execute. | |
#/ ----------------------------------- | |
#/ Description: Create clickable link, from stdin or option | |
#/ | |
#/ Option: -s | --seperator <sep> | |
#/ > Input custom seperator (default is |) | |
#/ -n | --no-newline | |
#/ > The output of the command will no have any newline | |
#/ -f | --as-file | |
#/ > This will treat the input link as file and auto append file:// | |
#/ -r | --reverse | |
#/ > Reverse the input stack (default must be "text|link") | |
#/ -M | --message <message> | |
#/ > Instead of read from stdin | |
#/ -T | --text | |
#/ > Raw input text, must be use with --link | |
#/ -L | --link | |
#/ > Raw input link, must be use with --text | |
#/ -h | -? | --help | |
#/ > Show this message | |
#/ -v | -V | --version | |
#/ > Show command version | |
#/ --changelog | |
#/ > Show changelog | |
#/ ----------------------------------- | |
#/ Create by: Kamontat Chantrachirathunrong <kamontat.c@hotmail.com> | |
#/ Since: 16/10/2018 | |
#/ ----------------------------------- | |
#/ Error code 1 -- unknown error | |
#/ ----------------------------------- | |
#// Version: 1.0.0 -- Add clickable script to making clickable link in support terminal | |
#// 1.1.0 -- Add more option and help command | |
export NAME="clink" | |
export VERSION="1.1.0" | |
export DEFAULT_SEP="|" | |
export DEFAULT_NEWLINE="\n" | |
export DEFAULT_URL_AS_FILE="" | |
export DEFAULT_IS_MESSAGE_IN_PARAM=false | |
export DEFAULT_REVERSE=false | |
export LINK="" | |
export TEXT="" | |
help() { | |
grep "^#/ " "$NAME" | sed 's/#\/ //g' | |
} | |
version() { | |
echo "$NAME ($VERSION)" | |
} | |
versions() { | |
grep "^#// " "$NAME" | sed 's/#\/\/ //g' | |
} | |
# @helper | |
throw() { | |
printf '%s\n' "$1" >&2 && is_integer "$2" && exit "$2" | |
return 0 | |
} | |
# @helper | |
throw_if_empty() { | |
local text="$1" | |
test -z "$text" && throw "$2" "$3" | |
return 0 | |
} | |
# @option | |
require_argument() { | |
throw_if_empty "$LONG_OPTVAL" "'$LONG_OPTARG' require argument" 9 | |
} | |
# @option | |
no_argument() { | |
[[ -n $LONG_OPTVAL ]] && ! [[ $LONG_OPTVAL =~ "-" ]] && throw "$LONG_OPTARG don't have argument" 9 | |
OPTIND=$((OPTIND - 1)) | |
} | |
# @syscall | |
set_key_value_long_option() { | |
if [[ $OPTARG =~ "=" ]]; then | |
LONG_OPTVAL="${OPTARG#*=}" | |
LONG_OPTARG="${OPTARG%%=$LONG_OPTVAL}" | |
else | |
LONG_OPTARG="$OPTARG" | |
LONG_OPTVAL="$1" | |
OPTIND=$((OPTIND + 1)) | |
fi | |
} | |
load_option() { | |
while getopts 'Hh?VvnsfrMLT-:' flag; do | |
case "${flag}" in | |
H) help && exit 0 ;; | |
h) help && exit 0 ;; | |
\\?) help && exit 0 ;; | |
V) version && exit 0 ;; | |
v) version && exit 0 ;; | |
n) DEFAULT_NEWLINE="" ;; | |
s) DEFAULT_SEP="$OPTARG" ;; | |
f) DEFAULT_URL_AS_FILE="file://" ;; | |
r) DEFAULT_REVERSE=true ;; | |
M) | |
DEFAULT_IS_MESSAGE_IN_PARAM=true && | |
message="$OPTARG" | |
;; | |
L) | |
DEFAULT_IS_MESSAGE_IN_PARAM=true && | |
LINK="$OPTARG" | |
;; | |
T) | |
DEFAULT_IS_MESSAGE_IN_PARAM=true && | |
TEXT="$OPTARG" | |
;; | |
-) | |
export LONG_OPTARG | |
export LONG_OPTVAL | |
NEXT="${!OPTIND}" | |
set_key_value_long_option "$NEXT" | |
case "${OPTARG}" in | |
help) | |
no_argument | |
help | |
exit 0 | |
;; | |
version) | |
no_argument | |
version | |
exit 0 | |
;; | |
changelog) | |
no_argument | |
versions | |
exit 0 | |
;; | |
no-newline) | |
no_argument | |
DEFAULT_NEWLINE="" | |
;; | |
seperator) | |
require_argument | |
DEFAULT_SEP="$LONG_OPTVAL" | |
;; | |
as-file) | |
no_argument | |
DEFAULT_URL_AS_FILE="file://" | |
;; | |
message) | |
require_argument | |
DEFAULT_IS_MESSAGE_IN_PARAM=true | |
message="$LONG_OPTVAL" | |
;; | |
reverse) | |
no_argument | |
DEFAULT_REVERSE=true | |
;; | |
link) | |
require_argument | |
DEFAULT_IS_MESSAGE_IN_PARAM=true | |
LINK="$LONG_OPTVAL" | |
;; | |
text) | |
require_argument | |
DEFAULT_IS_MESSAGE_IN_PARAM=true | |
TEXT="$LONG_OPTVAL" | |
;; | |
*) | |
# because optspec is assigned by 'getopts' command | |
# shellcheck disable=SC2154 | |
if [ "$OPTERR" == 1 ] && [ "${optspec:0:1}" != ":" ]; then | |
echo "Unexpected option '$LONG_OPTARG', run --help for more information" >&2 | |
exit 9 | |
fi | |
;; | |
esac | |
;; | |
?) | |
echo "Unexpected option, run --help for more information" >&2 | |
exit 10 | |
;; | |
*) | |
echo "Unexpected option $flag, run --help for more information" >&2 | |
exit 10 | |
;; | |
esac | |
done | |
} | |
load_option "$@" | |
! $DEFAULT_IS_MESSAGE_IN_PARAM && read -r message | |
IFS="$DEFAULT_SEP" read -ra array <<<"$message" | |
if $DEFAULT_REVERSE; then | |
test -z "$LINK" && LINK="${array[0]}" | |
test -z "$TEXT" && TEXT="${array[1]}" | |
else | |
test -z "$LINK" && LINK="${array[1]}" | |
test -z "$TEXT" && TEXT="${array[0]}" | |
fi | |
printf "\e]8;;%s\a%s\e]8;;\a$DEFAULT_NEWLINE" "${DEFAULT_URL_AS_FILE}$LINK" "$TEXT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run the script online:
bash <(curl -s 'https://gist.githubusercontent.com/kamontat/dd8cd62a9ad55fc1271dccc45f14dac3/raw/7ed79a4139960758a6f5c1966893b66c4c37fc68/clink')
For
version 1.1.0