.. | ||
askpass | ||
dmount | ||
dumount | ||
enable_touch | ||
README.org | ||
rofiemoji | ||
starwars | ||
wacom-setup |
Phundrak’s 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
user’s 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, let’s 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
, let’s set LFS
as a local variable. This si in order to
get sane variables in the current block.
set -l LFS
Now, let’s detect the amount of mountable Android filesystems, and if any are detected, let’s 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
We’ll 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 that’s the end of our first block!
end
Alright, we’ll save what kind on drives we can mount in a temporary file
called /tmp/drives
. We’ll make sure it’s 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 doesn’t exist (yet).
set -g TMPDRIVES /tmp/drives
rm -f $TMPDRIVES
touch $TMPDRIVES
Now, let’s 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, we’ll
only look in /mnt
, but you can add more if you wish.
set -g basemount /mnt
Get the drive to mount
Now, let’s 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. We’ll 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, we’ll 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, let’s 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? Let’s 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 let’s 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, let’s verify something has actually been selected. If not, let’s abort the script.
test -z $chosen && return 1
Finally, let’s close the function.
end