diff --git a/screen.to.0x0 b/screen.to.0x0 index 3bacf8a..7d7bfc3 100755 --- a/screen.to.0x0 +++ b/screen.to.0x0 @@ -1,18 +1,194 @@ #!/bin/sh # The script will : {{{ -## Open scrot and allow the user to select the rectangle to capture -## Send the file to a remote file hosting +## Open a capture tool (flameshot or scrot) and +## allow the user to select the rectangle to capture and +## Send the file to a remote file hosting. +## ## Require the script https://git.101010.fr/gardouille-dotfiles/scripts/src/branch/master/send.to.0x0.sh # }}} -# Try to make a screen capture with scrot -# --select : Interactively select a window or rectangle with the mouse. -# --multidisp : For multiple heads -# --exec : Execute some commands -## Move the image to /tmp -## Send the absolut path of the image file to clipboard -scrot --select --multidisp --exec 'mv $f /tmp ; echo "/tmp/$f" | xclip -rmlastnl -selection clipboard' +# Vars {{{ +readonly PROGNAME=$(basename "${0}") +readonly NBARGS="${#}" +## Test if DEBUG is already defined (inline, environment variable,…) +[ -z "${DEBUG}" ] && DEBUG=1 + +## Colors +readonly RED='\033[0;31m' +readonly RESET='\033[0m' +readonly COLOR_DEBUG="${PURPLE}" + +DELAYED_MODE="1" +# }}} +usage() { # {{{ + + cat <<- EOF +usage: $PROGNAME [--debug,--help] [--delay INT_DELAY_SECONDS] [INT_DELAY_SECONDS] + +Try to get a screen capture with available tool : + 1. Flameshot (see : https://github.com/flameshot-org/flameshot) + 2. Scrot +And send it to a remote file hosting. + +EXAMPLES : + - Select the area, on the screen, to capture + ${PROGNAME} + + - Wait 5 seconds before launching the capture tool + ${PROGNAME} 5 + ${PROGNAME} --delay 5 + +OPTIONS : + --debug + Enable debug messages. + + -d,--delay INT_DELAY_SECONDS + Wait INT_DELAY_SECONDS before launching capture tool + + --help + Print this help message. + +EOF + +} +# }}} +debug_message() { # {{{ + + local_debug_message="${1}" + + ## Print message if DEBUG is enable (=0) + [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}" + + return 0 +} +# }}} + +# Manage arguments # {{{ + +## If there is argument(s) +if [ ! "${NBARGS}" -eq "0" ]; then + + manage_arg="0" + + ## If the first argument is not an option {{{ + ## Use it to delay screenshot + if ! printf -- '%s' "${1}" | grep -q -E -- "^-+"; + then + ## Use this argument for delay_screenshot (in seconds) + delay_screenshot="${1}" + ## Enable DELAYED screenshot mode + DELAYED_MODE="0" + + ## Switch to the next argument + shift + manage_arg=$((manage_arg+1)) + fi + ## }}} + # Parse all options (start with a "-") one by one + while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do + + case "${1}" in + --debug ) ## Enable debug mode + ## Enable DEBUG + DEBUG="0" + ;; + -d|--delay ) ## Add a delay + ## Enable DELAYED screenshot mode + DELAYED_MODE="0" + ## Move to the next argument + shift + ## Define the delay with this argument (in seconds) + delay_screenshot="${1}" + ;; + -h|--help ) ## help + usage + ## Exit after help informations + exit 0 + ;; + * ) ## unknow option + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + exit 1 + ;; + esac + + debug_message "Arguments management − \ +${RED}${1}${COLOR_DEBUG} option managed." + + ## Move to the next argument + shift + manage_arg=$((manage_arg+1)) + + done + + debug_message "Arguments management − \ +${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed." +else + debug_message "Arguments management − \ +No arguments/options to manage." +fi + +# }}} + +## Use Flameshot if available {{{ +if [ "$(command -v flameshot)" ]; then + ### Take a capture {{{ + if [ "${DELAYED_MODE}" -eq "0" ]; then + debug_message "Flameshot tool − \ +DELAYED_MODE enabled." + ## Convert delay screenshot to milliseconds + delay_screenshot="$(( ${delay_screenshot} * 1000 ))" + ## Open flameshot after "${delay_screenshot}" seconds + flameshot gui --path /tmp --delay "${delay_screenshot}" + else + debug_message "Flameshot tool − \ +Capture with gui." + ## Open flameshot + flameshot gui --path /tmp --delay 50 + fi + ### }}} + + ### Get full path of the image file of the capture + ### If the capture was successfully saved + if find /tmp -maxdepth 1 -mmin -1 -iname "*.png" > /dev/null 2>&1; then + ### Get the absolut path of the most recent image + img_absolut_path="$(find /tmp -maxdepth 1 -mmin -1 -iname "*.png" | sort | tail -n 1)" + debug_message "Flameshot tool − \ +Absolut path of the most recent image in /tmp: ${img_absolut_path}." + else + debug_message "Flameshot tool − \ +No recent image in /tmp." + fi + echo "${img_absolut_path}" | xclip -rmlastnl -selection clipboard +## }}} +else ## Use Scrot {{{ + debug_message "Scrot tool − \ +You might consider installing `flameshot`." + ## --delay : Wait NUM seconds before taking a shot. + ## --select : Interactively select a window or rectangle with the mouse. + ## --multidisp : For multiple heads, grab shot from each and join them together. + ## --exec : Directly open the screenshot with Gimp then delete it + + ## After successful capture, scrot will : + ### Move the image to /tmp + ### Send the absolut path of the image file to clipboard + if [ "${DELAYED_MODE}" -eq "0" ]; then + debug_message "Scrot tool − \ +DELAYED_MODE enabled." + ## Take a full screenshot because + ### mouse couldn't be grab if already used in another app + scrot --delay "${delay_screenshot}" --multidisp --exec 'gimp $f ; mv $f /tmp/' + else + debug_message "Scrot tool − \ +Capture with select." + scrot --select --multidisp --exec 'gimp $f ; mv $f /tmp/' + scrot --select --multidisp --exec 'mv $f /tmp ; echo "/tmp/$f" | xclip -rmlastnl -selection clipboard' + fi + +fi +## }}} # Call a script to automatically send the content of clipboard to a remote file hosting ~/bin/send.to.0x0.sh