From a8d1d817242d452087bda6033e8be0b1a9218086 Mon Sep 17 00:00:00 2001 From: Thomas Dehaeze Date: Mon, 25 Oct 2021 14:22:47 +0200 Subject: [PATCH] Improove linkhandler --- binaries.org | 431 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 403 insertions(+), 28 deletions(-) diff --git a/binaries.org b/binaries.org index d7285da..220390c 100644 --- a/binaries.org +++ b/binaries.org @@ -743,31 +743,45 @@ else fi #+end_src -* =linkhandler= - Open with Default application +* =linkhandler= - Open any URL with Default application :PROPERTIES: :CUSTOM_ID: linkhandler :END: Inspired from =linkhandler= script ([[https://github.com/LukeSmithxyz/voidrice/][github]]). -This is used to open any type of file with the wanted program. -It can be used in =newsboat=, =neomutt= and =ranger= for instance. +This is used to open any *url* with the wanted program. #+begin_src bash :tangle ~/.local/bin/linkhandler [ -z "$1" ] && { "$BROWSER"; exit; } case "$1" in - ,*mkv|*webm|*mp4|*youtube.com/watch*|*youtube.com/playlist*|*youtu.be*) - setsid mpv --input-ipc-server=/tmp/mpvsoc$(date +%s) -quiet "$1" >/dev/null 2>&1 & ;; - ,*png|*jpg|*jpe|*jpeg|*gif) - curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///")" && sxiv -a "/tmp/$(echo "$1" | sed "s/.*\///")" >/dev/null 2>&1 & ;; + ,*mkv|*webm|*mp4|*youtube.com/watch*|*youtube.com/playlist*|*youtu.be*|*gif) + setsid mpv -quiet "$1" >/dev/null 2>&1 & ;; + ,*png|*jpg|*jpe|*jpeg) + curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" && sxiv -a "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;; + ,*pdf|*cbz|*cbr) + curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" && zathura "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;; + ,*svg) + curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" && inkview "/tmp/$(echo "$1" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;; ,*mp3|*flac|*opus|*mp3?source*) setsid curl -LO "$1" >/dev/null 2>&1 & ;; ,*) - if [ -f "$1" ]; then "$TERMINAL" -e "$EDITOR $1" - else setsid $BROWSER "$1" >/dev/null 2>&1 & fi ;; + [ -f "$1" ] && setsid -f "$TERMINAL" -e "$EDITOR" "$1" >/dev/null 2>&1 || setsid -f "$BROWSER" "$1" >/dev/null 2>&1 esac #+end_src +* =open= - Open any file using =rifle= +:PROPERTIES: +:CUSTOM_ID: filehandler +:END: + +Simple alternative to =xdg-open=, uses the =rifle= program included with =ranger=. +The default applications are listed [[file:ranger.org::#rifle][here]]. + +#+begin_src bash :tangle ~/.local/bin/open +rifle -p $(rifle -l "$1" | rofi -dmenu -i | sed -n -e 's/\([0-9]*\):.*/\1/p') "$1" +#+end_src + * =lockscreen= - Lock Screen :PROPERTIES: :CUSTOM_ID: lockscreen @@ -917,19 +931,46 @@ if [[ -f "$1" && "$1" == *.pdf ]]; then fi #+end_src -* =convert-file= - Convert any file to another filetype +* TODO =convert-file= - Convert any file to another filetype :PROPERTIES: :CUSTOM_ID: convert-file +:header-args: :tangle ~/.local/bin/convert-file :END: -#+begin_src bash :tangle ~/.local/bin/convert-file +** Get basic information about the file +#+begin_src bash # Get filename filename_with_extension=$(basename -- "$1") # Extract extension of the file in_ext="${filename_with_extension##*.}" # filename without extension filename_without_extension=${filename_with_extension%.*} +#+end_src +** SVG files +List of useful programs: +- =inkscape= + +*** SVG to PNG +#+begin_src bash +svg2png_function() { \ + if command -v inkscape &> /dev/null; then + inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension" + fi +} +#+end_src + +*** SVG to PDF +#+begin_src bash +svg2pdf_function() { \ + if command -v inkscape &> /dev/null; then + inkscape --export-type="$out_ext" "$filename_with_extension" + fi +} +#+end_src + +*** Main function +#+begin_src bash # Convert SVG Files svg2() { \ out_ext=$(echo -e "pdf\npng" | rofi -i -dmenu -p "Convert SVG to") @@ -940,36 +981,220 @@ svg2() { \ case "$out_ext" in "png") - inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension" + svg2png_function ;; "pdf") - inkscape --export-type="$out_ext" "$filename_with_extension" - # pdf2svg file.pdf file.svg + svg2pdf_function ;; esac } +#+end_src +** PDF files +List of useful programs: +- =pdftocairo= +- =pdftoppm= +- =convert= +- =inkscape= +- =gs= +- =pdfcrop= +- =pdftk= + +*** PDF to PNG +#+begin_src bash +pdf2png_function() { \ + if command -v pdftocairo &> /dev/null; then + pdftocairo -png -singlefile -cropbox "$filename_with_extension" "$filename_without_extension" + elif command -v pdftoppm &> /dev/null; then + pdftoppm -png "$filename_with_extension" > "$filename_without_extension.png" + elif command -v convert &> /dev/null; then + convert -density 100 -trim -antialias "$filename_with_extension" -quality 100 "$filename_without_extension.png" + elif command -v inkscape &> /dev/null; then + inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension" + fi +} +#+end_src + +*** PDF to SVG +#+begin_src bash +pdf2svg_function() { \ + if command -v pdftocairo &> /dev/null; then + pdftocairo -svg "$filename_with_extension" "$filename_without_extension.svg" + elif command -v pdf2svg &> /dev/null; then + pdf2svg "$filename_with_extension" "$filename_without_extension.svg" + elif command -v inkscape &> /dev/null; then + inkscape --export-type="$out_ext" "$filename_with_extension" + fi +} +#+end_src + +*** PDF Reduce Size +#+begin_src bash +pdfreduce_function() { \ + if command -v gs &> /dev/null; then + # Maybe ask for new filename? + gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$filename_without_extension.red.pdf" "$filename_with_extension" + fi +} +#+end_src + +*** PDF Trim +#+begin_src bash +pdftrim_function() { \ + if command -v pdfcrop &> /dev/null; then + pdfcrop "$filename_with_extension" "$filename_with_extension" + fi +} +#+end_src + +*** PDF Extract Pages +#+begin_src bash +pdfextract_function() { \ + if command -v pdftk &> /dev/null; then + pages=$(echo -e "get first\ndelete first\nextract i-j" | rofi -i -dmenu -p "Extract pages") + + if [ -z "$pages" ]; then + exit; + fi + + case "$pages" in + "get first") + if command -v pdftk &> /dev/null; then + pdftk "$filename_with_extension" cat 1-1 output "$filename_without_extension.first.pdf" + fi + ;; + "delete first") + if command -v pdftk &> /dev/null; then + pdftk "$filename_with_extension" cat 2-end output "$filename_with_extension" + fi + ;; + "extract i-j") + # TODO + page_i=$(rofi -dmenu -p "From") + page_j=$(rofi -dmenu -p "To") + if command -v pdftk &> /dev/null; then + pdftk "$filename_with_extension" cat "$page_i-$page_j" output "$filename_with_extension.slice.pdf" + fi + ;; + esac + fi +} +#+end_src + +*** Delete First Page +#+begin_src bash +pdf_delete_first_page_function() { \ + if command -v stapler &> /dev/null; then + stapler del "$filename_with_extension" 1 /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$filename_with_extension" + elif command -v pdftk &> /dev/null; then + pdftk "$filename_with_extension" cat 2-end output /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$filename_with_extension" + fi +} +#+end_src + +*** Remove Annotations +#+begin_src bash +pdf_remove_annotations_function() { \ + if command -v pdftk &> /dev/null; then + pdftk "$filename_with_extension" output /tmp/uncompressed.pdf uncompress + LANG=C sed -n '/^\/Annots/!p' /tmp/uncompressed.pdf > /tmp/stripped.pdf + pdftk /tmp/stripped.pdf output "$filename_with_extension" compress + fi +} +#+end_src + +*** Main function +#+begin_src bash # Convert PDF Files pdf2() { \ - out_ext=$(echo -e "svg\npng\nreduce" | rofi -i -dmenu -p "Convert PDF to") + out_ext=$(echo -e "svg\npng\nreduce size\ntrim\nextract pages\ndelete first page\nremove annotations" | rofi -i -dmenu -p "Convert PDF to") if [ -z "$out_ext" ]; then exit; fi case "$out_ext" in - "png") - inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension" - ;; - "reduce") - gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$filename_without_extension.red.pdf" "$filename_with_extension" - ;; "svg") - inkscape --export-type="$out_ext" "$filename_with_extension" + pdf2svg_function + ;; + "png") + pdf2png_function + ;; + "reduce size") + pdfreduce_function + ;; + "trim") + pdftrim_function + ;; + "extract pages") + pdfextract_function + ;; + "delete first page") + pdf_delete_first_page_function + ;; + "remove annotations") + pdf_remove_annotations_function ;; esac } +#+end_src +** DVI files +List of useful programs: +- =dvisvgm= +- =dvipng= + +*** DVI to PNG +#+begin_src bash +dvi2png_function() { \ + if command -v dvipng &> /dev/null; then + dvipng "$filename_with_extension" + fi +} +#+end_src + +*** DVI to SVG +#+begin_src bash +dvi2svg_function() { \ + if command -v dvisvgm &> /dev/null; then + dvisvgm "$filename_with_extension" -o "$filename_without_extension.svg" + fi +} +#+end_src + +*** Main function +#+begin_src bash +# Convert DVI Files +dvi2() { \ + out_ext=$(echo -e "svg\npng" | rofi -i -dmenu -p "Convert DVI to") + + if [ -z "$out_ext" ]; then + exit; + fi + + case "$out_ext" in + "svg") + dvi2svg_function + ;; + "png") + dvi2png_function + ;; + esac +} +#+end_src + +** DOCX/PTTX files +*** DOC to PDF +#+begin_src bash +doc2pdf_function() { \ + if command -v lowriter &> /dev/null; then + lowriter --convert-to pdf "$filename_with_extension" + fi +} +#+end_src + +*** Main function +#+begin_src bash # Convert DOCX/PPTX Files docx2() { \ out_ext=$(echo -e "pdf" | rofi -i -dmenu -p "Convert DOCX/PPTX to") @@ -980,11 +1205,24 @@ docx2() { \ case "$out_ext" in "pdf") - lowriter --convert-to pdf "$filename_with_extension" + doc2pdf_function ;; esac } +#+end_src +** GIF files +*** GIF to PNG +#+begin_src bash +gif2png_function() { \ + if command -v convert &> /dev/null; then + convert -coalesce "$filename_with_extension" "$filename_without_extension.png" + fi +} +#+end_src + +*** Main function +#+begin_src bash # Convert GIF Files gif2() { \ out_ext=$(echo -e "png" | rofi -i -dmenu -p "Convert GIF to") @@ -995,14 +1233,61 @@ gif2() { \ case "$out_ext" in "png") - convert -coalesce "$filename_with_extension" "$filename_without_extension.png" + gif2png_function ;; esac } +#+end_src +** PNG files +*** PNG to PDF +#+begin_src bash +png2pdf_function() { \ + if command -v convert &> /dev/null; then + convert "$filename_with_extension" "$filename_without_extension.pdf" + fi +} +#+end_src + +*** PNG to JPG +#+begin_src bash +png2jpg_function() { \ + if command -v convert &> /dev/null; then + convert "$filename_with_extension" "$filename_without_extension.jpg" + fi +} +#+end_src + +*** PNG Trim +#+begin_src bash +pngtrim_function() { \ + if command -v convert &> /dev/null; then + convert -trim "$filename_with_extension" "$filename_with_extension" + fi +} +#+end_src + +*** PNG Resize +#+begin_src bash +pngresize_function() { \ + if command -v convert &> /dev/null; then + size_type=$(echo -e "width\nheight" | rofi -i -dmenu -p "Maximum:") + size_px=$(rofi -dmenu -p "Number of px:") + + if [ "$size_type" = "width" ]; then + convert -resize "$size_px"x "$filename_with_extension" "$filename_with_extension" + elif [ "$size_type" = "height" ]; then + convert -resize x"$size_px" "$filename_with_extension" "$filename_with_extension" + fi + fi +} +#+end_src + +*** Main function +#+begin_src bash # Convert PNG Files png2() { \ - out_ext=$(echo -e "pdf\ntrim" | rofi -i -dmenu -p "Convert PNG to") + out_ext=$(echo -e "pdf\njpg\ntrim\nresize" | rofi -i -dmenu -p "Convert PNG to") if [ -z "$out_ext" ]; then exit; @@ -1010,14 +1295,92 @@ png2() { \ case "$out_ext" in "pdf") - convert "$filename_with_extension" "$filename_without_extension.pdf" + png2pdf_function + ;; + "jpg") + png2jpg_function ;; "trim") - convert -trim "$filename_with_extension" "$filename_with_extension" # Maybe should ask new filename + pngtrim_function + ;; + "resize") + pngresize_function ;; esac } +#+end_src +** JPG files +*** JPG to PDF +#+begin_src bash +jpg2pdf_function() { \ + if command -v convert &> /dev/null; then + convert "$filename_with_extension" "$filename_without_extension.pdf" + fi +} +#+end_src + +*** JPG Resize +#+begin_src bash +jpgresize_function() { \ + if command -v convert &> /dev/null; then + size_type=$(echo -e "width\nheight" | rofi -i -dmenu -p "Maximum:") + size_px=$(rofi -dmenu -p "Number of px:") + + if [ "$size_type" = "width" ]; then + convert -resize "$size_px"x "$filename_with_extension" "$filename_with_extension" + elif [ "$size_type" = "height" ]; then + convert -resize x"$size_px" "$filename_with_extension" "$filename_with_extension" + fi + fi +} +#+end_src + +*** Main function +#+begin_src bash +# Convert JPG Files +jpg2() { \ + out_ext=$(echo -e "pdf\nresize" | rofi -i -dmenu -p "Convert JPG to") + + if [ -z "$out_ext" ]; then + exit; + fi + + case "$out_ext" in + "pdf") + jpg2pdf_function + ;; + "resize") + jpgresize_function + ;; + esac +} +#+end_src + +** MP4 files +*** MP4 to GIF +#+begin_src bash +mp42gif_function() { \ + if command -v ffmpeg &> /dev/null; then + palette="/tmp/palette.png" + + width=$(echo -e "auto" | rofi -i -dmenu -p "GIF width") + if [ "$width" = "auto" ]; then + filters="fps=15" + else + filters="fps=15,scale=$width:-1:flags=lanczos" + fi + + # Generate optimal pallette of colors + ffmpeg -v warning -i "$filename_with_extension" -vf "$filters,palettegen" -y $palette + # Convert + ffmpeg -v warning -i "$filename_with_extension" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$filename_without_extension.gif" + fi +} +#+end_src + +*** Main function +#+begin_src bash # Convert MP4 Files mp42() { \ out_ext=$(echo -e "gif" | rofi -i -dmenu -p "Convert MP4 to") @@ -1028,11 +1391,14 @@ mp42() { \ case "$out_ext" in "gif") - make-gif "$filename_with_extension" "$filename_without_extension.gif" + mp42gif_function ;; esac } +#+end_src +** Case statement +#+begin_src bash case "$in_ext" in "svg") svg2 @@ -1040,6 +1406,9 @@ case "$in_ext" in "gif") gif2 ;; + "dvi") + dvi2 + ;; "pdf") pdf2 ;; @@ -1049,6 +1418,12 @@ case "$in_ext" in "png") png2 ;; + "jpg") + jpg2 + ;; + "jpeg") + jpg2 + ;; "docx") docx2 ;;