literate-dotfiles/dotfiles/scripts.org

7.3 KiB

Scripts

LockScreen

First, turn off dunst

  killall -SIGUSR1 dunst && echo "off" > /tmp/dunststatus;

Turn off the music if it is playing.

  MPC_STATE=$(mpc | sed -n '2p' | cut -d "[" -f2 | cut -d "]" -f1)
  if [[ $MPC_STATE == "playing" ]]; then
    mpc pause
  fi

Then take a screenshot and process it.

  temp_file="/tmp/screen.png"

  rm -f $temp_file

  maim $temp_file
  convert $temp_file -scale 10% -scale 1000% $temp_file

Finally, lock the screen using i3lock.

  i3lock --no-unlock-indicator --ignore-empty-password --nofork --image=$temp_file && killall -SIGUSR2 dunst && echo "on" > /tmp/dunststatus
  revert() {
      xset dpms 0 0 0
  }
  trap revert HUP INT TERM
  # turn off screen after 5 seconds
  xset +dpms dpms 5 5 5

  # Parameters
  temp_file="/tmp/screen.png"
  icon="$HOME/Pictures/Evil_Rick_Sprite.png"
  width=1920
  height=1080
  blur_factor=6
  lock_blur_factor=0

  # Take the screen shot, blur the image and add the icon
  ffmpeg -f x11grab -video_size "${width}x${height}" -y -i $DISPLAY -i $icon -filter_complex "boxblur=$blur_factor:$blur_factor,overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2,boxblur=$lock_blur_factor:$lock_blur_factor" -vframes 1 $temp_file

  # Alternative
  # maim -d 1 $temp_file
  # convert -blur 0x8 $temp_file $temp_file
  # convert -composite $temp_file $icon -gravity South -geometry -20x1200 $temp_file

  # Lock the screen with the image
  i3lock --no-unlock-indicator --ignore-empty-password --show-failed-attempts --nofork --image=$temp_file

  # Remove the screenshot
  rm $temp_file

  # Don't turn off screen when back from lock
  revert

TODO Lock / Exit / Suspend / …

  • Seems duplicated with i3exit - Manage lock, suspend, reboot, …
  option=$(echo -e "Lock\nExit\nLogout\nSuspend\nHibernate\nReboot\nShutdown" | rofi -i -dmenu)

  case "$option" in
      "Lock")
          i3exit lock ;;
      "Exit")
          i3exit switch_user ;;
      "Logout")
          i3exit logout ;;
      "Suspend")
          i3exit suspend ;;
      "Hibernate")
          i3exit hibernate ;;
      "Reboot")
          i3exit reboot ;;
      "Shutdown")
          i3exit shutdown ;;
  esac

NAS - Mount

As an alternative, sshfs can be used:

  sshfs -o allow_other,default_permissions nas:/Data ~/mnt/NAS
  if [ $1 == "mount" ]; then
      if mount /home/thomas/mnt/NAS/; then
          dunstify --replace=58249 'NAS' 'Successfully mounted'
      else
          dunstify --replace=58249 'NAS' 'Error while mounted'
      fi
  elif [ $1 == "umount" ]; then
      if umount /home/thomas/mnt/NAS/; then
          dunstify --replace=58249 'NAS' 'Successfully unmounted'
      else
          dunstify --replace=58249 'NAS' 'Error while unmounted'
      fi
  fi

Download Torrent

  transmission-remote ***REMOVED***:9091 --auth tdehaeze:$(pass nas/transmission | sed -n 1p) -a $1 && \
      dunstify 'Torrent' 'Successfully added' || \
        dunstify 'Torrent' 'Error'

Org-Protocol-Capture-HTML

Defaults

  heading="link"
  template="pu"
  url="https://google.com/"

Functions

  function debug {
      if [[ -n $debug ]]
      then
          function debug {
              echo "DEBUG: $@" >&2
          }
          debug "$@"
      else
          function debug {
              true
          }
      fi
  }
  function die {
      echo "$@" >&2
      exit 1
  }
  function urlencode {
      python -c "
  from __future__ import print_function
  try:
      from urllib import quote  # Python 2
  except ImportError:
      from urllib.parse import quote  # Python 3
  import sys
  print(quote(sys.stdin.read()[:-1], safe=''))"
  }

Documentation

  function usage {
      cat <<EOF
  $0 [OPTIONS] [HTML]
  html | $0 [OPTIONS]
  Send HTML to Emacs through org-protocol, passing it through Pandoc to
  convert HTML to Org-mode.  HTML may be passed as an argument or
  through STDIN.  If only URL is given, it will be downloaded and its
  contents used.
  Options:
      -h, --heading HEADING     Heading
      -t, --template TEMPLATE   org-capture template key (default: pu)
      -u, --url URL             URL
      --debug  Print debug info
      --help   I need somebody!
  EOF
  }

Arguments

  args=$(getopt -n "$0" -o dh:rt:u: -l debug,help,heading:,template:,url: -- "$@") \
      || die "Unable to parse args.  Is getopt installed?"
  eval set -- "$args"

  while true
  do
      case "$1" in
          -d|--debug)
              debug=true
              debug "Debugging on"
              ;;
          --help)
              usage
              exit
              ;;
          -h|--heading)
              shift
              heading="$1"
              ;;
          -t|--template)
              shift
              template="$1"
              ;;
          -u|--url)
              shift
              url="$1"
              ;;
          --)
              # Remaining args
              shift
              rest=("$@")
              break
              ;;
      esac

      shift
  done

  debug "ARGS: $args"
  debug "Remaining args: ${rest[@]}"

Get HTML

  if [[ -n $@ ]]
  then
      debug "Text from args"

      body="$@"
  fi

URL-encode

  heading=$(urlencode <<<"$heading") || die "Unable to urlencode heading."
  url=$(urlencode <<<"$url") || die "Unable to urlencode URL."
  body=$(urlencode <<<"$body") || die "Unable to urlencode text."

Send to Emacs

  emacsclient "org-protocol://capture?template=$template&url=$url&title=$heading&body=$body"

Restart Mopidy

  pids=( $(pgrep -f mopidy) )

  for pid in "${pids[@]}"; do
      if [[ $pid != $$ ]]; then
          kill "$pid"
      fi
  done

  echo "Killed mopidy."
  echo "Restarting mopidy..."
  mopidy --config ~/.config/mopidy/mopidy.conf >/dev/null 2>&1 &
  echo "Done"