dotfiles/.local/bin
2019-10-23 10:18:23 +02:00
..
askpass .local/bin/README.org now source file for askpass and rofiemoji 2019-10-23 10:18:23 +02:00
dmount whitespace 2019-10-16 11:59:13 +02:00
dumount Nicer usage of dunst 2019-10-08 17:28:34 +02:00
enable_touch fixed script call 2019-10-07 14:02:13 +02:00
README.org .local/bin/README.org now source file for askpass and rofiemoji 2019-10-23 10:18:23 +02:00
rofiemoji .local/bin/README.org now source file for askpass and rofiemoji 2019-10-23 10:18:23 +02:00
starwars added starwars to my bins 2019-10-08 17:00:52 +02:00
wacom-setup better wacom setup utility 2019-10-21 01:25:57 +02:00

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/fish
  rofi -dmenu -font 'DejaVu Sans 10' -password -no-fixed-num-lines \
  -p (printf $argv[1] | sed s/://)

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/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')
  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.

Rofimount

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.

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" >> /tmp/drives
  test -n "$cddrives" && echo "CD" >> /tmp/drives
  test -n "$anddrives" && echo "Android" >> /tmp/drives

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

Finally, lets close the function.

end