parent
ab572b0d32
commit
3f67c75e60
2 changed files with 290 additions and 96 deletions
@ -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]" |
CLEAR="0" # If this is set to 1 it clears the teminal before printing the image |
||||||
else |
FIT="1" # If this is set to 1 then wiw will limit the size of the image |
||||||
printf "\033]" |
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 |
||||||
fi |
CENTER="0" # If this is set to 1 then wiw will center the image between x,y and mw,mh |
||||||
} |
|
||||||
|
# Print help |
||||||
# More of the tmux workaround described above. |
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then |
||||||
function print_st() { |
echo "wiw: w3mimg Wrapper Version: $VERSION" |
||||||
if [[ $TERM == screen* ]] ; then |
echo "USAGE" |
||||||
printf "\a\033\\" |
echo " wiw [-h --help] Display this" |
||||||
else |
echo " wiw --size IMAGE Print the width and height of the image, returned as 'WIDTH HEIGHT'" |
||||||
printf "\a" |
echo " wiw [ARGS] IMAGE Display an image on the terminal" |
||||||
fi |
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" |
||||||
# print_image filename inline base64contents |
echo " --clear Clear the terminal before printing the image" |
||||||
# filename: Filename to convey to client |
echo " --no-fit Don't force the image to fit the terminal" |
||||||
# inline: 0 or 1 |
echo " --center Tells wiw to center the image in the given space" |
||||||
# base64contents: Base64-encoded contents |
echo " -f width${DEL}height Set the width and height of the font, used to determine max term size and items specified in U" |
||||||
function print_image() { |
echo " (default width = ${DFW}, default height = ${DFH})" |
||||||
print_osc |
echo " -g x${DEL}y${DEL}width${DEL}height Set the x, y, width, and height of the image" |
||||||
printf '1337;File=' |
echo " (default x = 0, default y = 0, default w = term width, default h = term height)" |
||||||
if [[ -n "$1" ]]; then |
echo " You can specify these as a #U (were # is any number) to use the unit of the font" |
||||||
printf 'name='`echo -n "$1" | base64`";" |
echo " width and height can be set to R, this will preserve image ratio" |
||||||
fi |
echo " -s x${DEL}y${DEL}width${DEL}height Set the x, y, width, and height of the source image" |
||||||
if $(base64 --version 2>&1 | grep GNU > /dev/null) |
echo " (default x = 0, default y = 0, default w = image width, default h = image height)" |
||||||
then |
echo " You can specify these as a #U (were # is any number) to use the unit of the font" |
||||||
BASE64ARG=-d |
echo " This command is a bit weird, I would reccomend not using it unless really needed" |
||||||
else |
echo " -m width${DEL}height Set the max width and height the image can consume" |
||||||
BASE64ARG=-D |
echo " (default w = full term width, default h = full term height)" |
||||||
fi |
echo " You can specify these as a #U (were # is any number) to use the unit of the font" |
||||||
echo -n "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}' |
echo " You can specify these as a #N (were # is any number) to subtract from the max term size" |
||||||
printf ";inline=$2" |
echo " -c width${DEL}height Set the cursor position for after the image is printed" |
||||||
printf ";width=50" |
echo " (default x = 0, default y = line after image)" |
||||||
printf ":" |
echo " x and y can be set to LAST, to be placed at the last element" |
||||||
echo -n "$3" |
echo " -d delimiter Set the delimiter between items in the -f, -g, and -s args (MUST BE A SINGLE CHARACTER)" |
||||||
print_st |
echo " (default delimiter = '${DEL}')" |
||||||
printf '\n' |
exit |
||||||
} |
# Print the size of the image |
||||||
|
elif [[ "$1" == "--size" ]]; then |
||||||
function error() { |
echo -e "5;$2" | $W3MIMGDISPLAY |
||||||
echo "ERROR: $*" 1>&2 |
exit |
||||||
} |
# Pass a command directly into w3mimgdisplay |
||||||
|
elif [[ "$1" == "--direct" ]]; then |
||||||
function show_help() { |
echo -e "$2" | $W3MIMGDISPLAY |
||||||
echo "Usage: imgcat filename ..." 1>& 2 |
exit |
||||||
echo " or: cat filename | imgcat" 1>& 2 |
fi |
||||||
} |
|
||||||
|
|
||||||
## Main |
|
||||||
|
# Cycle through args |
||||||
if [ -t 0 ]; then |
while [ $# != 1 ]; do |
||||||
has_stdin=f |
if [[ "$1" == "--clear" ]]; then |
||||||
else |
CLEAR="1" |
||||||
has_stdin=t |
elif [[ "$1" == "--no-fit" ]]; then |
||||||
fi |
FIT="0" |
||||||
|
elif [[ "$1" == "--echo" ]]; then |
||||||
# Show help if no arguments and no stdin. |
ECHO="1" |
||||||
if [ $has_stdin = f -a $# -eq 0 ]; then |
elif [[ "$1" == "--center" ]]; then |
||||||
show_help |
CENTER="1" |
||||||
exit |
elif [[ "$1" == "-f" ]]; then |
||||||
fi |
FW=`echo "$2" | cut -d "$DEL" -f1` |
||||||
|
FH=`echo "$2" | cut -d "$DEL" -f2` |
||||||
# Look for command line flags. |
shift |
||||||
while [ $# -gt 0 ]; do |
elif [[ "$1" == "-g" ]]; then |
||||||
case "$1" in |
X=`echo "$2" | cut -d "$DEL" -f1` |
||||||
-h|--h|--help) |
Y=`echo "$2" | cut -d "$DEL" -f2` |
||||||
show_help |
W=`echo "$2" | cut -d "$DEL" -f3` |
||||||
exit |
H=`echo "$2" | cut -d "$DEL" -f4` |
||||||
;; |
shift |
||||||
-*) |
elif [[ "$1" == "-s" ]]; then |
||||||
error "Unknown option flag: $1" |
SX=`echo "$2" | cut -d "$DEL" -f1` |
||||||
show_help |
SY=`echo "$2" | cut -d "$DEL" -f2` |
||||||
exit 1 |
SW=`echo "$2" | cut -d "$DEL" -f3` |
||||||
;; |
SH=`echo "$2" | cut -d "$DEL" -f4` |
||||||
*) |
shift |
||||||
if [ -r "$1" ] ; then |
elif [[ "$1" == "-c" ]]; then |
||||||
print_image "$1" 1 "$(base64 < "$1")" |
CX=`echo "$2" | cut -d "$DEL" -f1` |
||||||
else |
CY=`echo "$2" | cut -d "$DEL" -f2` |
||||||
error "imgcat: $1: No such file or directory" |
shift |
||||||
exit 2 |
elif [[ "$1" == "-m" ]]; then |
||||||
fi |
MW=`echo "$2" | cut -d "$DEL" -f1` |
||||||
;; |
MH=`echo "$2" | cut -d "$DEL" -f2` |
||||||
esac |
shift |
||||||
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 |
||||||
|
|
||||||
|
# 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 |
||||||
|
|
||||||
|
# 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 |
fi |
||||||
|
|
||||||
echo |
# If a width or height is in U |
||||||
echo |
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 |
||||||
|
|
||||||
exit 0 |
### 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…
Reference in new issue