💾 Archived View for bad-gateway.smol.pub › 20220201 captured on 2023-09-28 at 15:50:09. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-03-01)

-=-=-=-=-=-=-

2022-02-01: Gimme gimme gimme

Gimme is one of those absurdly useful little programs that I write once and then forget about. I should make it more permanent.

The idea behind it is simple. Say you have a folder full of things. Say a big bunch of .dot files you want to turn into pretty .png charts, or a bunch of .zip files you want to rename into .cbz's so you can stick 'em on your comic reader. You could use a simple loop like:

for f in *.dot; do dot -Tpng "$f" ${f%%.*}.png; done
for f in *.zip; do mv "$f" "$(basename "$f" .zip).cbz"; done

But who seriously has the time to remember the commands. What I *really* want is to be able to yell:

gimme png
gimme cbz

And have something figure out how to transform all the files for me.

I present: gimme.

  #! /usr/bin/env bash
  make='/usr/bin/env gmake' 
  config="${HOME}/.config/gimme/Makefile"

  makeflags="--keep-going" 

  for extension in $@; do
      for f in *; do
          target="${f%%.*}.${extension}"
          ${make} -f "${config}" ${makeflags} "${target}" 2>/dev/null
      done
  done

Gimme works by having a Makefile hidden in the config directory (~/.config/gimme/Makefile) that contains all the rules for converting files that gimme needs to know about. The gimme script itself just switches out the extension for the one you want for every file it can see and sees if make can find a way through the makefile for it. My gimmefile looks like:

  %.cbz: %	; zip -r $@ {body}lt;

  %.jpeg: %.jpg	; cp {body}lt; $@
  %.jpeg: %.png	; convert {body}lt; $@
  %.jpeg: %.pdf	; convert "{body}lt;[0]" $@

  %.mp4: %.avi	; ffmpeg -i "{body}lt;" -c:v copy -c:a copy -y "$@"
  %.mp4: %.gif	; ffmpeg -i "{body}lt;" -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" "$@"
  %.mp4: %.mov	; ffmpeg -i "{body}lt;" -vcodec h264 -acodec mp2  -qscale 0 "$@"
  %.mp4: %.mkv	; ffmpeg -i "{body}lt;" -vcodec h264 -acodec mp2  -qscale 0 "$@"

  %.pdf: %.dot	; dot -Tpdf {body}lt; -o $@
  %.pdf: %.tex	; latexmk -pdf {body}lt;
  %.png: %.pdf	; convert "{body}lt;[0]" $@

  %.png: %.dot	; dot -Tpng {body}lt; -o $@
  %.png: %.jpeg	; convert {body}lt; >$@

  %.tar.gz: %	; tar czf $@ {body}lt;

  %.zip: %	; zip -r $@ {body}lt;

It is surprisingly powerful. It is also prone to breaking and going wrong but who cares? Its a tool for doing bulk conversion and I love it.