literate-dotfiles/binaries.org

1187 lines
33 KiB
Org Mode
Raw Normal View History

2019-05-16 21:13:08 +02:00
#+TITLE: Binaries
2021-01-01 20:12:34 +01:00
#+SETUPFILE: ./setup/org-setup-file.org
2019-05-16 21:13:08 +02:00
2020-04-05 15:39:08 +02:00
#+PROPERTY: header-args:bash :comments both :mkdirp yes
#+PROPERTY: header-args:bash+ :shebang "#!/usr/bin/env bash"
2019-05-16 21:13:08 +02:00
* =dmenumount= - Mount USB and Android
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: dmenumount
:END:
Script taken from Luke Smith.
#+begin_src bash :tangle ~/.local/bin/dmenumount
getmount() { \
[ -z "$chosen" ] && exit 1
# shellcheck disable=SC2086
mp="$(find $1 2>/dev/null | dmenu -i -p "Type in mount point.")" || exit 1
[ "$mp" = "" ] && exit 1
if [ ! -d "$mp" ]; then
mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?") || exit 1
[ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp")
fi
}
mountusb() { \
chosen="$(echo "$usbdrives" | dmenu -i -p "Mount which drive?")" || exit 1
chosen="$(echo "$chosen" | awk '{print $1}')"
sudo -A mount "$chosen" 2>/dev/null && notify-send "💻 USB mounting" "$chosen mounted." && exit 0
alreadymounted=$(lsblk -nrpo "name,type,mountpoint" | awk '$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not ( -path *%s -prune ) ",$3}')
getmount "/mnt /media /mount /home -maxdepth 5 -type d $alreadymounted"
partitiontype="$(lsblk -no "fstype" "$chosen")"
case "$partitiontype" in
"vfat") sudo -A mount -t vfat "$chosen" "$mp" -o rw,umask=0000;;
,*) sudo -A mount "$chosen" "$mp"; user="$(whoami)"; ug="$(groups | awk '{print $1}')"; sudo -A chown "$user":"$ug" "$mp";;
esac
notify-send "💻 USB mounting" "$chosen mounted to $mp."
}
mountandroid() { \
chosen="$(echo "$anddrives" | dmenu -i -p "Which Android device?")" || exit 1
chosen="$(echo "$chosen" | cut -d : -f 1)"
getmount "$HOME -maxdepth 3 -type d"
simple-mtpfs --device "$chosen" "$mp"
echo "OK" | dmenu -i -p "Tap Allow on your phone if it asks for permission and then press enter" || exit 1
simple-mtpfs --device "$chosen" "$mp"
notify-send "🤖 Android Mounting" "Android device mounted to $mp."
}
asktype() { \
choice="$(printf "USB\\nAndroid" | dmenu -i -p "Mount a USB drive or Android device?")" || exit 1
case $choice in
USB) mountusb ;;
Android) mountandroid ;;
esac
}
anddrives=$(simple-mtpfs -l 2>/dev/null)
usbdrives="$(lsblk -rpo "name,type,size,mountpoint" | awk '$4==""{printf "%s (%s)\n",$1,$3}')"
if [ -z "$usbdrives" ]; then
[ -z "$anddrives" ] && echo "No USB drive or Android device detected" && exit
echo "Android device(s) detected."
mountandroid
else
if [ -z "$anddrives" ]; then
echo "USB drive(s) detected."
mountusb
else
echo "Mountable USB drive(s) and Android device(s) detected."
asktype
fi
fi
#+end_src
* =dmenuumount= - Unmount USB and Android devices
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: dmenuumount
:END:
Script taken from Luke Smith.
#+begin_src bash :tangle ~/.local/bin/dmenuumount
unmountusb() {
[ -z "$drives" ] && exit
chosen="$(echo "$drives" | dmenu -i -p "Unmount which drive?")" || exit 1
chosen="$(echo "$chosen" | awk '{print $1}')"
[ -z "$chosen" ] && exit
sudo -A umount "$chosen" && notify-send "💻 USB unmounting" "$chosen unmounted."
}
unmountandroid() { \
chosen="$(awk '/simple-mtpfs/ {print $2}' /etc/mtab | dmenu -i -p "Unmount which device?")" || exit 1
[ -z "$chosen" ] && exit
sudo -A umount -l "$chosen" && notify-send "🤖 Android unmounting" "$chosen unmounted."
}
asktype() { \
choice="$(printf "USB\\nAndroid" | dmenu -i -p "Unmount a USB drive or Android device?")" || exit 1
case "$choice" in
USB) unmountusb ;;
Android) unmountandroid ;;
esac
}
drives=$(lsblk -nrpo "name,type,size,mountpoint" | awk '$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "%s (%s)\n",$4,$3}')
2019-05-16 21:13:08 +02:00
if ! grep simple-mtpfs /etc/mtab; then
[ -z "$drives" ] && echo "No drives to unmount." && exit
echo "Unmountable USB drive detected."
unmountusb
else
if [ -z "$drives" ]
then
echo "Unmountable Android device detected."
unmountandroid
else
echo "Unmountable USB drive(s) and Android device(s) detected."
asktype
fi
fi
#+end_src
2021-01-01 20:12:34 +01:00
* =nordvpn-toggle= - Connect to VPN using NordVPN
:PROPERTIES:
:CUSTOM_ID: nordvpn-toggle
:END:
To use this this, =nordvpn= must be installed: =yay -S nordvpn-bin=.
#+begin_src bash :tangle ~/.local/bin/nordvpn-toggle
tmpfile="/tmp/vpnstatus";
2019-05-16 21:13:08 +02:00
if [[ $(nordvpn status) == *"Connected"* ]]; then
2021-01-01 20:12:34 +01:00
nordvpn disconnect && \
dunstify --replace=23198 "VPN" "Disconnected" && \
echo "off" > $tmpfile;
2019-05-16 21:13:08 +02:00
else
2021-01-01 20:12:34 +01:00
# Select Country to connect to
country=`cat ~/.local/data/nordvpn_countries.txt | rofi -i -dmenu | sed 's/\s/_/g'`;
dunstify --replace=23198 "VPN" "Connecting to $country...";
nordvpn connect $country && \
dunstify --replace=23198 "VPN" "Connected to $country" && \
echo "on" > $tmpfile;
2019-05-16 21:13:08 +02:00
fi
#+end_src
2020-03-28 11:10:14 +01:00
* =bukurun= - Open link from Buku
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: bukurun
:END:
Script taken from [[https://github.com/carnager/buku_run][here]] that acts as a buku frontend using Rofi.
2019-05-16 21:13:08 +02:00
#+begin_src bash :tangle ~/.local/bin/bukurun
2020-05-26 08:33:31 +02:00
_rofi () {
rofi -dmenu -i -no-levenshtein-sort -width 1000 "$@"
}
# display settings
display_type=1
max_str_width=80
2019-05-16 21:13:08 +02:00
2020-05-26 08:33:31 +02:00
# keybindings
switch_view="Alt+Tab"
new_bookmark="Alt+n"
actions="Alt+a"
edit="Alt+e"
delete="Alt+d"
# colors
help_color="#334433"
# source global config
if [[ -f /etc/buku_run.config ]]
2019-05-16 21:13:08 +02:00
then
2020-05-26 08:33:31 +02:00
source /etc/buku_run.config
2019-05-16 21:13:08 +02:00
fi
2020-05-26 08:33:31 +02:00
# source local config:
if [[ -z $XDG_CONFIG_DIR ]]
2019-05-16 21:13:08 +02:00
then
2020-05-26 08:33:31 +02:00
if [[ -f $HOME/.config/buku_run/config ]]
then
source $HOME/.config/buku_run/config
else
echo "User config file \'~/.config/buku_run/config\' not found. using global config"
fi
2019-05-16 21:13:08 +02:00
else
2020-05-26 08:33:31 +02:00
if [[ -f "${XDG_CONFIG_DIR}/buku_run/config" ]]
then
source "${XDG_CONFIG_DIR}/buku_run/config"
else
echo "User config file \'~/.config/buku_run/config\' not found. using global config"
fi
2019-05-16 21:13:08 +02:00
fi
2020-05-26 08:33:31 +02:00
main () {
HELP="Welcome to Buku. Use <span color='${help_color}'>${new_bookmark}</span> to add a new Bookmark
Use <span color='${help_color}'>${switch_view}</span> to switch View. <span color='${help_color}'>${actions}</span> for actions"
if [[ $mode == "bookmarks" ]]; then
content=$(parseBuku)
menu=$(echo "${content}" | _rofi -p '> ' -filter "${filter}" -mesg "${HELP}" -kb-custom-1 "${new_bookmark}" -kb-custom-2 "${switch_view}" -kb-custom-3 "${actions}" -kb-custom-4 "${edit}" -kb-custom-5 "${delete}")
elif [[ $mode == "tags" ]]; then
menu=$(buku --np --st | awk '{$NF=""; print $0}' | cut -d ' ' -f2- | _rofi -p '> ' -mesg "${HELP}" -kb-custom-1 "${new_bookmark}" -kb-custom-2 "${switch_view}" -kb-custom-3 "${actions}" -kb-custom-4 "${edit}" -kb-custom-5 "${delete}")
2019-05-16 21:13:08 +02:00
fi
2020-05-26 08:33:31 +02:00
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 12 ]]; then
optionsMenu
elif [[ $val -eq 10 ]]; then
addMark
elif [[ $val -eq 14 ]]; then
deleteMenu
elif [[ $val -eq 13 ]]; then
editMenu
elif [[ $val -eq 11 ]]; then
if [[ $mode == "bookmarks" ]]; then
export mode="tags"
mode=tags main
elif [[ $mode == "tags" ]]; then
export mode="bookmarks"
mode=bookmarks main
fi
elif [[ $val -eq 0 ]]; then
if [[ $mode == "bookmarks" ]]; then
id=$(getId "$content" "$menu")
for bm in ${id}; do
buku -o "${bm}"
done
elif [[ $mode == "tags" ]]; then
filter="${menu}" mode="bookmarks" main
fi
fi
}
2019-05-16 21:13:08 +02:00
2020-05-26 08:33:31 +02:00
optionsMenu () {
if [[ $mode == "bookmarks" ]]; then
askmenu=$(echo -e "< Return\n---\n1. Edit\n2. Delete" | _rofi -p '> ' -mesg "Choose Action for bookmark")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $askmenu == "< Return" ]]; then
export mode=bookmarks
main
elif [[ $askmenu == "1. Edit" ]]; then
editMenu
elif [[ $askmenu == "2. Delete" ]]; then
deleteMenu
fi
fi
elif [[ $mode == "tags" ]]; then
askmenu=$(echo -e "< Return\n---\n1. Replace Tag\n2. Delete Tag" | _rofi -p '> ' -mesg "Choose Action for tag \"${menu}\"")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $askmenu == "< Return" ]]; then
export mode=tags
main
elif [[ $askmenu == "1. Replace Tag" ]]; then
newtag=$(echo | _rofi -p '> ' -mesg "Enter new tag name for tag \"${menu}\"")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $newtag == "" ]]; then
mode=tags main
else
buku -r "${menu}" "${newtag}"
mode=tags main
fi
fi
elif [[ $askmenu == "2. Delete Tag" ]]; then
delask=$(echo -e "1. Yes\n2. No" | _rofi -p '> ' -mesg "Really delete tag?")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $delask == "1. Yes" ]]; then
buku -r "${menu}"
mode=tags main
elif [[ $delask == "2. No" ]]; then
mode=tags main
fi
fi
fi
fi
fi
2019-05-16 21:13:08 +02:00
}
2020-05-26 08:33:31 +02:00
deleteMenu () {
id=$(getId "$content" "$menu")
delask=$(echo -e "1. Yes\n2. No" | _rofi -p '> ' -mesg "Really delete bookmark?")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $delask == "1. Yes" ]]; then
buku -d ${id} --tacit
mode=bookmarks main
elif [[ $delask == "2. No" ]]; then
optionsMenu
fi
fi
2019-05-16 21:13:08 +02:00
}
2020-05-26 08:33:31 +02:00
editMenu () {
id=$(getId "$content" "$menu")
title="$(getTitleFromId $id)"
bookmark="$(getUrlFromId $id)"
tags="$(getTagsFromId $id)"
content=$(echo -e "1. title: $title\n2. url: $bookmark\n3. tags: $tags")
editmenu=$(echo -e "< Return\n---\n${content}" | _rofi -p '> ')
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $editmenu == "< Return" ]]; then
main
elif [[ $editmenu == "tags: NOTAG" ]]; then
addTags --update
elif [[ $editmenu =~ tags:* ]]; then
tags="${tags}" editTags
elif [[ $editmenu =~ url:* ]]; then
editBookmark
elif [[ $editmenu =~ title:* ]]; then
editTitle
fi
fi
2019-05-16 21:13:08 +02:00
}
2020-05-26 08:33:31 +02:00
editTags () {
edittagsmenu=$(echo | _rofi -filter "${tags}" -p '> ' -mesg "Edit Tags and hit Enter")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
buku -u ${id} --tag "${edittagsmenu}"
fi
mode=bookmarks main
}
editBookmark () {
bmarkmenu=$(echo "" | _rofi -p "> " -filter "${bookmark}" -mesg "Edit Bookmark and hit Enter")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $bmarkmenu == "http"* ]]; then
buku -u "${id}" --url "${bmarkmenu}"
else
echo "" | rofi -e "Not a valid URI, Make sure URLs start with http"
editBookmark
fi
fi
mode=bookmarks main
}
editTitle () {
titlemenu=$(echo "" | _rofi -p "> " -filter "${title}" -mesg "Edit Title and hit Enter")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
buku -u "${id}" --title "${titlemenu}"
fi
mode=bookmarks main
}
addMark () {
inserturl=$(echo -e "$(xclip -o)" | _rofi -p '> ' -mesg "Use URL below or type manually")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
addTags
fi
2019-05-16 21:13:08 +02:00
}
2020-05-26 08:33:31 +02:00
addTags () {
inserttags=$(buku --np --st | awk '{$NF=""; print $0}' | cut -d ' ' -f2- | _rofi -p '> ' -mesg "Add some tags. Separate tags with ', '")
val=$?
if [[ $val -eq 1 ]]; then
exit
elif [[ $val -eq 0 ]]; then
if [[ $(echo "${inserttags}" | wc -l) -gt 1 ]]; then
taglist=$(echo "${inserttags}" | tr '\n' ',')
tags=()
for tag in $taglist; do
tags+=("$tag")
done
else
tags=${inserttags}
fi
if [[ $1 == "--update" ]]; then
buku -u "${id}" --tag ${tags}
else
buku -a ${inserturl} ${tags}
fi
fi
}
parseBuku () {
echo "$(buku --nc -p | gawk -v max="$max_str_width" -v type="$display_type" '
2019-05-16 21:13:08 +02:00
BEGIN {
RS=""
FS="\n"
}
{
2020-05-26 08:33:31 +02:00
if ($3 == "")
$3 = " # NOTAG"
id = gensub(/([0-9]+)\.(.*)/, "\\1", "g", $1)
url = substr(gensub(/\s+> (.*)/, "\\1", "g", $2),0,max)
tags = gensub(/\s+# (.*)/, "\\1", "g", $3)
title = substr(gensub(/[0-9]+\.\s*(.*)/, "\\1", "g", $1),0,max)
if (type == 1)
print id "\t" url "\t" tags
else
print id "\t" title "\t" tags
if (type == 3)
print " \t" url "\t "
print ""
}
' | column -t -s $'\t')"
}
getId () {
id=$(echo "${2%% *}")
if [ -z "$id" ]; then
prev=""
IFS=$'\n'
for line in $1; do
if [ "$2" = "$line" ]; then
id=$(echo "${prev%% *}")
break
else
prev="$line"
fi
done
fi
echo $id
}
getTitleFromId () {
echo "$(buku --nc -p $1 | gawk '
BEGIN {
RS=""
FS="\n"
}
{
print gensub(/[0-9]+\.\s*(.*)/, "\\1", "g", $1)
}
')"
}
getUrlFromId () {
echo "$(buku --nc -p $1 | gawk '
BEGIN {
RS=""
FS="\n"
}
{
print gensub(/\s+> (.*)/, "\\1", "g", $2)
}
')"
}
getTagsFromId () {
echo "$(buku --nc -p $1 | gawk '
BEGIN {
RS=""
FS="\n"
}
{
print gensub(/\s+# (.*)/, "\\1", "g", $3)
}
')"
2019-05-16 21:13:08 +02:00
}
2020-05-26 08:33:31 +02:00
mode=bookmarks main
2019-05-16 21:13:08 +02:00
#+end_src
2020-04-05 15:39:08 +02:00
2020-03-28 11:10:14 +01:00
* =i3exit= - Manage lock, suspend, reboot, ...
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: i3exit
:END:
2019-05-16 21:13:08 +02:00
#+begin_src bash :tangle ~/.local/bin/i3exit
2020-05-26 08:33:31 +02:00
option=$(echo -e "Lock\nExit\nLogout\nSuspend\nHibernate\nReboot\nShutdown" | rofi -i -dmenu)
case "$option" in
"Lock")
~/.local/bin/lockscreen
2019-05-16 21:13:08 +02:00
;;
2020-05-26 08:33:31 +02:00
"Exit")
2021-01-01 20:12:34 +01:00
pkill -15 -t tty"$XDG_VTNR" Xorg
2019-05-16 21:13:08 +02:00
;;
2020-05-26 08:33:31 +02:00
"Logout")
loginctl terminate-session `loginctl session-status | head -n 1 | awk '{print $1}'`
;;
"Suspend")
systemctl suspend && ~/.local/bin/lockscreen
2019-05-16 21:13:08 +02:00
;;
2020-05-26 08:33:31 +02:00
"Hibernate")
systemctl hibernate && ~/.local/bin/lockscreen
2019-05-16 21:13:08 +02:00
;;
2020-05-26 08:33:31 +02:00
"Reboot")
2020-11-03 11:39:23 +01:00
confirmation=$(echo -e "Yes\nNo" | rofi -i -p "Are you sure you want to Reboot?" -dmenu)
case "$confirmation" in
"Yes")
systemctl reboot
;;
esac
2019-05-16 21:13:08 +02:00
;;
2020-05-26 08:33:31 +02:00
"Shutdown")
2020-11-03 11:39:23 +01:00
confirmation=$(echo -e "Yes\nNo" | rofi -i -p "Are you sure you want to Shutdown?" -dmenu)
case "$confirmation" in
"Yes")
systemctl poweroff
;;
esac
2019-05-16 21:13:08 +02:00
;;
,*)
echo "== ! i3exit: missing or invalid argument ! =="
exit 2
esac
exit 0
#+end_src
2019-06-22 17:27:21 +02:00
2020-05-26 08:33:31 +02:00
* =askpass-rofi= - GUI prompt for passwords
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: askpass-rofi
:END:
2019-05-16 21:13:08 +02:00
Take password prompt from STDIN, print password to STDOUT.
The sed piece just removes the colon from the provided prompt: =rofi -p= already gives us a colon
2021-01-01 20:12:34 +01:00
#+BEGIN_SRC bash :tangle ~/.local/bin/askpass-rofi
2019-05-16 21:13:08 +02:00
rofi -dmenu \
-password \
-no-fixed-num-lines \
-p "$(printf "$1" | sed s/://)"
#+END_SRC
2019-06-22 17:27:21 +02:00
2020-03-28 11:10:14 +01:00
* =screenshot= - Take Screenshot
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: screenshot
:END:
2019-06-22 17:27:21 +02:00
#+begin_src bash :tangle ~/.local/bin/screenshot
2021-01-01 20:12:34 +01:00
# Ask for screenshot type
status=$(echo -e "All\nGUI\nSelection\nCropped\nCopy\nPretty\nShadow\nWindow" | rofi -i -dmenu -p "Type")
2019-06-22 17:27:21 +02:00
if [ -z "$status" ]; then
2021-01-01 20:12:34 +01:00
exit;
2019-06-22 17:27:21 +02:00
fi
2021-01-01 20:12:34 +01:00
# Ask for filename if not copying the image
if [[ $status != "Copy" ]]; then
name=$(echo -e "screenshot-$(date +"%m-%d-%y_%H-%M-%S")" | rofi -i -dmenu -p "Filename")
if [ -z "$name" ]; then
exit;
fi
filename=~/Pictures/$name.png
2019-06-22 17:27:21 +02:00
fi
case "$status" in
"All")
2021-01-01 20:12:34 +01:00
maim -u $filename ;;
2019-12-15 10:53:05 +01:00
"GUI")
2021-01-01 20:12:34 +01:00
flameshot gui -r > $filename && \
pkill flameshot;;
2019-06-22 17:27:21 +02:00
"Selection")
2021-01-01 20:12:34 +01:00
maim -u -s $filename ;;
2019-06-22 17:27:21 +02:00
"Cropped")
2021-01-01 20:12:34 +01:00
maim -u -s $filename && convert -trim $filename $filename ;;
2019-06-22 17:27:21 +02:00
"Copy")
2021-01-01 20:12:34 +01:00
maim -u -s | xclip -selection clipboard -t image/png ;;
2019-06-22 17:27:21 +02:00
"Shadow")
2021-01-01 20:12:34 +01:00
maim -u -s | convert - \( +clone -background black -shadow 80x3+5+5 \) +swap -background none -layers merge +repage $filename ;;
"Pretty")
maim -u -s $filename && \
convert $filename \( +clone -alpha extract -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \( +clone -flip \) -compose Multiply -composite \( +clone -flop \) -compose Multiply -composite \) -alpha off -compose CopyOpacity -composite $filename && \
convert $filename \( +clone -background black -shadow 40x5+0+0 \) +swap -background none -layers merge +repage $filename ;;
"Window")
maim -u -i $(xdotool selectwindow) $filename ;;
2019-06-22 17:27:21 +02:00
esac
#+end_src
2019-07-12 13:26:49 +02:00
2020-03-28 11:10:14 +01:00
* =network-toggle= - Toggle Network
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: network-toggle
:END:
Minimal network manager to just toggle the Wifi or Ethernet connection.
2019-07-12 13:26:49 +02:00
#+begin_src bash :tangle ~/.local/bin/network-toggle
2019-07-12 13:26:49 +02:00
result=$(nmcli device | sed '1d' | dmenu -l 20);
interface=$(echo $result | awk -F ' ' '{print $1}');
status=$(echo $result | awk -F ' ' '{print $3}');
if [ $status == 'disconnected' ]; then
nmcli device connect $interface
else
nmcli device disconnect $interface
fi
#+end_src
2021-01-01 20:12:34 +01:00
* =make-gif= - Convert an =MP4= video to =GIF=
:PROPERTIES:
:CUSTOM_ID: make-gif
:END:
First argument is the =mp4= file and the second argument is the output =gif= file.
2020-03-01 21:25:11 +01:00
#+begin_src bash :tangle ~/.local/bin/make-gif
2020-03-01 21:25:11 +01:00
palette="/tmp/palette.png"
filters="fps=15,scale=320:-1:flags=lanczos"
ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $2
#+end_src
2020-05-26 08:33:31 +02:00
2020-03-28 11:10:14 +01:00
* =yt-audio= - Download-Audio from youtube
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: yt-audio
:END:
2020-03-01 21:25:11 +01:00
#+begin_src bash :tangle ~/.local/bin/yt-audio
2020-03-01 21:25:11 +01:00
if [ $TMUX ]; then
2020-03-22 23:21:26 +01:00
tmux split -v -l 5 "cd ~/Downloads/ && youtube-dl --add-metadata -xic -f bestaudio/best $1" && tmux select-pane -U
2020-03-01 21:25:11 +01:00
else
cd ~/Downloads/;
setsid nohup youtube-dl --add-metadata -xic -f bestaudio/best $1 &> /dev/null &
fi
#+end_src
2020-03-28 11:10:14 +01:00
* =yt-video= - Download-Video from youtube
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: yt-video
:END:
2020-03-01 21:25:11 +01:00
#+begin_src bash :tangle ~/.local/bin/yt-video
2020-03-01 21:25:11 +01:00
if [ $TMUX ]; then
2020-03-22 23:21:26 +01:00
tmux split -v -l 5 "cd ~/Downloads/ && youtube-dl --add-metadata -ic $1" && tmux select-pane -U
2020-03-01 21:25:11 +01:00
else
cd ~/Downloads/;
setsid nohup youtube-dl --add-metadata -ic $1 &> /dev/null &
fi
#+end_src
* =setbg= - Set Background
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: setbg
:END:
First argument is either:
- the background file
- a directory, in such case it will pick a random picture file from that directory
#+begin_src bash :tangle ~/.local/bin/setbg
bgloc="${XDG_CACHE_HOME:-$HOME/.cache/}/bg"
2021-01-01 20:12:34 +01:00
# If the argument is a file
[ -f "$1" ] && ln -sf "$(readlink -f "$1")" "$bgloc"
2021-01-01 20:12:34 +01:00
# If the argument is a directory
[ -d "$1" ] && ln -sf "$(find "$(readlink -f "$1")" -iregex '.*.\(jpg\|jpeg\|png\|gif\)' -type f | shuf -n 1)" "$bgloc"
2021-01-01 20:12:34 +01:00
# Set the wallpaper
xwallpaper --zoom "$bgloc"
#+end_src
* =insert-unicode= - Insert Unicode Icon
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: insert-unicode
:END:
The list of emojis is available [[file:data.org::#emojis][here]].
#+begin_src bash :tangle ~/.local/bin/insert-unicode
# Must have xclip installed to even show menu.
xclip -h 2>/dev/null || exit 1
2020-05-26 08:33:31 +02:00
chosen=$(cut -d ';' -f1 ~/.local/data/emojis | 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
* =insert-nerd-fonts= - Insert Nerd Font Icon
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: insert-nerd-fonts
:END:
The list of emojis is available [[file:data.org::#nerd-fonts][here]].
#+begin_src bash :tangle ~/.local/bin/insert-nerd-fonts
# Must have xsel installed to even show menu.
xsel -h 2>/dev/null || exit 1
chosen=$(cat ~/.local/data/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
* =linkhandler= - Open with Default application
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: linkhandler
:END:
Inspired from =linkhandler= script ([[https://github.com/LukeSmithxyz/voidrice/][github]]).
This is used to open any type of file with the wanted program.
It can be used in =newsboat=, =neomutt= and =ranger= for instance.
#+begin_src bash :tangle ~/.local/bin/linkhandler
[ -z "$1" ] && { "$BROWSER"; exit; }
case "$1" in
2021-01-01 20:12:34 +01:00
,*mkv|*webm|*mp4|*youtube.com/watch*|*youtube.com/playlist*|*youtu.be*)
setsid mpv --input-ipc-server=/tmp/mpvsoc$(date +%s) -quiet "$1" >/dev/null 2>&1 & ;;
,*png|*jpg|*jpe|*jpeg|*gif)
curl -sL "$1" > "/tmp/$(echo "$1" | sed "s/.*\///")" && sxiv -a "/tmp/$(echo "$1" | sed "s/.*\///")" >/dev/null 2>&1 & ;;
,*mp3|*flac|*opus|*mp3?source*)
2021-01-01 20:12:34 +01:00
setsid curl -LO "$1" >/dev/null 2>&1 & ;;
,*)
if [ -f "$1" ]; then "$TERMINAL" -e "$EDITOR $1"
else setsid $BROWSER "$1" >/dev/null 2>&1 & fi ;;
esac
#+end_src
2021-01-01 20:12:34 +01:00
* =lockscreen= - Lock Screen
:PROPERTIES:
2021-01-01 20:12:34 +01:00
:CUSTOM_ID: lockscreen
:header-args: :tangle ~/.local/bin/lockscreen
:END:
2021-01-01 20:12:34 +01:00
A nice lockscreen that uses =i3lock=.
It takes a screenshot, pixelize it and overlay an image in the lockscreens folder.
#+begin_src bash
2021-01-01 20:12:34 +01:00
# First, turn off dunst
killall -SIGUSR1 dunst && echo "off" > /tmp/dunststatus;
2021-01-01 20:12:34 +01:00
# 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
2021-01-01 20:12:34 +01:00
mpc pause
fi
2021-01-01 20:12:34 +01:00
# Take the screenshot and process it nicely
temp_file="/tmp/screen.png"
rm -f $temp_file
2021-01-01 20:12:34 +01:00
maim $temp_file && \
# Pixelize the Screenshot
convert $temp_file -scale 10% -scale 1000% $temp_file && \
# Overlay a random image in the lockscreens folder
composite -gravity center $(find ~/.local/data/lockscreens/ -type f | shuf -n 1) $temp_file $temp_file;
# Finally, lock the screen using =i3lock=
i3lock --ignore-empty-password --nofork --image=$temp_file && \
# When unlocking, restart dunst
killall -SIGUSR2 dunst && echo "on" > /tmp/dunststatus
#+end_src
2021-01-01 20:12:34 +01:00
* =mopidy-restart= - Restart Mopidy
:PROPERTIES:
:CUSTOM_ID: mopidy-restart
:END:
2021-01-01 20:12:34 +01:00
Sometimes =mopidy= need to be restarted...
#+begin_src bash :tangle ~/.local/bin/mopidy-restart
pids=( $(pgrep -f mopidy) )
for pid in "${pids[@]}"; do
if [[ $pid != $$ ]]; then
kill "$pid"
fi
done
echo "Killed mopidy."
2021-01-01 20:12:34 +01:00
echo "Restarting mopidy..."
mopidy --config ~/.config/mopidy/mopidy.conf >/dev/null 2>&1 &
echo "Done"
#+end_src
2020-05-26 08:33:31 +02:00
* =upload= - Upload Script
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: upload
:END:
Upload a file to https://0x0.st/ and copy the generated url.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/upload
if [ $TMUX ]; then
tmux split -v -l 1 "curl --progress-bar -F\"file=@$1\" https://0x0.st | xsel -ib;" && tmux select-pane -U
else
curl --progress-bar -F"file=@$1" https://0x0.st | xsel -ib && \
dunstify 'Upload' 'Successful' || \
dunstify --urgency=critical 'Upload' 'Failed'
fi
#+end_src
* =weather= - Display Weather in terminal
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: weather
:END:
Get the weather from http://wttr.in/.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/weather
if [ -n "$*" ]; then
address="wttr.in/"
address+=$*
else
address="wttr.in/"
fi
if type sxiv > /dev/null 2>&1; then
address+=".png"
wget -qO- "$address" > /tmp/weather.png && \
sxiv -b /tmp/weather.png
elif type feh > /dev/null 2>&1; then
address+=".png"
wget -qO- "$address" | feh -
else
curl "$address"
fi
#+end_src
2020-11-03 11:39:35 +01:00
* =readbib= - Open Bibliography File
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: readbib
:END:
List all =pdf= files and open selected one with zathura.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/readbib
cd ~/Cloud/pdfs/ && ls | rofi -dmenu -lines 20 | xargs -I {} zathura {}
#+end_src
* =pdf2bib= - Extract bibtex entry from PDF file
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf2bib
:END:
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf2bib
pdf2doi () {
pdfinfo "$1" | grep -io "doi:.*" | grep -Poi "10.\d+/[^\s]+" || \
pdftotext "$1" 2>/dev/null - | grep -io "doi:.*" -m 1 | grep -Poi "10.\d+/[^\s]+" || \
pdftotext "$1" 2>/dev/null - | grep -Poi "10.\d+/[^\s]+"
}
doi2bib () {
# curl -LHs "Accept: application/x-bibtex" http://dx.doi.org/$1 -w "\\n"
curl -s "http://api.crossref.org/works/$1/transform/application/x-bibtex" -w "\\n"
}
# If a file is specified, try to extract DOI from the file
if [ -f "$1" ]; then
doi=$(pdf2doi "$1")
fi
if hash rofi 2>/dev/null; then
doi=$(echo "$doi" | rofi -i -dmenu -p "DOI")
else
doi=$(echo "$doi" | dmenu -i -p "DOI")
fi
if [ -n "$1" ]; then
doi2bib $doi | xsel -ib && \
dunstify 'BibTeX' 'Copied to Clipboard' || \
dunstify --urgency=critical 'BibTeX' 'Failed'
fi
#+end_src
* =pdf2png= - Convert a PDF to PNG
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf2png
:END:
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf2png
2021-01-01 20:12:34 +01:00
# Check if the input argumetn is a PDF file
2020-11-03 11:39:35 +01:00
if [[ -f "$1" && "$1" == *.pdf ]]; then
pdftoppm -png "$1" > "$(echo "$1" | cut -f 1 -d '.' | sed 's/$/.png/')"
2020-05-26 08:33:31 +02:00
fi
#+end_src
2021-01-01 20:12:34 +01:00
* =convert-file= - Convert any file to another filetype
:PROPERTIES:
:CUSTOM_ID: convert-file
:END:
#+begin_src bash :tangle ~/.local/bin/convert-file
# Get filename
filename_with_extension=$(basename -- "$1")
# Extract extension of the file
in_ext="${filename_with_extension##*.}"
# filename without extension
filename_without_extension=${filename_with_extension%.*}
# Convert SVG Files
svg2() { \
out_ext=$(echo -e "pdf\npng" | rofi -i -dmenu -p "Convert SVG to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"png")
inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension"
;;
"pdf")
inkscape --export-type="$out_ext" "$filename_with_extension"
# pdf2svg file.pdf file.svg
;;
esac
}
# Convert PDF Files
pdf2() { \
out_ext=$(echo -e "svg\npng" | rofi -i -dmenu -p "Convert PDF to")
# Ask if crop?
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"png")
inkscape --export-type="$out_ext" --export-dpi=200 --export-area-drawing "$filename_with_extension"
;;
"svg")
inkscape --export-type="$out_ext" "$filename_with_extension"
;;
esac
}
# Convert DOCX/PPTX Files
docx2() { \
out_ext=$(echo -e "pdf" | rofi -i -dmenu -p "Convert DOCX/PPTX to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"pdf")
lowriter --convert-to pdf "$filename_with_extension"
;;
esac
}
# Convert PNG Files
png2() { \
out_ext=$(echo -e "pdf" | rofi -i -dmenu -p "Convert PNG to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"pdf")
convert "$filename_with_extension" "$filename_without_extension.pdf"
;;
esac
}
# Convert MP4 Files
mp42() { \
out_ext=$(echo -e "gif" | rofi -i -dmenu -p "Convert MP4 to")
if [ -z "$out_ext" ]; then
exit;
fi
case "$out_ext" in
"gif")
make-gif "$filename_with_extension" "$filename_without_extension.gif"
;;
esac
}
case "$in_ext" in
"svg")
svg2
;;
"pdf")
pdf2
;;
"mp4")
mp42
;;
"png")
png2
;;
"docx")
docx2
;;
"pttx")
docx2
;;
esac
#+end_src
2020-11-03 11:39:35 +01:00
* =pdf-shrink= - Pdf Shrink
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf-shrink
:END:
Simply reduces the size of a given pdf file.
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf-shrink
shrink ()
{
2020-11-03 11:39:35 +01:00
gs \
-q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.3 \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dAutoRotatePages=/None \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=$3 \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=$3 \
-dMonoImageDownsampleType=/Subsample \
-dMonoImageResolution=$3 \
-sOutputFile="$2" \
"$1"
2020-05-26 08:33:31 +02:00
}
check_smaller ()
{
2020-11-03 11:39:35 +01:00
# If $1 and $2 are regular files, we can compare file sizes to
# see if we succeeded in shrinking. If not, we copy $1 over $2:
if [ ! -f "$1" -o ! -f "$2" ]; then
return 0;
fi
ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )"
OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )"
if [ "$ISIZE" -lt "$OSIZE" ]; then
echo "Input smaller than output, doing straight copy" >&2
cp "$1" "$2"
fi
2020-05-26 08:33:31 +02:00
}
usage ()
{
2020-11-03 11:39:35 +01:00
echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
echo "Not guaranteed to succeed, but usually works."
echo " Usage: $1 infile [outfile] [resolution_in_dpi]"
2020-05-26 08:33:31 +02:00
}
IFILE="$1"
# Need an input file:
if [ -z "$IFILE" ]; then
2020-11-03 11:39:35 +01:00
usage "$0"
exit 1
2020-05-26 08:33:31 +02:00
fi
# Output filename defaults to "-" (stdout) unless given:
if [ ! -z "$2" ]; then
2020-11-03 11:39:35 +01:00
OFILE="$2"
2020-05-26 08:33:31 +02:00
else
2020-11-03 11:39:35 +01:00
OFILE="-"
2020-05-26 08:33:31 +02:00
fi
2021-01-01 20:12:34 +01:00
# Output resolution defaults to 90 unless given:
2020-05-26 08:33:31 +02:00
if [ ! -z "$3" ]; then
2020-11-03 11:39:35 +01:00
res="$3"
2020-05-26 08:33:31 +02:00
else
2020-11-03 11:39:35 +01:00
res="90"
2020-05-26 08:33:31 +02:00
fi
shrink "$IFILE" "$OFILE" "$res" || exit $?
check_smaller "$IFILE" "$OFILE"
#+end_src
* =pdf-delete-annotations= - Delete Annotations from PDFs
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf-delete-annotations
:END:
Taken from this [[https://gist.github.com/stefanschmidt/5248592][gist]].
2020-05-26 08:33:31 +02:00
#+begin_src bash :tangle ~/.local/bin/pdf-delete-annotations
2021-01-01 20:12:34 +01:00
# Check if the input argumetn is a PDF file
if [[ -f "$1" && "$1" == *.pdf ]]; then
pdftk $1 output /tmp/uncompressed.pdf uncompress
LANG=C sed -n '/^\/Annots/!p' /tmp/uncompressed.pdf > /tmp/stripped.pdf
pdftk /tmp/stripped.pdf output $1 compress
fi
2020-05-26 08:33:31 +02:00
#+end_src
* =pdf-delete-first-page= - Delete first page of PDF
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pdf-delete-first-page
:END:
2020-05-26 08:33:31 +02:00
The requirement is to have =pdftk= or =stapler= installed.
#+begin_src bash :tangle ~/.local/bin/pdf-delete-first-page
2021-01-01 20:12:34 +01:00
# Check if the input argumetn is a PDF file
2020-05-26 08:33:31 +02:00
if [[ -f $1 && $1 == *.pdf ]]; then
if type stapler > /dev/null 2>&1; then
stapler del "$1" 1 /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$1"
elif type pdftk > /dev/null 2>&1; then
pdftk "$1" cat 2-end output /tmp/pdftk_out.pdf && mv /tmp/pdftk_out.pdf "$1"
else
echo "Neither pdftk nor stapler are installed"
fi
fi
#+end_src
2020-11-03 11:39:35 +01:00
* =rofi-calc= - Simple Calculation using Rofi
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: rofi-calc
:END:
Run some simple calculations with =rofi=.
2020-11-03 11:39:35 +01:00
#+begin_src bash :tangle ~/.local/bin/rofi-calc
rofi -show calc -mode calc -no-show-match -no-sort
#+end_src
* =pass-gen= - Generate Random Alphanumeric Password
2021-01-01 20:12:34 +01:00
:PROPERTIES:
:CUSTOM_ID: pass-gen
:END:
2020-11-03 11:39:35 +01:00
#+begin_src bash :tangle ~/.local/bin/pass-gen
2021-01-01 20:12:34 +01:00
# Ask for the wanted number of caracters
2020-11-03 11:39:35 +01:00
num=$(rofi -dmenu -p "Number of caracters")
2021-01-01 20:12:34 +01:00
# Random generation of alphanumeric caracters
2020-11-03 11:39:35 +01:00
pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
re='^[0-9]+$'
if [[ $num =~ $re ]] ; then
pass=${pass:0:$num}
fi
2021-01-01 20:12:34 +01:00
# Send the password to the clipboard
2020-11-03 11:39:35 +01:00
printf "$pass" | xclip -sel clip && \
dunstify 'Password' 'Generated'
#+end_src
2021-01-01 20:12:34 +01:00
* =sxhkd-help= - List of keybindings using Rofi
:PROPERTIES:
:CUSTOM_ID: sxhkd-help
:END:
#+begin_src bash :tangle ~/.local/bin/sxhkd-help
awk '/^[a-z]/ && last {print "<small>",$0,"\t",last,"</small>"} {last=""} /^#/{last=$0}' ~/.config/sxhkd/sxhkdrc{,.i3} |
column -t -s $'\t' |
rofi -dmenu -i -markup-rows -no-show-icons -width 1000 -lines 15 -yoffset 40
#+end_src