config.phundrak.com/.local/bin/README.org
2019-10-23 14:52:39 +02:00

33 KiB
Raw Blame History

Phundraks executable scripts

Presentation

This file will present all the executable scripts I wrote. It is also their original source code, all the following code snippets are exported and tangled from this file to the actual executables.

Please do not forget to run the following before tangling files from this file to make sure the tangled files will be executables.

  (defun phundrak/make-tangled-files-executable ()
    (set-file-modes (buffer-file-name) #o755))
  (add-hook 'org-babel-post-tangle-hook 'phundrak/make-tangled-files-executable)

Askpass

Askpass is a simple script that invokes rofi as a way to get from a GUI the users sudo password. It is inspired by this original tool, rewritten in fish and with rofi support instead of dmenu. As you can see, this is a oneliner if we ignore the initial shebang. This executable is pointed at by the

  #!/usr/bin/env fish
  rofi -dmenu -font 'DejaVu Sans 10' -password -no-fixed-num-lines \
  -p (printf $argv[1] | sed s/://)

Dmenu

I wrote this very simple script in order to replace dmenu with rofis emulation of dmenu, since I prefer rofis appearance. It basically calls rofis dmenu emulation with the arguments initially passed to dmenu.

  #!/usr/bin/env fish
  rofi -dmenu $argv

Emoji picker

The emoji picker is a simple fish script that uses rofi and ~/.config/emoji.txt to provide a small, local search for emojis. Once the emoji is selected, it is copied to the clipboard using xclipboard.

  #!/usr/bin/env fish
  grep -v "#" ~/.config/emoji.txt | rofi -dmenu -i | awk '{print $1}' | tr -d '\n' | xclip -selection clipboard

Also, lets send a notification telling the user the emoji has been copied!

  set emoji (xclip -o -selection clipboard | tr -d '\n')
  test -z "$emoji" && notify-send "No emoji copied" && exit
  set -a emoji "copied to clipboard"
  pgrep -x dunst >/dev/null && notify-send $emoji

It is inspired from this video from Luke Smith, rewritten in Fish.

Polybar-launch

This scripts allows the user to kill polybar and relaunch it, or to simply launch it if polybar isnt launched yet. This script is a bash script, so lets declare its shebang.

#!/usr/bin/env bash

First thing to do is kill all polybar processes.

killall -q polybar

Now we have to wait untill all polybar processes have been shut down.

while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done

Now that our system isnt running polybar anymore, well launch it again on all of our screens. By the way, I have two bars, so Ill have to lauch them both.

  if type "xrandr"; then
      for m in $(xrandr --query | grep " connected" | cut -d" " -f1); do
          MONITOR=$m polybar --reload top &
          MONITOR=$m polybar --reload bottom &
      done
  else
      polybar --reload top &
      polybar --reload bottom &
  fi

And were done! Lets just launch a notification polybar has been relaunched.

  notify-send "Polybar restarted!" -a "polybar-launch"

Rofi-mount

rofimount is a script inspired by this one, based on dmenu, which interactively asks the user what to mount, and where to mount it. What I did was replace dmenu with rofi, and fix a couple of bugs I encountered in the original script. For the record, this is a fish script. Lets declare our shebang.

#!/usr/bin/env fish

Get the mountable elements

  begin

What the script does first is detect everything that can be mounted. Between a begin and end, lets set LFS as a local variable. This si in order to get sane variables in the current block.

set -l LFS

Now, lets detect the amount of mountable Android filesystems, and if any are detected, lets read them into a global variable.

  set -l a (math (jmtpfs -l | wc -l) - 2)
  test $a -ge 0 && jmtpfs -l 2> /dev/null | tail -n $a | read -zg anddrives

Well do the same for external and internal drives and partitions that can be mounted here.

  lsblk -rpo "name,type,size,mountpoint" | \
  awk '$2=="part"&&$4==""{printf "%s (%s)\n",$1,$3}' | \
  read -zg usbdrives

Finally, we look for any CD drive that could be mounted on our device.

  blkid /dev/sr0 | awk '{print $1}' | sed 's/://' | read -z cddrives

And thats the end of our first block!

  end

Alright, well save what kind on drives we can mount in a temporary file called /tmp/drives. Well make sure its blank by erasing it then creating it again with touch, like so. The -f flag on rm is here so we get no error if we try to delete a file that doesnt exist (yet).

  set -g TMPDRIVES /tmp/drives
  rm -f $TMPDRIVES
  touch $TMPDRIVES

Now, lets write what type of drives we can mount in this temporary file.

  test -n "$usbdrives" && echo "USB" >> $TMPDRIVES
  test -n "$cddrives" && echo "CD" >> $TMPDRIVES
  test -n "$anddrives" && echo "Android" >> $TMPDRIVES

Now, we want to declare where to look for mount directories. For now, well only look in /mnt, but you can add more if you wish.

  set -g basemount /mnt

Get the drive to mount

Now, lets declare a function that will allow us to chose the drive we want to mount.

  function getmount

First, we want to get our mount point. Well run a find command on each of the directories listed in $basemount to look for folders on which our drive could be mounted. This list will be passed to rofi from which we will chose our mount point.

  set -g mp (for d in $basemount
      find $d -maxdepth 5 -type d
  end | rofi -dmenu -i -p 'Type in mount point.')

We should verify that something has been actually selected, otherwise we should abort the script.

  if test -z $mp || test $mp = ""
      return 1
  end

Now, if the selected mount point does not exist, well ask the user whether the directory should be created. If no, the script will abort. If yes, an attempt will be made at creating the directory as the user; if that fails, a new attempt will be made as sudo.

  if test ! -d $mp
      switch (printf "No\\nYes" | rofi -dmenu -i -p "$mp does not exist. Create it?")
          case 'Yes'
              mkdir -p $mp || sudo -A mkdir -p $mp
          case '*'
              return 1
      end
  end

Finally, lets close the function

  end

Mount a USB drive, hard drive or partition

Alright, we want to mount a partition that answers by the name of /dev/sdXX, how do we do that? Lets create first the function mountusb that will take care of it for us.

function mountusb

Now, the first thing we want to do is select the partition we want to mount. Remember, we stored those in $usbdrives earlier, so lets pipe them into rofi so we can chose from it. Also, awk will get their path in /dev.

  set -g chosen (echo $usbdrives | \
  rofi -dmenu -i -p "Mount which drive?" | \
  awk '{print $1}')

As usual after a user selection, lets verify something has actually been selected. If not, lets abort the script.

test -z $chosen && return 1

Now, lets select the mount point of our partition. Well call the function getmount described in Get the drive to mount to select it.

getmount

Lets verify the variable mp set in getmount is not empty, otherwise abort the script.

test -z $mp && return 1

Now, lets mount it! Well use a switch which will detect the filesystem used so we know how to mount the partition.

switch (lsblk -no "fstype" $chosen)

We have two named case: vfat filesystems.

  case "vfat"
      sudo -A mount -t vfat $chosen $mp -o rw,umask=0000

And ntfs filesystems.

  case "ntfs"
      sudo -A mount -t ntfs $chosen $mp -o rw,umask=0000

Else, well let mount determine which filesystem is used by the partition (generally ext4).

  case '*'
      sudo -A mount $chosen $mp

Well also run a chown on this newly mounted filesystem so the user can access it without any issues.

  sudo -A chown -R $USER:(id -g $USER) $mp

Lets close the switch block and send a notification the partition has been mounted.

end && notify-send -a "dmount" "💻 USB mounting" "$chosen mounted to $mp."

And lets close the function.

end

Mount an Android device

The function that manages to mount Android filesystems is mountandroid. Lets declare it.

function mountandroid -d "Mount an Android device"

Well select which Android we want to mount. We will be asked through rofi.

set chosen (echo $anddrives | rofi -dmenu -i -p "Which Android device?" | awk '{print $1 $2}' | sed 's/,$//')

Now, we need to get the bus of the Android device we want to mount. It will be useful later, after we authorized mounting from our device, to get the path to our partition.

set bus (echo $chosen | sed 's/,.*//')

Lets temporarily mount our device.

jmtpfs -device=$chosen $mp

Now, we need to allow our computer to mount our Android device. Depending on the Android version it is running on, we either need to specify our device is USB connected in order to exchange files, or Android will explicitely ask us if it is OK for our computer to access it. Lets inform the user of that.

  echo "OK" | \
  rofi -dmenu -i -p "Press (Allow) on your phone screen, or set your USB settings to allow file transfert"

Now, lets get the actual path of our Android filesystem we wish to mount, and lets unmount the previous temporary filesystem.

  set newchosen (jmtpfs -l | grep $bus | awk '{print $1 $2}' | sed 's/,$//')
  sudo -A umount $mp

Now we cam mount the new filesystem and send a notification if everything went well.

  jmtpfs -device=$newchosen $mp && \
  notify-send -a "dmount" "🤖 Android Mounting" "Android device mounted to $mp."

And now, we can close our function.

end

Mount a CD drive

This part is way easier than the previous functions. As we will see, the function mountcd's body is only three lines long. First, lets declare the function.

function mountcd

Now, lets chose the CD drive we want to mount using rofi.

  set chosen (echo $cddrives | rofi -dmenu -i -p "Which CD drive?")

Well also get the mountpoint thanks to the getmount function described earlier.

getmount

And finally, lets mount it and send the notification everything went well.

  sudo -A mount $chosen $mp && \
  notify-send -a "dmount" "💿 CD mounting" "$chosen mounted."

Finally, lets close our function.

end

Ask what type of drive we want to mount

The first thing we will be asked if different types of drives are detected is which of these types the user wishes to mount. This is done with the function asktype which is declared below.

function asktype

We will use a switch statement which will use our anwser to rofi about what we wish to mount.

switch (cat $TMPDRIVES | rofi -dmenu -i -p "Mount which drive?")

If we chose the option "USB", well mount a hard drive, partition or USB drive. In which case well call the mountusb function.

  case "USB"
      mountusb

If we chose the "Android" option, the mountandroid function is called.

  case "Android"
      mountandroid

Else if we chose the "CD" option, well call the mountcd function.

  case "CD"
      mountcd

If nothing is selected, the function will naturally exit. Now, lets close our switch statement and our function.

end
end

Launch the mounting functions

Now that we have declared our functions and set our variables, well read the temporary file described in Get the mountable elements. The amount of lines is passed in a switch statement.

switch (wc -l < $TMPDRIVES)

If the file has no lines, i.e. it is empty, we have no mountable media. Lets inform our user this is the case.

  case 0
      notify-send "No USB drive or Android device or CD detected" -a "dmount"

If we only have one line, we have only one type of mountable media. Well pass this line to a second switch statement.

  case 1
      switch (cat $TMPDRIVES)

This will allow the script to automatically detect what type of media it is, and mount the corresponding function.

  case "USB"
      mountusb
  case "Android"
      mountandroid
  case "CD"
      mountCD

Lets close this nested switch case.

end

If we have more than one line, well have to ask the user what type of media they want to mount.

  case '*'
      asktype

Now, lets end our switch statement!

end

Finally, well delete our temporary file.

rm -f $TMPDRIVES

And with that, this is the end of our script!

Rofi-umount

rofiumount is the counterpart of rofimount for unmounting our mounted partitions. It is a fish script, so lets declare it as that with its shebang.

#!/usr/bin/env fish

Get the unmountable drives

First, we will need to list all the drives that can be safely unmounted. Lets run this.

  set -g drives (lsblk -nrpo "name,type,size,mountpoint" | \
  awk '$2=="part"&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "%s (%s)\n",$4,$3}')

Now, lets get the android devices that are mounted.

set -g androids (awk '/jmtpfs/ {print $2}' /etc/mtab)

And lets get the CD drives that are mounted.

set -g cds (awk '/sr0/ {print $2}' /etc/mtab)

Well store all of our information in a temporary file, /tmp/undrives.

set -g undrivefile /tmp/undrives

Lets make sure we begin with a clean, empty file.

  rm -f $undrivefile
  touch $undrivefile

Depending on if the related variables are set, write the different types of mounted drives in the temporary file.

  test -n "$drives" && echo "USB" >> $undrivefile
  test -n "$cds" && echo "CD" >> $undrivefile
  test -n "$androids" && echo "Android" >> $undrivefile

Unmount disk partitions

The function unmountusb will take care of unmounting any drive we can safely unmount. First, lets declare the function.

function unmountusb

Lets chose the drive to unmount with rofi.

  set chosen (echo $drives | \
  rofi -dmenu -i -p "Unmount which drive?" | \
  awk '{print $1}')

Lets verify if the user actually selected any drive. If no, lets abort the script.

test -z "$chosen" && exit 0

Now, lets unmount the chosen drive and send a notification if it has been done. Otherwise, a notification will be sent, saying the operation failed.

  sudo -A umount $chosen && \
  notify-send "💻 USB unmounting" "$chosen unmounted." -a "dumount" || \
  notify-send "💻 USB unmounting" "$chosen unmounting failed." -a "dumount"

Now, lets close the function.

end

Unmount Android device

The function unmountandroid will take care of unmounting any mounted Android device. First, lets declare our function.

function unmountandroid

Let the user choose which Android device to unmount.

set chosen (echo $androids | rofi -dmenu -i -p "Unmount which device?")

Well verify the user chose any device.

  test -z "$chosen" && exit 0

If a device has been chosen, lets unmount it and send a notification whether it has been successfuly unmounted.

  sudo -A umount -l $chosen && |
  notify-send "🤖 Android unmounting" "$chosen unmounted." -a "dumount" || \
  notify-send "🤖 Android unmounting" "$chosen failed to unmount." -a "dumount"

Finally, lets close the function.

end

Unmount CD drive

unmountcd will take care of unmounting any mounted CD drive. Lets declare this function.

function unmountcd

As before, let the user chose which CD drive to unmount.

set chosen (echo "$cds" | rofi -dmenu -i -p "Unmount which CD?")

Well verify the user chose any device.

  test -z "$chosen" && exit 0

If a drive has been chosen, lets unmount it and send a notification whether it has been successfuly unmounted.

  sudo -A umount -l $chosen && \
  notify-send "💿 CD unmounting" "$chosen unmounted." -a "dumount" || \
  notify-send "💿 CD unmounting" "$chosen failed to unmount." -a "dumount"

Now, lets close the function.

end

Ask what type of drive to unmount

If several types of unmountable drives are available, lets ask the user which type to unmount based on the content of the temporary file declared in Get the unmountable drives. First, lets declare the function.

function asktype

Lets create a switch statement to which will be passed the selection of the user from rofi.

  switch (cat $undrivefile | rofi -dmenu -i -p "Unmount which type of device?")

Three types of values can be returned: "USB", "CD", or "Android". These values will be used to launch their corresponding function.

  case 'USB'
      unmountusb
  case 'CD'
      unmountcd
  case 'Android'
      unmountandroid

Lets close the switch statement.

end

Lets now close the function.

end

Launch the unmounting functions

Now back to the body of our script, lets input in a switch case the number of lines contained in our temporary file.

switch (wc -l < $undrivefile)

If the file containes no lines. i.e. it is empty, nothing is to be unmounted. Lets inform the user of that.

  case 0
      notify-send "No USB drive or Android device or CD to unmount" -a "dumount"

Else, if there is only one type of drive, well automatically let our script choose based on the content of this sole line.

  case 1
      switch (cat $undrivefile)
          case 'USB'
              unmountusb
          case 'CD'
              unmountcd
          case 'Android'
              unmountandroid
      end

And if there are more types than one, lets ask the user.

case '*'
     asktype

Lets close our main switch statement.

end

And finally, lets delete our temporary file.

rm -f $undrivefile

Starwars

This is a one-liner that allows you to watch Star Wars episode 4 in ASCII art in your terminal. Here is the code:

  #!/usr/bin/env fish
  telnet towel.blinkenlights.nl

Wacom setup

I made a small and quick utility to set up my Wacom tablet so it is only bound to one screen. This is a fish script, so lets insert the sheband.

#!/usr/bin/env fish

Set our variables

Lets first declare our function that will be called to set our variables.

function set_device

We need some variables in order to correctly set our tablet. First, lets get declare what the name of our tablet is, and what the name of its touchpad is.

  set -g DEVICE "Wacom USB Bamboo PAD Pen stylus"
  set -g DEVICETOUCH "Wacom USB Bamboo PAD Finger touch"

We will also modify two settings: the speed of the cursor on the touchpad, and the scroll speed. Lets declare the name of these two settings.

  set -g WACOMPROPTOUCHSPEED "Device Accel Velocity Scaling"
  set -g WACOMPROPSCROLLPSEED "ScrollDistance"

To get the correct values for the area it can cover, well need to reset our tablet.

xsetwacom set "$DEVICE" ResetArea

Now we can get the X and Y areas.

  set -l AREATOT (xsetwacom get "$DEVICE" Area)
  set -g AREAX (echo $AREATOT | awk '{print $3}')
  set -g AREAY (echo $AREATOT | awk '{print $4}')

Now lets close our function.

end

Select our screen

This function will allow us to select the screen on which the tablet will be active. We can also select the option “desktop” so that all screens are selected. Lets declare our function.

function set_screen

First, lets set what screens are available, including the desktop option.

  set CONNECTED_DISPLAYS (xrandr -q --current | \
  sed -n 's/^\([^ ]\+\) connected .*/\1/p') desktop

Now, lets select the one we wish to use using rofi.

  set -g SCREEN (for d in $CONNECTED_DISPLAYS
      echo $d
  end | rofi -dmenu -i -p "Select your dispaly" | tr -d '\n')

Now, lets get the resolution of our selected screen.

  set -l LINE (xrandr -q --current | if [ "$SCREEN" = "desktop" ]
      sed -n 's/^Screen 0:.*, current \([0-9]\+\) x \([0-9]\+\),.*/\1 \2/p'
  else
      sed -n "s/^$SCREEN"' connected \(primary \)\{0,1\}\([0-9]\+\)x\([0-9]\+\)+.*/\2 \3/p'
  end)

From that, lets get the vertical and horizontal resolution of our screen.

echo $LINE | read -g WIDTH HEIGHT

If any of our WIDTH ou HEIGHT it empty, well have to abort the script.

  if test -z $WIDTH || test -z $HEIGHT
      exit 1
  end

Lets close our function now.

end

Adjust the tablet

This function will take care of adjusting our tablet to our screen. Lets declare our function.

function adjust_device

If our screen is too high or too wide for our tablet, we will have to adjust the height or width of the area used by the tablet. So lets get the theoretical new height and width of the area.

  set RATIOAREAY (math ceil \($AREAX \* $HEIGHT \/ $WIDTH\))
  set RATIOAREAX (math ceil \($AREAY \* $WIDTH \/ $HEIGHT\))

Now, if the current height of the tablets area is greater than the theoretical new area, it means the current area is too high. Otherwise, it should be the other way around. Lets set NEWAREAX and NEWAREAY that will be used to set the new area for the tablet.

  if test $AREAY -gt $RATIOAREAY
      set -g NEWAREAX $AREAX
      set -g NEWAREAY $RATIOAREAY
  else
      set -g NEWAREAX $RATIOAREAX
      set -g NEWAREAY $AREAY
  end

Alright, now lets set the new area with these new variables.

  xsetwacom set "$DEVICE" Area 0 0 $NEWAREAX $NEWAREAY
  xsetwacom set "$DEVICE" MapToOutput "$SCREEN"

Lets slow down the cursors speed on the touchpad.

xinput set-float-prop $DEVICETOUCH $WACOMPROPTOUCHSPEED 0.5

Lets also slow down the scroll speed of the touchpad.

  xsetwacom set $DEVICETOUCH $WACOMPROPSCROLLPSEED "90"

Now, lets close the function.

end

Lauch the functions

Back to the main body of the script, we can now launch the functions sequencially.

  set_device
  set_screen
  adjust_device

Yadm

For some reason, yadm wont stop making polybar crash. So, I created this script that will wrap yadm with a call to yadm, and then a call to polybar-launch declared in Polybar-launch. This is a oneliner, as you can see below:

  #!/usr/bin/env fish
  /usr/bin/yadm $argv; polybar-launch 2>/dev/null >/dev/null