Rebases from master

This commit is contained in:
Alicia Sykes 2022-08-23 12:47:56 +01:00
parent a2eabae2a3
commit af42de60d1
5 changed files with 493 additions and 0 deletions

View File

@ -0,0 +1,71 @@
#!/bin/sh -
# Quick utility to aid in keeping clutter down in the $HOME directory
# Lists statistics about number of files, auto-cleans certain files,
# and prompts user wheather they'd like to each remaining dotfile in turn
set -u
readonly HOME=${HOME:-$(getent passwd "$(id -un)" | cut -d : -f 6)}
inlist() {
for e in $2; do
case "$1" in ($e)
return
esac
done
false
}
die() {
retval=$(($1)); shift
{ printf "$@"; echo; } >&2
exit $retval
}
prompt_delete() {
printf '\nDelete %s? [Y/n] ' "$1"
read -r a
test -t 0 || printf '\033[1;32m%s\033[0m\n' "$a"
case "$a" in
(''|Y*|y*) rm -rv "$1" ;;
(N*|n*) ;;
(*) prompt_delete "$1" ;;
esac
}
# gather and show statistics:
n_nondots=$(find "$HOME" -maxdepth 1 -mindepth 1 -name '[^.]*' | wc -l)
n_dots=$(find "$HOME" -maxdepth 1 -mindepth 1 -name '.*' | wc -l)
n_all=$((n_nondots + n_dots))
cat <<- EOF
total: $n_all
normal files: $n_nondots
$(printf '\033[1m')dotfiles: $n_dots$(printf '\033[0m')
EOF
# List dotfiles:
cd "$HOME" || die 1 'Could not cd into home directory (%s)' "$HOME"
if ! ls -1d --color=auto .[!.]*; then
die 1 'Could not list files in home directory (%s)' "$HOME"
fi
# Automatic decisions for specific files/directories:
keeplist='.anthy .local .pam_environment .pki .ssh'
deletelist='.ansible .ansible_galaxy .mozilla .w3m .*_history'
# delete:
for d in .*; do
case "$d" in (.|..) continue ;; esac
if inlist "$d" "$keeplist"; then
# Do not delete this file
continue
elif inlist "$d" "$deletelist"; then
# Delete this file without asking
echo y | prompt_delete "$d"
continue
else
# Ask the user if should delete or not
prompt_delete "$d"
fi
done

63
utils/print-color-map.sh Normal file
View File

@ -0,0 +1,63 @@
#!/usr/bin/env bash
trap 'tput sgr0' exit # Clean up even if user hits ^C
function setfg () {
printf '\e[38;5;%dm' $1
}
function setbg () {
printf '\e[48;5;%dm' $1
}
function showcolors() {
# Given an integer, display that many colors
for ((i=0; i<$1; i++))
do
printf '%4d ' $i
setbg $i
tput el
tput sgr0
echo
done
tput sgr0 el
}
### Main starts here
# First, test if terminal supports OSC 4 at all.
printf '\e]4;%d;?\a' 0
read -d $'\a' -s -t 0.1 </dev/tty
if [ -z "$REPLY" ]
then
# OSC 4 not supported, so we'll fall back to terminfo
max=$(tput colors)
else
# OSC 4 is supported, so use it for a binary search
min=0
max=256
while [[ $((min+1)) -lt $max ]]
do
i=$(( (min+max)/2 ))
printf '\e]4;%d;?\a' $i
read -d $'\a' -s -t 0.1 </dev/tty
if [ -z "$REPLY" ]; then
max=$i
else
min=$i
fi
done
fi
# If -v is given, show all the colors
case ${1-none} in none)
echo $max;;-v)
showcolors $max;;*)
if [[ "$1" -gt 0 ]]; then
showcolors $1
else
echo $max
fi
;;
esac | less --raw-control-chars --QUIT-AT-EOF --no-init

205
utils/qr-code.sh Normal file
View File

@ -0,0 +1,205 @@
#!/usr/bin/env bash
# Based on similar script by Linyos Torovoltos and Alex Epstein
multiline="0" # flag that indicates multiline option
fileoutput="0" # flag indicating the -f option
## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient()
{
if command -v curl &>/dev/null; then
configuredClient="curl"
elif command -v wget &>/dev/null; then
configuredClient="wget"
elif command -v http &>/dev/null; then
configuredClient="httpie"
elif command -v fetch &>/dev/null; then
configuredClient="fetch"
else
echo "Error: This tool requires either curl, wget, httpie or fetch to be installed." >&2
return 1
fi
}
## Allows to call the users configured client without if statements everywhere
httpGet()
{
case "$configuredClient" in
curl) curl -A curl -s "$@" ;;
wget) wget -qO- "$@" ;;
httpie) http -b GET "$@" ;;
fetch) fetch -q "$@" ;;
esac
}
getConfiguredPython()
{
if command -v python3 &>/dev/null; then
configuredPython="python3"
elif command -v python2 &>/dev/null; then
configuredPython="python2"
elif command -v python &>/dev/null; then
configuredPython="python"
else
echo "Error: This tool requires python to be installed."
return 1
fi
}
if [[ $(uname) != "Darwin" ]]; then
python()
{
case "$configuredPython" in
python3) python3 "$@" ;;
python2) python2 "$@" ;;
python) python "$@" ;;
esac
}
fi
makeqr()
{
input=$(echo "$input" | sed s/" "/%20/g ) ## replace all spaces in the sentence with HTML-encoded space %20
httpGet qrenco.de/"$input" ## get a response for the qrcode
}
# redirects the image obtained from the goqr api into a png file
makeQRFile() {
input=$(echo "$input" | sed -e s/" "/%20/g -e s/'\\n'/%0A/g ) ##same as in the makeqr function
addFileExt
httpGet "api.qrserver.com/v1/create-qr-code/?size=150x150&data=$input" > "$fileName"
}
addFileExt() {
if ! echo "$fileName" | grep -E -q ".*\.png$|.*\.PNG$"
then
fileName="$fileName.png"
fi
}
makeMultiLineQr()
{
if [[ ${configuredClient} != "curl" ]]; then ## prevent usage without curl it is unreliable
echo "Multiline currently only supports curl!"
return 1
else
input=$(echo "$input" | sed -e s/" "/%20/g -e s/'\\n'/%0A/g ) ##same as in the makeqr function
printf "%s" "$input" | curl -F-=\<- qrenco.de
fi
}
# Function to get the json response from POST request
decodeQR() {
local qrFile="$1"
if ! echo "$fileName" | grep -E -q ".*\.png$|.*\.PNG$|.*\.gif$|.*\.jpg$|.*\.jpeg$|.*\.GIF$|.*\.JPG$|.*\.JPEG$"
then
exit 1
fi
# only uses curl
# Cannot use wget because it does not support multipart/form-data (as per the man page)]
case "$configuredClient" in
curl) JSONresponse=$(curl -s -F "file=@$qrFile" http://api.qrserver.com/v1/read-qr-code/) || exit 1;;
wget) echo "Error:-Not supported with wget" >&2 && exit 1;;
httpie) JSONresponse=$(http -b --form POST http://api.qrserver.com/v1/read-qr-code/ file@"$qrFile") || exit 1;;
fetch) echo "Error:-Not supported with wget" >&2 && exit 1;;
esac
error="$(echo "$JSONresponse" | python -c "from __future__ import print_function; import sys, json; print(json.load(sys.stdin)[0]['symbol'][0]['error'])")"
if [[ "$error" == "None" ]]
then
data="$(echo "$JSONresponse" | python -c "from __future__ import print_function; import sys, json; print(json.load(sys.stdin)[0]['symbol'][0]['data'])")"
else
echo "Error:-$error" >&2 && exit 1
fi
}
checkInternet()
{
httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request
}
usage()
{
cat <<EOF
Qrify
Description: Converts strings or URLs into a QR code.
Usage: qrify [stringtoturnintoqrcode]
-m Enable multiline support (feature not working yet)
-h Show the help
-v Get the tool version
-f Store the QR code as a PNG file
-d Decode the QR code from a PNG/GIF/JP(E)G file
Examples:
qrify this is a test string
qrify -m two\\\\nlines
qrify github.com (no http:// or https://)
qrify -f fileoutputName google.com
qrify -d fileName.png
Please pay attention:
This script needs access to an external API.
Do not use it to encode sensitive data.
EOF
}
getConfiguredClient || exit 1
while getopts "d:f:m:hvu*:" option
do
case "${option}" in
h) usage && exit 0 ;;
m) multiline="1" && echo "Error this is not a supported feature yet" && exit 1 ;;
f)
fileName=$OPTARG
#file name is the first argument of the option -f
fileoutput="1";;
d)
fileName=$OPTARG
decode="1";;
esac
done
if [[ $# == "0" ]]; then
usage
exit 0
elif [[ $# == "1" ]];then
if [[ $1 == "help" || $1 == ":help" ]]; then
usage
exit 0
else
getConfiguredPython || exit 1
checkInternet || exit 1
input=$(printf '%s ' "$@")
makeqr || exit 1
exit 0
fi
else
getConfiguredPython || exit 1
checkInternet || exit 1
if [[ $fileoutput == "1" ]]
then
input=$(printf '%s ' "${@:3}") # first arg is -f, second is the file name, third onwards is the rest of the argument
# will have to be changed when implementing multiline QR code
makeQRFile || exit 1
exit 0
elif [[ $decode == "1" ]]
then
( decodeQR "$fileName" && echo "$data" ) || exit 1
exit 0
elif [[ $multiline == "0" ]]; then
input=$(printf '%s ' "$@")
makeqr || exit 1
exit 0
else
input=$(printf '%s ' "${@:2}")
makeMultiLineQr || exit 1 ## if multiline that means a flag existed so start from the second argument
exit 0
fi
fi

150
utils/weather.sh Normal file
View File

@ -0,0 +1,150 @@
#!/usr/bin/env bash
# Author: Alexander Epstein https://github.com/alexanderepstein
currentVersion="1.23.0" #This version variable should not have a v but should contain all other characters ex Github release tag is v1.2.4 currentVersion is 1.2.4
LANG="${LANG:-en}"
locale=$(echo "$LANG" | cut -c1-2)
unset configuredClient
if [[ $(echo "$locale" | grep -Eo "[a-z A-Z]*" | wc -c) != 3 ]]; then locale="en"; fi
## This function determines which http get tool the system has installed and returns an error if there isnt one
getConfiguredClient()
{
if command -v curl &>/dev/null; then
configuredClient="curl"
elif command -v wget &>/dev/null; then
configuredClient="wget"
elif command -v http &>/dev/null; then
configuredClient="httpie"
elif command -v fetch &>/dev/null; then
configuredClient="fetch"
else
echo "Error: This tool requires either curl, wget, httpie or fetch to be installed\." >&2
return 1
fi
}
## Allows to call the users configured client without if statements everywhere
httpGet()
{
case "$configuredClient" in
curl) curl -A curl -s "$@" ;;
wget) wget -qO- "$@" ;;
httpie) http -b GET "$@" ;;
fetch) fetch -q "$@" ;;
esac
}
getIPWeather()
{
country=$(httpGet ipinfo.io/country) > /dev/null ## grab the country
if [[ $country == "US" ]]; then ## if were in the us id rather not use longitude and latitude so the output is nicer
city=$(httpGet ipinfo.io/city) > /dev/null
region=$(httpGet ipinfo.io/region) > /dev/null
if [[ $(echo "$region" | wc -w) == 2 ]];then
region=$(echo "$region" | grep -Eo "[A-Z]*" | tr -d "[:space:]")
fi
httpGet $locale.wttr.in/"$city","$region""$1"
else ## otherwise we are going to use longitude and latitude
location=$(httpGet ipinfo.io/loc) > /dev/null
httpGet $locale.wttr.in/"$location""$1"
fi
}
getLocationWeather()
{
args=$(echo "$@" | tr " " + )
httpGet $locale.wttr.in/"${args}"
}
checkInternet()
{
httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request
}
usage()
{
cat <<EOF
Weather
Description: Provides a 3 day forecast on your current location or a specified location.
With no flags Weather will default to your current location.
Usage: weather or weather [flag] or weather [country] or weather [city] [state]
weather [i][M] get weather in imperial units, optional M means windspeed in m/s
weather [m][M] get weather in metric units, optional M means windspeed in m/s
weather [Moon] grabs the phase of the moon
-h Show the help
-v Get the tool version
Examples:
weather
weather Paris m
weather Tokyo
weather Moon
weather mM
EOF
}
getConfiguredClient || exit 1
while getopts "uvh" opt; do
case "$opt" in
\?) echo "Invalid option: -$OPTARG" >&2
exit 1
;;
h) usage
exit 0
;;
v) echo "Version $currentVersion"
exit 0
;;
u) checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here
update || exit 1
exit 0
;;
:) echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [[ $# == "0" ]]; then
checkInternet || exit 1
getIPWeather || exit 1
exit 0
elif [[ $1 == "help" || $1 == ":help" ]]; then
usage
exit 0
elif [[ $1 == "update" ]]; then
checkInternet || exit 1
update || exit 1
exit 0
fi
checkInternet || exit 1
if [[ $1 == "m" ]]; then
getIPWeather "?m" || exit 1
elif [[ "${@: -1}" == "m" ]];then
args=$( echo "${@:1:(($# - 1))}" ?m | sed s/" "//g)
getLocationWeather "$args" || exit 1
elif [[ $1 == "M" ]]; then
getIPWeather "?M" || exit 1
elif [[ "${@: -1}" == "M" ]];then
args=$( echo "${@:1:(($# - 1))}" ?M | sed s/" "//g)
getLocationWeather "$args" || exit 1
elif [[ $1 == "mM" || $1 == "Mm" ]]; then
getIPWeather "?m?M" || exit 1
elif [[ "${@: -1}" == "mM" || "${@:-1}" == "Mm" ]];then
args=$( echo "${@:1:(($# - 1))}" ?m?M | sed s/" "//g)
getLocationWeather "$args" || exit 1
elif [[ $1 == "iM" || $1 == "Mi" ]]; then
getIPWeather "?u?M" || exit 1
elif [[ "${@: -1}" == "iM" || "${@:-1}" == "Mi" ]];then
args=$( echo "${@:1:(($# - 1))}" ?u?M | sed s/" "//g)
getLocationWeather "$args" || exit 1
elif [[ $1 == "i" ]]; then
getIPWeather "?u" || exit 1
elif [[ "${@: -1}" == "i" ]];then
args=$( echo "${@:1:(($# - 1))}" ?u | sed s/" "//g)
getLocationWeather "$args" || exit 1
else
getLocationWeather "$@" || exit 1
fi

View File

@ -65,7 +65,11 @@ print_node_versions () {
versions=''
get_version () {
if hash $1 2> /dev/null || command -v $1 >/dev/null; then
<<<<<<< Updated upstream
versions="$versions\e[36m\e[1m $2: \033[0m$($1 --version)\n\033[0m"
=======
versions="$versions\e[36m\e[1m $2: \033[0m$($1 --version)\n"
>>>>>>> Stashed changes
else
versions="$versions\e[33m\e[1m $2: \033[0m\033[3m Not installed\n\033[0m"
fi