dotfiles/org/config/bin.org
Lucien Cartier-Tilet 166f2094df
Better sed' command and fixed set-screens'
`set-screens' used to automatically restart i3. Since I do not use i3
and Awesome needs no restart after invoking this command, I removed
the call to `i3-msg'.
2020-08-22 16:52:32 +02:00

45 KiB
Raw Blame History

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)

This code block can be evaluated once this file has been tangled in order to produce again non-executable files.

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

4chandl

Usage: 4chandl [ URL TO THREAD ]

I made this small script to download the attached files of 4chan threads. First of all, lets declare it as a fish script.

#!/usr/bin/env fish

Now, lets check if any arguments were passed to the executable. If none were passed, the script should be aborted.

  if ! count $argv > /dev/null
      echo 'No URL specified! Give the URL to thread as the only argument.'
      exit 1
  end

Now, lets store the regex we use to get the link to the attached files.

set regex_4cdn '\/\/is2\.4chan\.org\/[a-z]+\/[A-Za-z0-9]+\.[A-Za-z]{3,4}'

Well use a thread counter to get a visual indication on how the download is going.

  set thread_counter 1

Now, we will use each of the arguments passed as a URL to download the files from.

for url in $argv

As a visual indicator, lets get the amount of elements we are going to download from the current thread and print it.

  set file_total (curl -ks $url | grep -oE $regex_4cdn | uniq | wc -l)
  echo total files to download in current thread: $file_total

Lets set a file counter so we can visualize the download progress.

set file_counter 1

Now, lets download each file from the current thread.

  for image_url in (curl -k -s $url | grep -Eo $regex_4cdn | uniq | sed 's/^/https:/')
      echo -n Downloading image $counter of $total...
      wget --no-check-certificate -q -nc $image_url
      echo ' Done (thread: $thread_counter/thread_total\tfile: $file_counter/file_total)'
      set file_counter (math $file_counter + 1)
  end

Lets increment the thread counter.

set thread_counter (math $thread_counter + 1)

Lets now close the for loop.

end

awiki

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.

  #!/usr/bin/env 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')

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.

  xdg-open $WLOCATION$WPAGE.html

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 -password -no-fixed-num-lines -p (printf $argv[1] | sed s/://)

Backup

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:

  #!/usr/bin/env fish
  cp -r $argv[1] $argv[1].bak.(date +"%Y%m%d%H%M%S")

ConnectWifi

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. Well use the nmcli c s command to get the list of the available networks, and well chose one with rofi.

  #!/usr/bin/env fish
  set SELECTEDWIFI (nmcli d w l | \
  egrep -o '([0-9A-F]{2}:){5}[0-9A-F]{2}\s*(.*)Infra' | \
  egrep -o '\s+(.*)\s+' | awk '{$1=$1}1' | \
  rofi -dmenu -p "Select your WiFi network")

Now, if a network was selected, lets attempt to connect to it. Otherwise, lets just send a notification no network was selected.

  if test -z $SELECTEDWIFI
      notify-send "No WiFi network selected" -u low && exit
  end
  nmcli c u $SELECTEDWIFI

TODO fix it

Cppnew

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 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.

#!/usr/bin/env fish

First of all, if no arguments were passed, return an error.

  if ! count $argv >/dev/null
      echo "Missing argument: PROJECT" && return -1
  end

Now, lets set a couple of variables which will prove useful later on when trying to set up our project.

Cnew

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 ~/dev/templateC. This script is a fish script, so lets insert the shebang.

#!/usr/bin/env fish

If no argument was passed, display an error message and exit.

  if ! count $argv > /dev/null
      echo "Missing argument: PROJECT" && return -1
  end

Pass the first argument to a switch statement.

switch "$argv[1]"

If the argument is -h or --help, then display the help message and exit the script normally.

  case -h --help
      man ~/dev/fishfunctions/cnew.man
      exit 0

Else, the argument is the name of the project the user wants to create.

  case '*'
      set -g project_name $argv[1]

Lets close the switch statement.

end

Now, lets copy the template where the user is executing cnew from, give it the name of the project and move to the project.

  cp -r ~/dev/templateC $argv[1]
  cd $argv[1]

The default files have a placeholder for the name of the project. Lets replace these placeholders with the projects name.

  sed -i "s/PROJECTNAME/$argv[1]/g" CMakeLists.txt
  sed -i "s/PROJECTNAME/$argv[1]/g" README.org
  sed -i "s/CPROJECTNAME/$argv[1]/g" doc/Doxyfile

Now, lets create a git repository and initialize it.

  git init
  git add .
  git commit -m "initial commit"

And were done!

Dart Language Server

Spacemacs' recommendations on how to use Dart with LSP is outdated, since dart_language_server is obsolete. As recommended by the repo owner, we should launch instead the following code:

  #!/usr/bin/env fish
  /usr/bin/dart $DART_SDK/snapshots/analysis_server.dart.snapshot --lsp

So, instead of using the obsolete executable, instead we will be calling the analysis server as requested.

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

Emacsmail

This short script is used in my ~/.local/share/applications/mu4e.desktop file in order to send to Emacs any mailto: requests made in my system.

  #!/bin/bash
  emacsclient -c --eval "(browse-url-mail \"$@\")"

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 -p "Select emoji" -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" -u low && exit
  set -a emoji "copied to clipboard"
  notify-send -u low $emoji

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

Lock

lock 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.

  #!/usr/bin/fish
  set TMPBG /tmp/screen.png
  scrot $TMPBG
  corrupter -add 0 $TMPBG $TMPBG
  i3lock -t -e -f -i $TMPBG
  rm $TMPBG

mp42webm

This function allows me to convert easily an mp4 video to the webm format. Nothing too fancy here.

  #!/usr/bin/fish
  ffmpeg -i $argv[1] -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis $argv[1].webm

pape-update

This little tool sets a random wallpaper using nitrogen.

  #!/usr/bin/fish
  set -l PAPESDIR ~/Pictures/Wallpapers
  set -l PAPES (ls $PAPESDIR)
  set -l PAPE $PAPESDIR/$PAPES[(random 1 (count $PAPES))]
  for i in (seq (xrandr --current | grep ' connected' | wc -l))
      nitrogen --set-zoom-fill $PAPE --head=(math "$i - 1") --save
  end

Pinfo

pinfo is a utility that shows system information

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 /media, but you can add more if you wish.

  set -g basemount /media

Get the mount point

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 mount point 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-pass

rofi-pass is a simple utility that gets a password stored in the pass password manager with rofi as its interface, and then stores the password in the clipboard. It is a fish script, so lets declare it as one.

#!/usr/bin/env fish

Lets 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.

  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

Now, lets get the list of the passwords that exist in our pass repository.

  set passwords (find $HOME/.password-store -type f -name "*.gpg" | \
  string replace -r ".*.password-store/" "" | \
  string replace -r ".gpg" "" | sort)

Let the user choose which password they wish to select.

  set password (for elem in $passwords
      echo $elem
  end | rofi -dmenu -i -p "Select your password")

Lets verify we actually selected a password and not just exited. If no password was selected, lets simply exit the script.

  if test -z $password
      exit
  end

Depending on the arguments passed earlier, we might want some different behavior.

  if test $TYPE = "yes"
      <<rofi-pass-type>>
  else
      <<rofi-pass-copy>>
  end

The default behavior is to copy the password to the clipboard for 45 seconds, so lets do that.

  pass show -c $password 2> /dev/null

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. Lets do that.

  set -l IFS
  <<rofi-pass-type-get-password>>
  printf %s $pass | xvkbd -file -

To correctly get the password from pass, we need to parse the output and only get the first line, hence the following command.

set pass (pass show $password | string split -n \n)[1]

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.

  sudo -A umount $chosen && \
  notify-send "💻 USB unmounting" "$chosen unmounted." -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 it has been successfuly unmounted.

  sudo -A umount -l $chosen && \
  notify-send "🤖 Android unmounting" "$chosen unmounted." -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 it has been successfuly unmounted.

  sudo -A umount -l $chosen && \
  notify-send "💿 CD unmounting" "$chosen unmounted." -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

set-screens

set-screens is a small script that allows the user to automatically set up an external monitor

  #!/usr/bin/env fish

First, lets set some variables so we dont have to type in hidden places some values that should be easily modifiable.

  set internal "eDP1"
  set external "HDMI1"

Now, lets 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 its 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 weve 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.

  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}')
      xrandr --output "$external" --primary --auto --mode "$resolution" --right-of "$internal"
  end

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

UpdateFlutter

This is a simple utility to be ran when the flutter package is updated.

  #!/usr/bin/fish
  sudo chown -R :flutterusers /opt/flutter
  sudo chmod -R g+w /opt/flutter
  sudo chmod a+rw /opt/flutter/version
  sudo chown $USER:(id -g $USER) /opt/flutter/bin/cache

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

Weather

A quick and useful script I often use is a curl request to 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.

  #!/usr/bin/env fish
  if count $argv > /dev/null
      set -l SEARCH (string join '+' $argv)
      curl http://v2.wttr.in/~$SEARCH
  else
      curl http://v2.wttr.in/Aubervilliers
  end