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.
Because I sometimes switch from window manager to window manager, creating a script that handles by itself autostarting things for me is way easier than rewriting every time the autostart part of my configuration. As you can every instance will be launched asynchronously, and only if there is no other instance of said command running.
~set-screens~ is a custom script declared [[*set-screens][below]].
~awiki~ is a simple script used with ~rofi~ that relies on the ~arch-wiki-docs~ package in order to provide the user a way to quickly find and display any English page from the Arch Wiki in a browser. The advantage of using this over the ~wiki-search~ utility from the ~arch-wiki-lite~ package is you get instant suggestion in rofi using fuzzy-search. The downside is rofi will only help you find pages by their title, and it will not help you find keywords in the content of said pages.
The first step is to create the list of all the pages that are currently stored on disk. ~arch-wiki-docs~ stores them in ~/usr/share/doc/arch-wiki/html/en~. A simple ~ls~ piped in three ~sed~ will give us a list of page titles. We then pipe that into rofi in dmenu mode in order to choose the page we want to display. By the way, setting the location of the HTML files will come in handy later.
#+BEGIN_SRC fish
set WLOCATION /usr/share/doc/arch-wiki/html/en/
set WPAGE (/bin/ls $WLOCATION | \
sed -e 's/_//g' -e 's/\.html$//' -e 's|.*/\(.*\)|\1|' | \
rofi -dmenu -p "Arch Wiki" -i | sed 's/ +/_/g')
#+END_SRC
Now, all I need to do is to send this list into rofi and tell it to open the result with our favorite browser with ~xdg-open~.
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 [[https://github.com/ODEX-TOS/tools/blob/master/rofi/askpass][this original tool]], rewritten in fish and with [[https://wiki.archlinux.org/index.php/Rofi][rofi]] support instead of [[https://wiki.archlinux.org/index.php/Dmenu][dmenu]]. As you can see, this is a oneliner if we ignore the initial shebang. This executable is pointed at by the
#+BEGIN_SRC fish
rofi -dmenu -password -no-fixed-num-lines -p (printf $argv[1] | sed s/://)
~backup~ is a very simple, oneliner script that will create a local copy of a file and add the date at which it was copied in the filename. You can see its source code here:
~connect-wifi~ is a small utility tool that allows the user to connect to available WiFi networks. The first thing to do is to select the WiFi we want to connect to. We’ll use the ~nmcli c s~ command to get the list of the available networks, and we’ll chose one with ~rofi~.
=cppnew= is a small utility that helps you create a new C++ project. Several templates are available, the default one using CMake, and three others that are a bit more advances, based on:
There is also a default [[http://doxygen.nl/][Doxygen]] file included for your documentation, ready to go. I even made it so that you can execute it as an executable file, like =./doc/Doxyfile= from the project root.
The choice is given to the user which of them to use with options that will be given to =cppnew=.
First of all, if no arguments were passed, return an error.
#+begin_src fish
if ! count $argv >/dev/null
echo "Missing argument: PROJECT" && return -1
end
#+end_src
Now, let’s set a couple of variables which will prove useful later on when trying to set up our project.
=cnew= is a small utility script similar to but simpler than cppnew that creates a CMake template C project from the template that already exists in [[file:~/dev/templateC][~/dev/templateC]]. If no argument was passed, display an error message and exit.
Spacemacs' recommendations on how to use Dart with LSP is outdated, since [[https://github.com/natebosch/dart_language_server][=dart_language_server=]] is obsolete. As recommended by the repo owner, we should launch instead the following code:
I wrote this very simple script in order to replace =dmenu= with rofi’s emulation of dmenu, since I prefer rofi’s appearance. It basically calls rofi’s dmenu emulation with the arguments initially passed to dmenu.
The emoji picker is a simple fish script that uses rofi and [[file:~/.config/emoji.txt][~/.config/emoji.txt]] to provide a small, local search for emojis. Once the emoji is selected, it is copied to the clipboard using =xclipboard=.
~plock~ is a simple script that locks the screen with ~i3lock~ while setting as the background image of the locked screen a corrupted screenshot of the screen before it was locked.
This scripts allows the user to kill polybar and relaunch it, or to simply launch it if polybar isn’t launched yet. First thing to do is kill all polybar processes.
#+BEGIN_SRC bash
killall -q polybar
#+END_SRC
Now we have to wait untill all polybar processes have been shut down.
#+BEGIN_SRC bash
while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done
#+END_SRC
Now that our system isn’t running polybar anymore, we’ll launch it again on all of our screens. By the way, I have two bars, so I’ll have to lauch them both.
#+BEGIN_SRC bash
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
#+END_SRC
And we’re done! Let’s just launch a notification polybar has been relaunched.
#+BEGIN_SRC bash
notify-send "Polybar restarted!" -a "polybar-launch"
=rofimount= is a script inspired by [[https://github.com/ihebchagra/dotfiles/blob/master/.local/bin/dmount][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.
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.
#+BEGIN_SRC fish
set -l LFS
#+END_SRC
Now, let’s detect the amount of mountable Android filesystems, and if any are detected, let’s read them into a global variable.
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).
#+BEGIN_SRC fish
set -g TMPDRIVES /tmp/drives
rm -f $TMPDRIVES
touch $TMPDRIVES
#+END_SRC
Now, let’s write what type of drives we can mount in this temporary file.
#+BEGIN_SRC fish
test -n "$usbdrives" && echo "USB" >> $TMPDRIVES
test -n "$cddrives" && echo "CD" >> $TMPDRIVES
test -n "$anddrives" && echo "Android" >> $TMPDRIVES
#+END_SRC
Now, we want to declare where to look for mount directories. For now, we’ll only look in =/media=, but you can add more if you wish.
Now, let’s declare a function that will allow us to chose the drive we want to mount.
#+BEGIN_SRC fish
function getmount
#+END_SRC
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.
#+BEGIN_SRC fish
set -g mp (for d in $basemount
find $d -maxdepth 5 -type d
end | rofi -dmenu -i -p 'Type in mount point.')
#+END_SRC
We should verify that something has been actually selected, otherwise we should abort the script.
#+BEGIN_SRC fish
if test -z $mp || test $mp = ""
return 1
end
#+END_SRC
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.
#+BEGIN_SRC fish
if test ! -d $mp
switch (printf "No\\nYes" | rofi -dmenu -i -p "$mp does not exist. Create it?")
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.
#+BEGIN_SRC fish
function mountusb
#+END_SRC
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=.
#+BEGIN_SRC fish
set -g chosen (echo $usbdrives | \
rofi -dmenu -i -p "Mount which drive?" | \
awk '{print $1}')
#+END_SRC
As usual after a user selection, let’s verify something has actually been selected. If not, let’s abort the script.
#+BEGIN_SRC fish
test -z $chosen && return 1
#+END_SRC
Now, let’s select the mount point of our partition. We’ll call the function =getmount= described in [[#Rofi-mount-Get_the_mount_point-6c4bac06][Get the mount point]] to select it.
#+BEGIN_SRC fish
getmount
#+END_SRC
Let’s verify the variable =mp= set in =getmount= is not empty, otherwise abort the script.
#+BEGIN_SRC fish
test -z $mp && return 1
#+END_SRC
Now, let’s mount it! We’ll use a switch which will detect the filesystem used so we know how to mount the partition.
#+BEGIN_SRC fish
switch (lsblk -no "fstype" $chosen)
#+END_SRC
We have two named case: =vfat= filesystems.
#+BEGIN_SRC fish
case "vfat"
sudo -A mount -t vfat $chosen $mp -o rw,umask=0000
#+END_SRC
And =ntfs= filesystems.
#+BEGIN_SRC fish
case "ntfs"
sudo -A mount -t ntfs $chosen $mp -o rw,umask=0000
#+END_SRC
Else, we’ll let =mount= determine which filesystem is used by the partition (generally =ext4=).
#+BEGIN_SRC fish
case '*'
sudo -A mount $chosen $mp
#+END_SRC
We’ll also run a =chown= on this newly mounted filesystem so the user can access it without any issues.
#+BEGIN_SRC fish
sudo -A chown -R $USER:(id -g $USER) $mp
#+END_SRC
Let’s close the switch block and send a notification the partition has been mounted.
#+BEGIN_SRC fish
end && notify-send -a "dmount" "💻 USB mounting" "$chosen mounted to $mp."
The function that manages to mount Android filesystems is =mountandroid=. Let’s declare it.
#+BEGIN_SRC fish
function mountandroid -d "Mount an Android device"
#+END_SRC
We’ll select which Android we want to mount. We will be asked through rofi.
#+BEGIN_SRC fish
set chosen (echo $anddrives | rofi -dmenu -i -p "Which Android device?" | awk '{print $1 $2}' | sed 's/,$//')
#+END_SRC
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.
#+BEGIN_SRC fish
set bus (echo $chosen | sed 's/,.*//')
#+END_SRC
Let’s temporarily mount our device.
#+BEGIN_SRC fish
jmtpfs -device=$chosen $mp
#+END_SRC
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. Let’s inform the user of that.
#+BEGIN_SRC fish
echo "OK" | \
rofi -dmenu -i -p "Press (Allow) on your phone screen, or set your USB settings to allow file transfert"
#+END_SRC
Now, let’s get the actual path of our Android filesystem we wish to mount, and let’s unmount the previous temporary filesystem.
#+BEGIN_SRC fish
set newchosen (jmtpfs -l | grep $bus | awk '{print $1 $2}' | sed 's/,$//')
sudo -A umount $mp
#+END_SRC
Now we cam mount the new filesystem and send a notification if everything went well.
#+BEGIN_SRC fish
jmtpfs -device=$newchosen $mp && \
notify-send -a "dmount" "🤖 Android Mounting" "Android device mounted to $mp."
This part is way easier than the previous functions. As we will see, the function =mountcd='s body is only three lines long. First, let’s declare the function.
#+BEGIN_SRC fish
function mountcd
#+END_SRC
Now, let’s chose the CD drive we want to mount using =rofi=.
#+BEGIN_SRC fish
set chosen (echo $cddrives | rofi -dmenu -i -p "Which CD drive?")
#+END_SRC
We’ll also get the mountpoint thanks to the =getmount= function described earlier.
#+BEGIN_SRC fish
getmount
#+END_SRC
And finally, let’s mount it and send the notification everything went well.
#+BEGIN_SRC fish
sudo -A mount $chosen $mp && \
notify-send -a "dmount" "💿 CD mounting" "$chosen mounted."
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.
#+BEGIN_SRC fish
function asktype
#+END_SRC
We will use a switch statement which will use our anwser to rofi about what we wish to mount.
Now that we have declared our functions and set our variables, we’ll read the temporary file described in [[#Rofi-mount-Get_the_mountable_elements-24db7834][Get the mountable elements]]. The amount of lines is passed in a switch statement.
#+BEGIN_SRC fish
switch (wc -l < $TMPDRIVES)
#+END_SRC
If the file has no lines, i.e. it is empty, we have no mountable media. Let’s inform our user this is the case.
#+BEGIN_SRC fish
case 0
notify-send "No USB drive or Android device or CD detected" -a "dmount"
#+END_SRC
If we only have one line, we have only one type of mountable media. We’ll pass this line to a second switch statement.
#+BEGIN_SRC fish
case 1
switch (cat $TMPDRIVES)
#+END_SRC
This will allow the script to automatically detect what type of media it is, and mount the corresponding function.
#+BEGIN_SRC fish
case "USB"
mountusb
case "Android"
mountandroid
case "CD"
mountCD
#+END_SRC
Let’s close this nested switch case.
#+BEGIN_SRC fish
end
#+END_SRC
If we have more than one line, we’ll have to ask the user what type of media they want to mount.
=rofi-pass= is a simple utility that gets a password stored in the [[https://www.passwordstore.org/][=pass=]] password manager with rofi as its interface, and then stores the password in the clipboard.
Let’s parse all the arguments passed to the script. If one of them is =--type=, =-t= or =type=, the script will attempt to type the password to the text area already selected without pasting the password to the clipboard.
#+BEGIN_SRC fish
for arg in $argv
switch $arg
case '--type' '-t' 'type'
set -g TYPE "yes"
case '*'
printf 'Unknown argument: %s\n.' $arg
exit 1
end
end
#+END_SRC
Now, let’s get the list of the passwords that exist in our =pass= repository.
#+BEGIN_SRC fish
set passwords (find $HOME/.password-store -type f -name "*.gpg" | \
string replace -r ".*.password-store/" "" | \
string replace -r ".gpg" "" | sort)
#+END_SRC
Let the user choose which password they wish to select.
#+BEGIN_SRC fish
set password (for elem in $passwords
echo $elem
end | rofi -dmenu -i -p "Select your password")
#+END_SRC
Let’s verify we actually selected a password and not just exited. If no password was selected, let’s simply exit the script.
#+BEGIN_SRC fish
if test -z $password
exit
end
#+END_SRC
Depending on the arguments passed earlier, we might want some different behavior.
#+BEGIN_SRC fish :noweb yes
if test $TYPE = "yes"
<<rofi-pass-type>>
else
<<rofi-pass-copy>>
end
#+END_SRC
The default behavior is to copy the password to the clipboard for 45 seconds, so let’s do that.
#+NAME: rofi-pass-copy
#+BEGIN_SRC fish :noweb yes :tangle no
pass show -c $password 2> /dev/null
#+END_SRC
Else, if we passed =--type=, =-t= or =type= as an argument of the script, we want it to attempt to type our password in the currently selected text input. Let’s do that.
#+NAME: rofi-pass-type
#+BEGIN_SRC fish :noweb yes :tangle no
set -l IFS
<<rofi-pass-type-get-password>>
printf %s $pass | xvkbd -file -
#+END_SRC
To correctly get the password from ~pass~, we need to parse the output and only get the first line, hence the following command.
#+NAME: rofi-pass-type-get-password
#+BEGIN_SRC fish :tangle no
set pass (pass show $password | string split -n \n)[1]
If several types of unmountable drives are available, let’s ask the user which type to unmount based on the content of the temporary file declared in [[#Rofi-umount-Get_the_unmountable_drives-89c71040][Get the unmountable drives]]. First, let’s declare the function.
#+BEGIN_SRC fish
function asktype
#+END_SRC
Let’s create a switch statement to which will be passed the selection of the user from rofi.
#+BEGIN_SRC fish
switch (cat $undrivefile | rofi -dmenu -i -p "Unmount which type of device?")
#+END_SRC
Three types of values can be returned: "USB", "CD", or "Android". These values will be used to launch their corresponding function.
~set-screens~ is a small script that allows the user to automatically set up an external monitor. First, let’s set some variables so we don’t have to type in hidden places some values that should be easily modifiable.
#+BEGIN_SRC fish
set internal "eDP1"
set external "HDMI1"
#+END_SRC
Now, let’s set the ~DETECTEDSCREEN~ variable with a simple ~grep~. If the variable turns out to be empty, this means the display was not detected. However, if it’s not, then it will be an array with its second value that holds the maximum resolution the display can handle. It needs to be passed through ~awk~ in order to get only the resolution itself and not the refresh rate, but once we’ve got that, we can set our external monitor as the main monitor with its maximum resolution. i3 is also restarted in order to properly display the wallpaper and Polybar on the new screen.
#+BEGIN_SRC fish
set externaldisplay (xrandr -q --current | grep -A 1 -i "$external connected")
if test -n "$externaldisplay"
set resolution (echo $externaldisplay[2] | awk '{$1=$1;print $1}')
Something that I did not know for quite some time but that is actually crazy useful about SSH is its ability to bind locally the port of a remote machine, and vice versa. The syntax is actually very simple, but I prefer a more intuitive way of writing it. Its usage is ~sshbind PORT FROMHOST TOHOST~.
For some reasons, my firmware does not recognize the function key for toggling the touchpad. I’m not going to really complain about it since it lets me program it like I want. Since I often don’t need to completely deactivate the touchpad, I’ll instead toggle whether tapping is enabled or not when pressing ~XF86TouchpadToggle~. And for that, I need this small script that will actually toggle it, and it will be used in my window manager configuration.
First let’s declare some variables to make this script more personal. With my current computer (a Gazelle by System76), the name of my touchpad is the following:
#+BEGIN_SRC fish
set TPNAME "ELAN0412:00 04F3:3162 Touchpad"
#+END_SRC
Let’s now get the identifier of the touchpad for ~xinput~:
#+BEGIN_SRC fish
set TPID (xinput list | grep $TPNAME | awk '{print $6}' | sed 's|id=\(.*\)|\1|g')
#+END_SRC
Now, let’s detect the current status of the touchpad:
This will set ~TPSTATUS~ either to ~0~, meaning tapping is disabled, or to ~1~, meaning it’s enabled. I will consider any other value as being disabled.
#+BEGIN_SRC fish
test [[ $TPSTATUS = "1" ]] && set NEWTPSTATUS 0 || set NEWTPSTATUS 1
Let’s first declare our function that will be called to set our variables.
#+BEGIN_SRC fish
function set_device
#+END_SRC
We need some variables in order to correctly set our tablet. First, let’s get declare what the name of our tablet is, and what the name of its touchpad is.
#+BEGIN_SRC fish
set -g DEVICE "Wacom USB Bamboo PAD Pen stylus"
set -g DEVICETOUCH "Wacom USB Bamboo PAD Finger touch"
#+END_SRC
We will also modify two settings: the speed of the cursor on the touchpad, and the scroll speed. Let’s declare the name of these two settings.
#+BEGIN_SRC fish
set -g WACOMPROPTOUCHSPEED "Device Accel Velocity Scaling"
set -g WACOMPROPSCROLLPSEED "ScrollDistance"
#+END_SRC
To get the correct values for the area it can cover, we’ll need to reset our tablet.
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. Let’s declare our function.
#+BEGIN_SRC fish
function set_screen
#+END_SRC
First, let’s set what screens are available, including the desktop option.
#+BEGIN_SRC fish
set CONNECTED_DISPLAYS (xrandr -q --current | \
sed -n 's/^\([^ ]\+\) connected .*/\1/p') desktop
#+END_SRC
Now, let’s select the one we wish to use using rofi.
#+BEGIN_SRC fish
set -g SCREEN (for d in $CONNECTED_DISPLAYS
echo $d
end | rofi -dmenu -i -p "Select your dispaly" | tr -d '\n')
#+END_SRC
Now, let’s get the resolution of our selected screen.
#+BEGIN_SRC fish
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)
#+END_SRC
From that, let’s get the vertical and horizontal resolution of our screen.
#+BEGIN_SRC fish
echo $LINE | read -g WIDTH HEIGHT
#+END_SRC
If any of our ~WIDTH~ ou ~HEIGHT~ it empty, we’ll have to abort the script.
This function will take care of adjusting our tablet to our screen. Let’s declare our function.
#+BEGIN_SRC fish
function adjust_device
#+END_SRC
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 let’s get the theoretical new height and width of the area.
#+BEGIN_SRC fish
set RATIOAREAY (math ceil \($AREAX \* $HEIGHT \/ $WIDTH\))
set RATIOAREAX (math ceil \($AREAY \* $WIDTH \/ $HEIGHT\))
#+END_SRC
Now, if the current height of the tablet’s area is greater than the theoretical new area, it means the current area is too high. Otherwise, it should be the other way around. Let’s set =NEWAREAX= and =NEWAREAY= that will be used to set the new area for the tablet.
#+BEGIN_SRC fish
if test $AREAY -gt $RATIOAREAY
set -g NEWAREAX $AREAX
set -g NEWAREAY $RATIOAREAY
else
set -g NEWAREAX $RATIOAREAX
set -g NEWAREAY $AREAY
end
#+END_SRC
Alright, now let’s set the new area with these new variables.
#+BEGIN_SRC fish
xsetwacom set "$DEVICE" Area 0 0 $NEWAREAX $NEWAREAY
xsetwacom set "$DEVICE" MapToOutput "$SCREEN"
#+END_SRC
Let’s slow down the cursor’s speed on the touchpad.
A quick and useful script I often use is a ~curl~ request to [[http://v2.wttr.in/][v2.wttr.in]] to get a weather forecast in the terminal. By default, I want the request to be about the city I live in, but it is also possible for the script to accept as its arguments a search inquiry.