Add freeEbook and imgcat

master
Jonathan Hodgson 7 years ago
parent ab572b0d32
commit 3f67c75e60
  1. 8
      bin/freeEbook
  2. 364
      bin/imgcat

@ -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

@ -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
# filename: Filename to convey to client
# inline: 0 or 1
# base64contents: Base64-encoded contents
function print_image() {
print_osc
printf '1337;File='
if [[ -n "$1" ]]; then
printf 'name='`echo -n "$1" | base64`";"
fi
if $(base64 --version 2>&1 | grep GNU > /dev/null)
then
BASE64ARG=-d
else
BASE64ARG=-D
fi
echo -n "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}'
printf ";inline=$2"
printf ";width=50"
printf ":"
echo -n "$3"
print_st
printf '\n'
}
function error() {
echo "ERROR: $*" 1>&2
}
function show_help() {
echo "Usage: imgcat filename ..." 1>& 2
echo " or: cat filename | imgcat" 1>& 2
}
## Main
if [ -t 0 ]; then
has_stdin=f
else
has_stdin=t
fi
# Show help if no arguments and no stdin. # Print help
if [ $has_stdin = f -a $# -eq 0 ]; then if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
show_help echo "wiw: w3mimg Wrapper Version: $VERSION"
echo "USAGE"
echo " wiw [-h --help] Display this"
echo " wiw --size IMAGE Print the width and height of the image, returned as 'WIDTH HEIGHT'"
echo " wiw [ARGS] IMAGE Display an image on the terminal"
echo " wiw --echo [ARGS] IMAGE Print out the command to produce the desired image"
echo " wiw --direct CMD Pass a command directly into w3mimgdisplay"
echo "ARGS"
echo " --clear Clear the terminal before printing the image"
echo " --no-fit Don't force the image to fit the terminal"
echo " --center Tells wiw to center the image in the given space"
echo " -f width${DEL}height Set the width and height of the font, used to determine max term size and items specified in U"
echo " (default width = ${DFW}, default height = ${DFH})"
echo " -g x${DEL}y${DEL}width${DEL}height Set the x, y, width, and height of the image"
echo " (default x = 0, default y = 0, default w = term width, default h = term height)"
echo " You can specify these as a #U (were # is any number) to use the unit of the font"
echo " width and height can be set to R, this will preserve image ratio"
echo " -s x${DEL}y${DEL}width${DEL}height Set the x, y, width, and height of the source image"
echo " (default x = 0, default y = 0, default w = image width, default h = image height)"
echo " You can specify these as a #U (were # is any number) to use the unit of the font"
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)"
echo " You can specify these as a #U (were # is any number) to use the unit of the font"
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)"
echo " x and y can be set to LAST, to be placed at the last element"
echo " -d delimiter Set the delimiter between items in the -f, -g, and -s args (MUST BE A SINGLE CHARACTER)"
echo " (default delimiter = '${DEL}')"
exit
# Print the size of the image
elif [[ "$1" == "--size" ]]; then
echo -e "5;$2" | $W3MIMGDISPLAY
exit
# Pass a command directly into w3mimgdisplay
elif [[ "$1" == "--direct" ]]; then
echo -e "$2" | $W3MIMGDISPLAY
exit exit
fi fi
# Look for command line flags.
while [ $# -gt 0 ]; do
case "$1" in # Cycle through args
-h|--h|--help) while [ $# != 1 ]; do
show_help if [[ "$1" == "--clear" ]]; then
exit CLEAR="1"
;; elif [[ "$1" == "--no-fit" ]]; then
-*) FIT="0"
error "Unknown option flag: $1" elif [[ "$1" == "--echo" ]]; then
show_help ECHO="1"
exit 1 elif [[ "$1" == "--center" ]]; then
;; CENTER="1"
*) elif [[ "$1" == "-f" ]]; then
if [ -r "$1" ] ; then FW=`echo "$2" | cut -d "$DEL" -f1`
print_image "$1" 1 "$(base64 < "$1")" FH=`echo "$2" | cut -d "$DEL" -f2`
else shift
error "imgcat: $1: No such file or directory" elif [[ "$1" == "-g" ]]; then
exit 2 X=`echo "$2" | cut -d "$DEL" -f1`
Y=`echo "$2" | cut -d "$DEL" -f2`
W=`echo "$2" | cut -d "$DEL" -f3`
H=`echo "$2" | cut -d "$DEL" -f4`
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 fi
;;
esac
shift 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
# If an X or Y was not set then set them to 0
if [[ "$X" == "" ]]; then
X="0";
fi
if [[ "$Y" == "" ]]; then
Y="0";
fi fi
echo # If an x or y is in U
echo 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`
exit 0 # 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

Loading…
Cancel
Save