literate-dotfiles/dotfiles/scripts.org

350 lines
8.4 KiB
Org Mode
Raw Normal View History

2019-05-16 21:13:08 +02:00
#+TITLE: Scripts
2020-01-11 22:04:28 +01:00
:DRAWER:
#+STARTUP: overview
#+LANGUAGE: en
#+EMAIL: dehaeze.thomas@gmail.com
#+AUTHOR: Dehaeze Thomas
#+HTML_LINK_HOME: ./index.html
#+HTML_LINK_UP: ./index.html
2020-01-11 22:24:51 +01:00
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="./css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="./css/readtheorg.css"/>
#+HTML_HEAD: <script type="text/javascript" src="./js/jquery.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/bootstrap.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/jquery.stickytableheaders.min.js"></script>
#+HTML_HEAD: <script type="text/javascript" src="./js/readtheorg.js"></script>
2020-03-27 15:38:04 +01:00
#+PROPERTY: header-args:bash :comments both :mkdirp yes
#+PROPERTY: header-args:bash+ :shebang "#!/usr/bin/env bash"
2020-01-11 22:04:28 +01:00
:END:
2019-05-16 21:13:08 +02:00
2020-03-01 21:25:11 +01:00
* LockScreen
2019-12-16 08:53:36 +01:00
:PROPERTIES:
:header-args: :tangle ~/scripts/lockscreen.sh
:END:
2019-05-16 21:13:08 +02:00
First, turn off dunst
#+begin_src bash
killall -SIGUSR1 dunst && echo "off" > /tmp/dunststatus;
#+end_src
Turn off the music if it is playing.
#+begin_src bash
MPC_STATE=$(mpc | sed -n '2p' | cut -d "[" -f2 | cut -d "]" -f1)
if [[ $MPC_STATE == "playing" ]]; then
mpc pause
fi
#+end_src
Then take a screenshot and process it.
2019-05-16 21:13:08 +02:00
#+begin_src bash
2019-06-22 17:33:42 +02:00
temp_file="/tmp/screen.png"
2019-12-15 10:51:42 +01:00
rm -f $temp_file
2019-12-30 17:22:22 +01:00
maim $temp_file
2019-06-22 17:33:42 +02:00
convert $temp_file -scale 10% -scale 1000% $temp_file
#+end_src
2019-06-22 17:33:42 +02:00
Finally, lock the screen using =i3lock=.
#+begin_src bash
2020-03-01 21:25:11 +01:00
i3lock --no-unlock-indicator --ignore-empty-password --nofork --image=$temp_file && killall -SIGUSR2 dunst && echo "on" > /tmp/dunststatus
2019-06-22 17:33:42 +02:00
#+end_src
#+begin_src bash :tangle no
2019-05-16 21:13:08 +02:00
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
2019-12-30 17:22:22 +01:00
# maim -d 1 $temp_file
2019-05-16 21:13:08 +02:00
# 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
#+end_src
2020-03-30 00:07:56 +02:00
* TODO Lock / Exit / Suspend / ...
2020-01-11 22:24:51 +01:00
:PROPERTIES:
2020-03-01 21:25:11 +01:00
:header-args: :tangle ~/scripts/quit.sh
2020-01-11 22:24:51 +01:00
:END:
2019-05-16 21:13:08 +02:00
2020-03-30 00:07:56 +02:00
- [ ] Seems duplicated with [[file:binaries.org::*=i3exit= - Manage lock, suspend, reboot, ...][=i3exit= - Manage lock, suspend, reboot, ...]]
2019-05-16 21:13:08 +02:00
#+begin_src bash
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
#+end_src
2020-03-01 21:25:11 +01:00
* NAS - Mount
2019-12-16 11:39:10 +01:00
:PROPERTIES:
:header-args: :tangle ~/scripts/nas.sh
:END:
2020-03-01 21:25:11 +01:00
2020-03-30 00:07:56 +02:00
As an alternative, =sshfs= can be used:
#+begin_src bash :tangle no
sshfs -o allow_other,default_permissions nas:/Data ~/mnt/NAS
#+end_src
2019-05-16 21:13:08 +02:00
#+begin_src bash
if [ $1 == "mount" ]; then
if mount /home/thomas/mnt/NAS/; then
2019-05-16 21:13:08 +02:00
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
2019-05-16 21:13:08 +02:00
dunstify --replace=58249 'NAS' 'Successfully unmounted'
else
dunstify --replace=58249 'NAS' 'Error while unmounted'
fi
fi
#+end_src
2020-03-27 15:38:04 +01:00
* Download Torrent
:PROPERTIES:
:header-args: :tangle ~/scripts/torrent-add.sh
:END:
#+begin_src bash
transmission-remote ***REMOVED***:9091 --auth tdehaeze:$(pass nas/transmission | sed -n 1p) -a $1 && \
dunstify 'Torrent' 'Successfully added' || \
dunstify 'Torrent' 'Error'
#+end_src
2019-05-16 21:13:08 +02:00
* Icons Unicode
2020-03-01 21:25:11 +01:00
:PROPERTIES:
:header-args: :tangle ~/scripts/icons.sh
:END:
2019-05-16 21:13:08 +02:00
#+begin_src bash
2020-03-01 21:25:11 +01:00
# The famous "get a menu of emojis to copy" script.
2019-05-16 21:13:08 +02:00
2020-03-01 21:25:11 +01:00
# Must have xclip installed to even show menu.
xclip -h 2>/dev/null || exit 1
2020-03-01 21:25:11 +01:00
chosen=$(cut -d ';' -f1 ~/.local/share/emoji | rofi -dmenu -i -l 20 | sed "s/ .*//")
2019-05-16 21:13:08 +02:00
2020-03-01 21:25:11 +01:00
[ "$chosen" != "" ] || exit
2019-05-30 16:22:37 +02:00
2020-03-01 21:25:11 +01:00
# If you run this command with an argument, it will automatically insert the character.
if [ -n "$1" ]; then
xdotool key Shift+Insert
else
echo "$chosen" | tr -d '\n' | xsel -ib
2020-03-22 19:42:54 +01:00
dunstify "'$chosen' copied to clipboard." &
2020-03-01 21:25:11 +01:00
fi
2019-05-30 16:22:37 +02:00
#+end_src
* Icons Nerd Font
:PROPERTIES:
:header-args: :tangle ~/scripts/nerd-fonts.sh
:END:
#+begin_src bash
# Must have xsel installed to even show menu.
xsel -h 2>/dev/null || exit 1
chosen=$(cat ~/.local/share/nerd-fonts | rofi -dmenu -i -l 20 | sed "s/ .*//")
[ "$chosen" != "" ] || exit
# If you run this command with an argument, it will automatically insert the character.
if [ -n "$1" ]; then
xdotool key Shift+Insert
else
echo "$chosen" | tr -d '\n' | xsel -ib
dunstify "'$chosen' copied to clipboard." &
fi
#+end_src
2019-12-15 10:51:42 +01:00
* Org-Protocol-Capture-HTML
:PROPERTIES:
:header-args: :tangle ~/scripts/org-protocol-capture-html.sh
:END:
Defaults
#+begin_src bash
heading="link"
template="pu"
url="https://google.com/"
#+end_src
Functions
#+begin_src bash
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=''))"
}
#+end_src
Documentation
#+begin_src bash
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
}
#+end_src
Arguments
#+begin_src bash
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[@]}"
#+end_src
Get HTML
#+begin_src bash
if [[ -n $@ ]]
then
debug "Text from args"
body="$@"
fi
#+end_src
URL-encode
#+begin_src bash
heading=$(urlencode <<<"$heading") || die "Unable to urlencode heading."
url=$(urlencode <<<"$url") || die "Unable to urlencode URL."
body=$(urlencode <<<"$body") || die "Unable to urlencode text."
#+end_src
Send to Emacs
#+begin_src bash
emacsclient "org-protocol://capture?template=$template&url=$url&title=$heading&body=$body"
#+end_src
2020-03-01 21:25:11 +01:00
* Restart Mopidy
2019-12-15 10:51:42 +01:00
:PROPERTIES:
2020-03-01 21:25:11 +01:00
:header-args: :tangle ~/scripts/mopidy-restart.sh
2019-12-15 10:51:42 +01:00
:END:
#+begin_src bash
2020-03-01 21:25:11 +01:00
pids=( $(pgrep -f mopidy) )
2019-12-15 10:51:42 +01:00
2020-03-01 21:25:11 +01:00
for pid in "${pids[@]}"; do
if [[ $pid != $$ ]]; then
kill "$pid"
fi
done
2019-12-15 10:51:42 +01:00
2020-03-01 21:25:11 +01:00
echo "Killed mopidy."
echo "Restarting mopidy..."
mopidy --config ~/.config/mopidy/mopidy.conf >/dev/null 2>&1 &
echo "Done"
2019-12-15 10:51:42 +01:00
#+end_src