Skip to content

Instantly share code, notes, and snippets.

@hyiromori hyiromori/lgtm.sh
Last active Dec 6, 2019

Embed
What would you like to do?
#!/usr/bin/env bash
THIS_DIR="$(cd "$(dirname "${BASH_SOURCE}")"; pwd)"
function usage() {
cat << EOS
Usage
$(basename $0) <command>
Command
<Empty> -> rand
rand
conv
input
dir
list
help
EOS
}
INPUT_DIR="${HOME}/lgtm/input"
OUTPUT_DIR="${HOME}/lgtm/output"
TEXT="LGTM"
mkdir -p "${INPUT_DIR}"
mkdir -p "${OUTPUT_DIR}"
function convert_image(){
local input="$1"
local output="${OUTPUT_DIR}/$(uuidgen | tr '[:upper:]' '[:lower:]' | tr -d '\n').${input##*.}"
convert \
-resize "480x480>" \
-pointsize "60" \
-fill "#ff7700" \
-gravity "NorthWest" \
-annotate "0x0+20+0" \
"${TEXT}" \
"${input}" \
"${output}" &&
rm "${input}" &&
echo "Convert: ${input} -> ${output}" ||
echo "Failed : ${input}"
}
COMMAND="${1:-"rand"}"; shift
if [[ "${COMMAND}" == "rand" ]]; then
FILE_NUM="$(ls -F "${OUTPUT_DIR}" | grep -v / | wc -l)"
LINE=$((${RANDOM} % ${FILE_NUM} + 1))
FILE_PATH="${OUTPUT_DIR}/$(ls -1 "${OUTPUT_DIR}" | head -n "${LINE}" | tail -n 1)"
echo "Copy: ${FILE_PATH}"
osascript -e "set the clipboard to (read (POSIX file \"${FILE_PATH}\") as JPEG picture)"
elif [[ "${COMMAND}" == "dir" ]]; then
open "${OUTPUT_DIR}"
elif [[ "${COMMAND}" == "input" ]]; then
open "${INPUT_DIR}"
elif [[ "${COMMAND}" == "conv" ]]; then
find "${INPUT_DIR}" -name "*.jpg" |
while read FILE; do
convert_image "${FILE}"
done
elif [[ "${COMMAND}" == "list" ]]; then
find "${OUTPUT_DIR}" -name "*.jpg"
elif [[ "${COMMAND}" == "help" ]]; then
usage
exit 0
else
usage
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.