Add freeEbook and imgcat

This commit is contained in:
Jonathan Hodgson 2017-08-15 15:29:09 +01:00
parent 51d05f7f27
commit cc93470ef8
2 changed files with 285 additions and 91 deletions

8
bin/freeEbook Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/bash
markup=$(curl https://www.packtpub.com/packt/offers/free-learning 2> /dev/null | sed 's/"\/\//"https:\/\//g')
#img=$(echo "$markup" | sed -n "/dotd-main-book-image/,$ p" | head | sed -n "/<img/ p")
#img=$(echo "$markup" | sed -n "/dotd-main-book-image/,$ p" | head | sed -n "/<img/ p" | sed -nr "s/.*<img.*?src=[\"|'](.*?)[\"|'].*/\3/p")
img=$(echo "$markup" | sed -n "/dotd-main-book-image/,$ p" | head | sed -n "/<img/ p" | sed -nr 's/.*?<img src="([^"]*)".*img.*/\1/p')
curl "$img" > /tmp/freeBook 2> /dev/null
imgcat /tmp/freeBook
rm /tmp/freeBook

View file

@ -1,103 +1,289 @@
#!/bin/bash #!/bin/bash
VERSION="1.2" # Version number of wiw
# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; W3MIMGDISPLAY="/usr/lib/w3m/w3mimgdisplay" # Were w3mimgdisplay is located
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It DEL="-" # The default delimiter between items
# only accepts ESC backslash for ST. DFW="7" # Default font width
function print_osc() { DFH="16" # Default font height
if [[ $TERM == screen* ]] ; then
printf "\033Ptmux;\033\033]"
else
printf "\033]"
fi
}
# More of the tmux workaround described above. CLEAR="0" # If this is set to 1 it clears the teminal before printing the image
function print_st() { FIT="1" # If this is set to 1 then wiw will limit the size of the image
if [[ $TERM == screen* ]] ; then ECHO="0" # If this is set to 1 then wiw will output the command that should be piped into w3mimgdisplay, instead of printing the image
printf "\a\033\\" CENTER="0" # If this is set to 1 then wiw will center the image between x,y and mw,mh
else
printf "\a"
fi
}
# print_image filename inline base64contents # Print help
# filename: Filename to convey to client if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
# inline: 0 or 1 echo "wiw: w3mimg Wrapper Version: $VERSION"
# base64contents: Base64-encoded contents echo "USAGE"
function print_image() { echo " wiw [-h --help] Display this"
print_osc echo " wiw --size IMAGE Print the width and height of the image, returned as 'WIDTH HEIGHT'"
printf '1337;File=' echo " wiw [ARGS] IMAGE Display an image on the terminal"
if [[ -n "$1" ]]; then echo " wiw --echo [ARGS] IMAGE Print out the command to produce the desired image"
printf 'name='`echo -n "$1" | base64`";" echo " wiw --direct CMD Pass a command directly into w3mimgdisplay"
fi echo "ARGS"
if $(base64 --version 2>&1 | grep GNU > /dev/null) echo " --clear Clear the terminal before printing the image"
then echo " --no-fit Don't force the image to fit the terminal"
BASE64ARG=-d echo " --center Tells wiw to center the image in the given space"
else echo " -f width${DEL}height Set the width and height of the font, used to determine max term size and items specified in U"
BASE64ARG=-D echo " (default width = ${DFW}, default height = ${DFH})"
fi echo " -g x${DEL}y${DEL}width${DEL}height Set the x, y, width, and height of the image"
echo -n "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}' echo " (default x = 0, default y = 0, default w = term width, default h = term height)"
printf ";inline=$2" echo " You can specify these as a #U (were # is any number) to use the unit of the font"
printf ";width=50" echo " width and height can be set to R, this will preserve image ratio"
printf ":" echo " -s x${DEL}y${DEL}width${DEL}height Set the x, y, width, and height of the source image"
echo -n "$3" echo " (default x = 0, default y = 0, default w = image width, default h = image height)"
print_st echo " You can specify these as a #U (were # is any number) to use the unit of the font"
printf '\n' echo " This command is a bit weird, I would reccomend not using it unless really needed"
} echo " -m width${DEL}height Set the max width and height the image can consume"
echo " (default w = full term width, default h = full term height)"
function error() { echo " You can specify these as a #U (were # is any number) to use the unit of the font"
echo "ERROR: $*" 1>&2 echo " You can specify these as a #N (were # is any number) to subtract from the max term size"
} echo " -c width${DEL}height Set the cursor position for after the image is printed"
echo " (default x = 0, default y = line after image)"
function show_help() { echo " x and y can be set to LAST, to be placed at the last element"
echo "Usage: imgcat filename ..." 1>& 2 echo " -d delimiter Set the delimiter between items in the -f, -g, and -s args (MUST BE A SINGLE CHARACTER)"
echo " or: cat filename | imgcat" 1>& 2 echo " (default delimiter = '${DEL}')"
} exit
# Print the size of the image
## Main elif [[ "$1" == "--size" ]]; then
echo -e "5;$2" | $W3MIMGDISPLAY
if [ -t 0 ]; then exit
has_stdin=f # Pass a command directly into w3mimgdisplay
else elif [[ "$1" == "--direct" ]]; then
has_stdin=t echo -e "$2" | $W3MIMGDISPLAY
exit
fi fi
# Show help if no arguments and no stdin.
if [ $has_stdin = f -a $# -eq 0 ]; then
show_help
exit
fi
# Look for command line flags.
while [ $# -gt 0 ]; do # Cycle through args
case "$1" in while [ $# != 1 ]; do
-h|--h|--help) if [[ "$1" == "--clear" ]]; then
show_help CLEAR="1"
exit elif [[ "$1" == "--no-fit" ]]; then
;; FIT="0"
-*) elif [[ "$1" == "--echo" ]]; then
error "Unknown option flag: $1" ECHO="1"
show_help elif [[ "$1" == "--center" ]]; then
exit 1 CENTER="1"
;; elif [[ "$1" == "-f" ]]; then
*) FW=`echo "$2" | cut -d "$DEL" -f1`
if [ -r "$1" ] ; then FH=`echo "$2" | cut -d "$DEL" -f2`
print_image "$1" 1 "$(base64 < "$1")" shift
else elif [[ "$1" == "-g" ]]; then
error "imgcat: $1: No such file or directory" X=`echo "$2" | cut -d "$DEL" -f1`
exit 2 Y=`echo "$2" | cut -d "$DEL" -f2`
fi W=`echo "$2" | cut -d "$DEL" -f3`
;; H=`echo "$2" | cut -d "$DEL" -f4`
esac shift
shift elif [[ "$1" == "-s" ]]; then
SX=`echo "$2" | cut -d "$DEL" -f1`
SY=`echo "$2" | cut -d "$DEL" -f2`
SW=`echo "$2" | cut -d "$DEL" -f3`
SH=`echo "$2" | cut -d "$DEL" -f4`
shift
elif [[ "$1" == "-c" ]]; then
CX=`echo "$2" | cut -d "$DEL" -f1`
CY=`echo "$2" | cut -d "$DEL" -f2`
shift
elif [[ "$1" == "-m" ]]; then
MW=`echo "$2" | cut -d "$DEL" -f1`
MH=`echo "$2" | cut -d "$DEL" -f2`
shift
elif [[ "$1" == "-d" ]]; then
DEL="$2"
shift
fi
shift
done done
# Read and print stdin # Set the file to the only remaining param
if [ $has_stdin = t ]; then FN="$1"
print_image "" 1 "$(cat | base64)" shift
# Test to make sure that the file exists
test -e "$FN" || exit
### SET DEFAULTS IF THEY WERE NOT SET BY ARGS
# If an font width or height was not set then use the default
if [[ "$FW" == "" ]]; then
FW="$DFW"
fi
if [[ "$FH" == "" ]]; then
FH="$DFH"
fi fi
echo # If an X or Y was not set then set them to 0
echo if [[ "$X" == "" ]]; then
X="0";
fi
if [[ "$Y" == "" ]]; then
Y="0";
fi
exit 0 # If an x or y is in U
if [[ "$X" == *"U" ]]; then
X=`echo "$X" | cut -d "U" -f1`
X=$(($X * $FW))
fi
if [[ "$Y" == *"U" ]]; then
Y=`echo "$Y" | cut -d "U" -f1`
Y=$(($Y * $FH))
fi
# Full image width and height
read IW IH <<< `echo -e "5;$FN" | $W3MIMGDISPLAY`
# If no width or height were specified use the temp
if [[ "$W" == "" ]]; then
W="$IW";
fi
if [[ "$H" == "" ]]; then
H="$IH";
fi
# If a width or height is in U
if [[ "$W" == *"U" ]]; then
W=`echo "$W" | cut -d "U" -f1`
W=$(($W * $FW))
fi
if [[ "$H" == *"U" ]]; then
H=`echo "$H" | cut -d "U" -f1`
H=$(($H * $FH))
fi
# If no sx or sy were set then set them to 0
if [[ "$SX" == "" ]]; then
SX="0";
fi
if [[ "$SY" == "" ]]; then
SY="0";
fi
# If an sx or sy is in U
if [[ "$SX" == *"U" ]]; then
SX=`echo "$SX" | cut -d "U" -f1`
SX=$(($SX * $FW))
fi
if [[ "$SY" == *"U" ]]; then
SY=`echo "$SY" | cut -d "U" -f1`
SY=$(($SY * $FH))
fi
# If no s width or s height were specified use the temp
if [[ "$SW" == "" ]]; then
SW="$IW";
fi
if [[ "$SH" == "" ]]; then
SH="$IH";
fi
# If a width or height is in U
if [[ "$SW" == *"U" ]]; then
SW=`echo "$SW" | cut -d "U" -f1`
SW=$(($SW * $FW))
fi
if [[ "$SH" == *"U" ]]; then
SH=`echo "$SH" | cut -d "U" -f1`
SH=$(($SH * $FH))
fi
# Get column and line count
COL=`tput cols`
LIN=`tput lines`
# TEMPORARY VARIABLES FOR THE ABILITY TO USE BOTH U AND N
TMW="$MW"
TMH="$MH"
# If no max width or height were set then use the default
if [[ "$TMW" == "" ]]; then
MW=$((($FW * $COL) - $X + $SX))
fi
if [[ "$TMH" == "" ]]; then
MH=$((($FH * $(($LIN - 2)) - $Y + $SY))) # substract lines for prompt
fi
# If a width or height is in U
if [[ "$TMW" == *"U"* ]]; then
MW=`echo "$MW" | cut -d "U" -f1 | cut -d "N" -f1`
MW=$(($MW * $FW))
fi
if [[ "$TMH" == *"U"* ]]; then
MH=`echo "$MH" | cut -d "U" -f1 | cut -d "N" -f1`
MH=$(($MH * $FH))
fi
# If a width or height is in U
if [[ "$TMW" == *"N"* ]]; then
MW=`echo "$MW" | cut -d "U" -f1 | cut -d "N" -f1`
MW=$((($FW * $COL) - $X + $SX - $MW))
fi
if [[ "$TMH" == *"N"* ]]; then
MH=`echo "$MH" | cut -d "U" -f1 | cut -d "N" -f1`
MH=$((($FH * $(($LIN - 2)) - $Y + $SY - $MH)))
fi
### DONE SETTING DEFAULTS
if [[ "$W" == "R" ]] && [[ "$H" == "R" ]]; then
W="$IW";
H="$IH";
elif [[ "$W" == "R" ]]; then
W=$(($IW * $H))
W=`printf "%.0f" $(echo "scale=2;$W/$IH" | bc)`
elif [[ "$H" == "R" ]]; then
H=$(($IH * $W))
H=`printf "%.0f" $(echo "scale=2;$H/$IW" | bc)`
fi
# If --no-fit was not run then limit size
if [[ "$FIT" == "1" ]]; then
if test $W -gt $MW; then
H=$(($H * $MW / $W))
W=$MW
fi
if test $H -gt $MH; then
W=$(($W * $MH / $H))
H=$MH
fi
fi
# If --center was run
if [[ "$CENTER" == "1" ]]; then
X=$((($MW - $W) / 2))
Y=$((($MH - $H) / 2))
fi
# Format the command
WC="0;1;$X;$Y;$W;$H;$SX;$SY;$SW;$SH;$FN\n4;\n3;"
# If no cx or cy was set then use the defaults
if [[ "$CX" == "" ]]; then
CX="0";
fi
if [[ "$CY" == "" ]]; then
CY=$(($H + $Y - $SY))
CY=`printf "%.0f" $(echo "scale=2;$CY/$FH" | bc)`
fi
# If cx or cy was set to LAST
if [[ "$CX" == "LAST" ]]; then
CX="$COL";
fi
if [[ "$CY" == "LAST" ]]; then
CY="$LIN"
fi
# If --echo was run
if [[ "$ECHO" == "1" ]]; then
echo -e "$WC"
else
# If --clear was run
if [[ "$CLEAR" == "1" ]]; then
clear
fi
tput cup $CY $CX
echo -e "$WC" | $W3MIMGDISPLAY
fi