From 8f5589720949f932741746ade4c34fcac728caa9 Mon Sep 17 00:00:00 2001 From: yemou Date: Mon, 2 Nov 2020 03:22:30 +0000 Subject: [PATCH] transition repo to general scripts --- README | 20 +- exmaples/{ => thm}/scripts/reload-sway | 0 .../{ => thm}/templates/colors.sh.template | 0 .../{ => thm}/templates/sway-colors.template | 0 exmaples/{ => thm}/themes/default-dark.theme | 0 exmaples/{ => thm}/themes/default-light.theme | 0 .../{ => thm}/themes/grayscale-light.theme | 0 exmaples/{ => thm}/themes/light.theme | 0 scritps/colorgrab | 111 ++++++++++ scritps/scr | 201 ++++++++++++++++++ thm => scritps/thm | 0 11 files changed, 313 insertions(+), 19 deletions(-) rename exmaples/{ => thm}/scripts/reload-sway (100%) rename exmaples/{ => thm}/templates/colors.sh.template (100%) rename exmaples/{ => thm}/templates/sway-colors.template (100%) rename exmaples/{ => thm}/themes/default-dark.theme (100%) rename exmaples/{ => thm}/themes/default-light.theme (100%) rename exmaples/{ => thm}/themes/grayscale-light.theme (100%) rename exmaples/{ => thm}/themes/light.theme (100%) create mode 100755 scritps/colorgrab create mode 100755 scritps/scr rename thm => scritps/thm (100%) diff --git a/README b/README index 348a99f..d7df356 100644 --- a/README +++ b/README @@ -1,20 +1,2 @@ # description -thm is a shell script that applies colors to a template file. - -# usage -thm theme_name - -the theme_name does not include the `.theme` at the end of the file. - -# directories -By default, the config files will be searched for in `$XDG_CONFIG_HOME/thm` or `$HOME/.config/thm`. -The generated files will be placed in either `$XDG_CACHE_HOME/thm` or `$HOME/.cache/thm`. This can -be changed by setting the environment variables `THM_CONFIG_DIR` and `THM_DEST_DIR`. The dest dir -is cleared while running. - -# inside config directory -The themes directory holds shell scripts defining the colors. These files should end with `.theme` -if they should be usable with thm. The templates directory holds files for generating the themes. -These files should end with `.template` The scripts directory holds files to be executed after thm -is finished generating all the files. These files should be executable in order for thm to launch -them. Examples of these files can be found in the examples directory of this repository. +Random scripts that could be useful outside of my dotfiles diff --git a/exmaples/scripts/reload-sway b/exmaples/thm/scripts/reload-sway similarity index 100% rename from exmaples/scripts/reload-sway rename to exmaples/thm/scripts/reload-sway diff --git a/exmaples/templates/colors.sh.template b/exmaples/thm/templates/colors.sh.template similarity index 100% rename from exmaples/templates/colors.sh.template rename to exmaples/thm/templates/colors.sh.template diff --git a/exmaples/templates/sway-colors.template b/exmaples/thm/templates/sway-colors.template similarity index 100% rename from exmaples/templates/sway-colors.template rename to exmaples/thm/templates/sway-colors.template diff --git a/exmaples/themes/default-dark.theme b/exmaples/thm/themes/default-dark.theme similarity index 100% rename from exmaples/themes/default-dark.theme rename to exmaples/thm/themes/default-dark.theme diff --git a/exmaples/themes/default-light.theme b/exmaples/thm/themes/default-light.theme similarity index 100% rename from exmaples/themes/default-light.theme rename to exmaples/thm/themes/default-light.theme diff --git a/exmaples/themes/grayscale-light.theme b/exmaples/thm/themes/grayscale-light.theme similarity index 100% rename from exmaples/themes/grayscale-light.theme rename to exmaples/thm/themes/grayscale-light.theme diff --git a/exmaples/themes/light.theme b/exmaples/thm/themes/light.theme similarity index 100% rename from exmaples/themes/light.theme rename to exmaples/thm/themes/light.theme diff --git a/scritps/colorgrab b/scritps/colorgrab new file mode 100755 index 0000000..2ad1eed --- /dev/null +++ b/scritps/colorgrab @@ -0,0 +1,111 @@ +#!/bin/sh +# Grab the color of a specific point + +# Required Commands: +# convert(imagemagick) - Get the color from the screenshotted point +# grim - Take a screenshot of the selected point +# slurp - Select a point +# mkdir - Create missing directories +# notify-send (optional) - Send a screenshot with the color +# wl-copy(wl-clipboard) (optional) - Copy the color into the clipboard +# xdg-open (optional) - Open the screenshot in an image viewer + +usage() { + printf '%s\n' "usage: ${0##*/} [options]" \ + "options:" \ + " -c - copy the color code to the clipboard" \ + " -h - display usage statement" \ + " -n - send a notification with the color code" \ + " -o - open an image with the color and the color code" \ + " -r - display the color code as an rgb value" \ + " -x - display the color code as a hexcode value (default)" +} + +# Parse arguments +for flag in "$@" +do + # Make sure flag begins with '-' and are atleast two characters long + case $flag in + - ) continue ;; + -- ) break ;; + -* ) ;; + * ) continue ;; + esac + + # Split the flags into individual arguments and set variables + flag=${flag#-} + while [ "$flag" ] + do + a=${flag%${flag#?}} + case $a in + c ) copy_to_clipboard=true ;; + h ) usage; exit 0 ;; + n ) send_notification=true ;; + o ) open_color_image=true ;; + r ) color_type="rgb" ;; + x ) color_type="hex" ;; + * ) printf '%s\n' "${0##*/}: -$a invalid argument" 1>&2 + usage 1>&2; exit 1 ;; + esac + flag=${flag#?} + done +done + +# Make sure this directory exist before continuing +[ -d "/tmp/${0##*/}" ] || { + mkdir -p "/tmp/${0##*/}" || \ + printf '%s\n' "${0##*/}: failed to create directory: /tmp/${0##*/}" 1>&2 \ + exit 1 +} + +# Set color_type if not already set +[ $color_type ] || color_type="hex" + +# Get a screenshot of the pixel +grim -s 1 -g "$(slurp -p)" "/tmp/${0##*/}/temp.png" + +case $color_type in + hex ) color=$(convert "/tmp/${0##*/}/temp.png" -format "%[hex:p]\n" info:) ;; + rgb ) color=$(convert "/tmp/${0##*/}/temp.png" -format "%[pixel:p]\n" info:) + color=${color#*(}; color=${color%)*} ;; + * ) printf '%s\n' "${0##*/}: invalid color_type: $color_type" 1>&2; exit 1 ;; +esac +echo "$color" + +# Copy color to clipboard +[ $copy_to_clipboard ] && { + wl-copy -n "$color" || printf '%s\n' "${0##*/}: failed to copy color to clipboard" 1>&2 +} + +# Open color image in the user's perfered image viewer +[ $open_color_image ] && { + # Create the color image if it doesn't already exist + [ -f "/tmp/${0##*/}/o$color.png" ] || { + case $color_type in + hex ) ocolor="#$color" ;; + rgb ) ocolor="rgb($color)";; + esac + convert -size 150x150 xc:"$ocolor" +size -gravity center \ + \( -background white pango:" $color " \) \ + -composite "/tmp/${0##*/}/o$color.png" + } + xdg-open "/tmp/${0##*/}/o$color.png" > "/tmp/${0##*/}/xdg-open.log" 2>&1 & +} + +# Send a notification with an image of the color aswell as the value +[ $send_notification ] && { + [ -f "/tmp/${0##*/}/n$color.png" ] || { + case $color_type in + hex ) ncolor="#$color"; color_prefix="hex:";; + rgb ) ncolor="rgb($color)" + color_r="${color%%,*}" + color_g="${color#*,}"; color_g="${color_g%,*}" + color_b="${color##*,}" + color_rgb="$color_r$color_g$color_b" + color_prefix="rgb:";; + esac + convert -size 64x64 xc:"$ncolor" "/tmp/${0##*/}/n$color_rgb.png" + } + notify-send -a "${0##*/}" -i "/tmp/${0##*/}/n$color_rgb.png" "$color_prefix $color" +} diff --git a/scritps/scr b/scritps/scr new file mode 100755 index 0000000..1d7fb3d --- /dev/null +++ b/scritps/scr @@ -0,0 +1,201 @@ +#!/bin/sh +# shellcheck disable=SC1090,SC2154 + +# SC1090 & SC2154 +# The files sourced are user generated files that should contain the needed +# variables for the script to function correctly. It should be safe to ignore +# these warnings. + +# Required Commands +# ffmpeg - needed for aud +# grim - needed for pic +# mkdir - create missing directories +# pactl - create loopback devices for multi-device audio recording in wf-recorder +# slurp - make a selection +# wf-recorder - needed for rec +# wl-copy - copy images to clipboard + +# This script is intended for use on wayland; however, `scr aud` should work fine without +# wayland. + +# Set required variables if needed +[ "$SCR_CFG_DIR" ] || SCR_CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/scr" +[ "$SCR_CACHE_DIR" ] || SCR_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/scr" + +# Source the configuration file +# A sample configuration can be found in my dotfiles at: +# https://github.com/yemouu/setup/blob/master/root/home/cfg/scr/config.sh +. "$SCR_CFG_DIR/config.sh" || \ + { printf '%s\n' "${0##*/}: failed to source $SCR_CONFIG_DIR/config.sh" 1>&2; exit 1; } + +# Usage statement for the script +usage() { + printf '%s\n' "usage: ${0##*/} action [options]" \ + "actions:" \ + " aud - audio" \ + " pic - picture" \ + " rec - record" \ + "options:" \ + " -a - record desktop audio" \ + " -c - copy image to clipboard" \ + " -h - display this message" \ + " -m - record microphone audio" +} + +# Determine the action to run +case $1 in + aud ) action="scr_aud" ;; + pic ) action="scr_pic" ;; + rec ) action="scr_rec" ;; + *h|*help ) usage; exit 0 ;; + * ) printf '%s\n' "${0##*/}: $1: invalid action" 1>&2; usage 1>&2; exit 1 ;; +esac +shift + +# Determine options to run with based on arguments +for flag in "$@" +do + # Make sure arguments start with '-' and are atleast 2 characters long + case $flag in + - ) continue ;; + -- ) break ;; + -* ) ;; + * ) continue;; + esac + + # Split arguments to be 1 character long and determine options to use + flag=${flag#-} + while [ "$flag" ] + do + a=${flag%${flag#?}} + case $a in + a ) desktop_audio=true ;; + c ) copy_clipboard=true ;; + h ) usage; exit 0 ;; + m ) microphone=true ;; + * ) printf '%s\n' "${0##*/}: -$a: invalid argument" 1>&2 + usage 1>&2; exit 1 ;; + esac + flag=${flag#?} + done +done +unset args arg + +# Simple function to print out an error message and exit +die() { + printf '%s\n' "${0##*/}: $*" 1>&2 + exit 1 +} + +# Record Audio +scr_aud() { + # Create the directory to store audio recordings if it does not already exist + [ -d "$scr_aud_dir" ] || \ + { mkdir -p "$scr_aud_dir" || die "failed to make directory: $scr_aud_dir"; } + + # Create the directory to store logs if it does not already exist + [ -d "$SCR_CACHE_DIR" ] || \ + { mkdir -p "$SCR_CACHE_DIR" || \ + die "failed to make directory: $SCR_CACHE_DIR"; } + + filename="$scr_aud_dir/$aud_filename" + + # Require atleast one of the arguments: -a or -m + [ "$microphone" ] || [ "$desktop_audio" ] || \ + { die "aud: argument -a or -m is required to record audio"; } + + # Set ffmpeg options based on script options + [ "$microphone" ] && { args="-f pulse -i $aud_source"; } + [ "$desktop_audio" ] && { args="$args -f pulse -i $aud_sink"; } + [ "$microphone" ] && [ "$desktop_audio" ] && \ + { args="$args -filter_complex amix=inputs=2"; } + + # Pressing Ctrl+C will exit the script instead of just wf-recorder. + # Intercept Ctrl+C and exit wf-recorder instead of the script + trap 'kill -2 $aud_pid' INT + + # shellcheck disable=SC2086 + # Word splitting is favorable here + ffmpeg $args "$filename" > "$SCR_CACHE_DIR/aud.log" 2>&1 & + aud_pid=$! + printf '%s' "Press Ctrl+C to stop recording. " 1>&2 + wait $aud_pid + + # Reset the trap + trap - INT + + printf '\n%s\n' "$filename" +} + +# Take a screenshot +scr_pic() { + # Create directories if they do not already exist + [ -d "$scr_pic_dir" ] || \ + { mkdir -p "$scr_pic_dir" || die "failed to make directory: $scr_pic_dir"; } + + [ -d "$SCR_CACHE_DIR" ] || \ + { mkdir -p "$SCR_CACHE_DIR" || \ + die "failed to create directory: $SCR_CACHE_DIR"; } + + filename="$scr_pic_dir/$pic_filename" + + # Get the geometry of the screenshot from the user and take the screenshot + grim -g "$(slurp)" "$filename" > "$SCR_CACHE_DIR/pic.log" 2>&1 + + # Copy the image to the system clipboard + $copy_clipboard && { wl-copy <"$filename" > "$SCR_CACHE_DIR/copy.log" 2>&1; } + + printf '%s\n' "$filename" +} + +scr_rec() { + # Create directories if they do not already exist + [ -d "$scr_rec_dir" ] || \ + { mkdir -p "$scr_rec_dir" || die "failed to make directory: $scr_pic_dir"; } + + [ -d "$SCR_CACHE_DIR" ] || \ + { mkdir -p "$SCR_CACHE_DIR" || \ + die "failed to make directory: $SCR_CACHE_DIR"; } + + filename="$scr_rec_dir/$rec_filename" + + # Set wf-recorder arguments based on script options + [ "$microphone" ] && args="-a$aud_source" + [ "$desktop_audio" ] && args="-a$aud_sink" + + # If both microphone and desktop_audio is set, create a loopback devices pointing to + # scr_inputs. wf-record does not support multiple audio devices. This is how they + # recomend you record two devices at the same time. + [ "$microphone" ] && [ "$desktop_audio" ] && { + unload_pulse_modules=true + null_sink=$(pactl load-module module-null-sink sink_name=aud_both) + lb_desk=$(pactl load-module module-loopback sink=aud_both source="$aud_sink") + lb_mic=$(pactl load-module module-loopback sink=aud_both source="$aud_source") + args="-aaud_both.monitor" + } + + # Pressing Ctrl+C will exit the script instead of just wf-recorder. + # Intercept Ctrl+C and exit wf-recorder instead of the script + trap 'kill -2 $rec_pid' INT + + # Word splitting is favorable here + # shellcheck disable=SC2086 + wf-recorder $args -g "$(slurp)" -f "$filename" > "$SCR_CACHE_DIR/rec.log" 2>&1 & + rec_pid=$! + printf '%s' "Press Ctrl+C to stop recording. " 1>&2 + wait $rec_pid + + # Reset the trap + trap - INT + + # Clean up pulseaudio modules that the script created + [ "$unload_pulse_modules" ] && { + pactl unload-module "$lb_mic" + pactl unload-module "$lb_desk" + pactl unload-module "$null_sink" + } + + printf '\n%s\n' "$filename" +} + +$action diff --git a/thm b/scritps/thm similarity index 100% rename from thm rename to scritps/thm -- 2.51.2