Skip to content

Instantly share code, notes, and snippets.

@Grynn
Last active November 1, 2024 10:10
Show Gist options
  • Select an option

  • Save Grynn/189537dbc21049cbf6d6165b7944d9a5 to your computer and use it in GitHub Desktop.

Select an option

Save Grynn/189537dbc21049cbf6d6165b7944d9a5 to your computer and use it in GitHub Desktop.
VSCode remote quick launch
#Add to ~/.profile
function code() {
if ((( $# == 0 )) || [[ "$1" == "." ]]); then
echo "vscode://vscode-remote/ssh-remote+${USER}@gray:22${PWD}"
else
# convert relative path to absolute path
if [[ "$1" == /* ]]; then
echo "vscode://vscode-remote/ssh-remote+${USER}@gray:22$1"
else
echo "vscode://vscode-remote/ssh-remote+${USER}@gray:22${PWD}/$1"
fi
fi
}
function brewinfo() {
local tmp=$(gmktemp -t XXXXX.csv --tmpdir)
HOMEBREW_NO_ANALYTICS=1 brew info --json=v2 $@ | jq -r '.formulae[], .casks[] | [ (.name|join(","))? // .name , .desc, .homepage ] | @csv' | tee $tmp
echo $tmp
}
function ga() { curl -A 'Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36' -sL "http://${1}" | perl -wlne 'm/UA-[\d\-]+/ && print $&;' | sort -u ; }
function lineinfile(){
if [[ $# != 3 ]];then
local THIS_FUNC_NAME="${funcstack[1]-}${FUNCNAME[0]-}"
echo "$THIS_FUNC_NAME - 3 arguments are expected. given $#. args=[$@]" >&2
echo "usage: $THIS_FUNC_NAME PATTERN LINE FILE" >&2
return 1
fi
local PATTERN="$1"
local LINE="$2"
local FILE="$3"
if grep -E -q "${PATTERN}" "${FILE}" ;then
## solution 1: works with GNU sed well, but not works with BSD sed.
# sed -E -i '' "/${PATTERN//\//\\/}/c${LINE}" "${FILE}"
## solution 2: works with both (GNU|BSD) sed, but get useless *.bak file generated.
# sed -E -i.bak "/${PATTERN//\//\\/}/c\\"$'\n'"${LINE}" "${FILE}"
## solution 3: give up to use sed, using perl instead.
PATTERN="${PATTERN}" LINE="${LINE}" perl -i -nle 'if(/$ENV{"PATTERN"}/){print $ENV{"LINE"}}else{print}' "${FILE}"
else
echo "$LINE" >> "$FILE"
fi
}
function curl_time() {
curl -so /dev/null -w "\
namelookup: %{time_namelookup}s\n\
connect: %{time_connect}s\n\
appconnect: %{time_appconnect}s\n\
pretransfer: %{time_pretransfer}s\n\
redirect: %{time_redirect}s\n\
starttransfer: %{time_starttransfer}s\n\
-------------------------\n\
total: %{time_total}s\n" "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment