2020-05-17 13:04:07 +00:00
#+TITLE : Executable scripts
2020-07-16 12:25:18 +00:00
#+setupfile : headers
2020-01-15 11:38:29 +00:00
#+OPTIONS : auto-id:t
2020-12-01 15:16:59 +00:00
#+HTML_HEAD_EXTRA : <meta name="description" content="Phundrak’ s custom scripts" />
#+HTML_HEAD_EXTRA : <meta property="og:title" content="Phundrak’ s custom scripts" />
#+HTML_HEAD_EXTRA : <meta property="og:description" content="Source code of Phundrak’ s custom scripts" />
2020-01-16 18:48:14 +00:00
#+PROPERTY : header-args :exports code
2020-09-02 11:28:14 +00:00
#+PROPERTY : header-args:emacs-lisp :exports none :tangle no
2019-10-23 08:18:23 +00:00
* Presentation
2020-11-29 22:43:20 +00:00
:PROPERTIES:
:CUSTOM_ID: Presentation-721f3cc4
:END:
2021-02-04 13:43:09 +00:00
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.
2019-10-24 21:10:01 +00:00
2020-09-02 11:28:14 +00:00
* Autostart
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2022-02-26 14:40:15 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env bash" :mkdirp yes :tangle ~/ .local/bin/autostart
2020-11-29 22:43:20 +00:00
:CUSTOM_ID: Autostart-a99e99e7
:END:
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
~set-screens~ is a custom script declared [[*set-screens ][below ]].
#+NAME : autostart-table
2022-03-06 20:43:40 +00:00
| Command | Arguments | Run once? |
|----------------------+------------------------------------------+-----------|
| ~pactl~ | ~load-module module-switch-on-connect~ | |
| ~mpc~ | ~stop~ | no |
2022-06-07 17:20:14 +00:00
| ~xrdb~ | ~-merge "$XDG_CONFIG_HOME"/X11/Xresources~ | no |
2022-03-06 20:43:40 +00:00
| ~picom~ | ~--experimental-backends~ | yes |
| ~set-screens~ | | no |
| ~numlockx~ | ~on~ | yes |
| ~pumopm~ | | yes |
| ~xfce-polkit~ | | yes |
| ~nm-applet~ | | yes |
| ~xwallpaper~ | ~--zoom "$(cat "$HOME"/.cache/wallpaper)"~ | no |
| ~xss-lock~ | ~plock~ | yes |
| ~/usr/lib/kdeconnectd~ | | yes |
2022-03-10 21:25:56 +00:00
| ~dunst~ | | yes |
2020-11-13 14:18:43 +00:00
#+NAME : autostart-gen
2022-02-26 14:40:15 +00:00
#+header : :wrap "src bash :exports code"
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC emacs-lisp :var table=autostart-table :cache yes
2021-10-12 09:31:20 +00:00
(mapconcat (lambda (start-command)
2022-05-22 12:11:58 +00:00
(let* ((command (replace-regexp-in-string "~" "" (nth 0 start-command)))
(arguments (replace-regexp-in-string "~" "" (nth 1 start-command)))
(oncep (string= "yes" (nth 2 start-command)))
(full-command (replace-regexp-in-string
" +"
" "
(format "%s %s &" command arguments))))
(concat (format "which %s && %s"
command
(if (not oncep)
full-command
(format (concat "if pgrep -x %s ; then\n"
" echo %s already running\n"
"else\n"
" %s\n"
" disown\n"
"fi")
command
command
full-command))))))
2021-10-12 09:31:20 +00:00
table
"\n")
2020-11-13 14:18:43 +00:00
#+END_SRC
2022-06-21 14:38:42 +00:00
#+RESULTS[62975f44e46334798ca0fd7d51ea8f83802fe745]: autostart-gen
2022-02-26 14:40:15 +00:00
#+begin_src bash :exports code
2022-05-22 12:11:58 +00:00
which pactl && pactl load-module module-switch-on-connect &
which mpc && mpc stop &
2022-06-21 14:38:42 +00:00
which xrdb && xrdb -merge "$XDG_CONFIG_HOME"/X11/Xresources &
2022-05-22 12:11:58 +00:00
which picom && if pgrep -x picom ; then
echo picom already running
2022-02-26 14:40:15 +00:00
else
2022-05-31 19:08:08 +00:00
picom --experimental-backends &
2022-02-26 14:40:15 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which set-screens && set-screens &
which numlockx && if pgrep -x numlockx ; then
echo numlockx already running
2022-02-26 14:40:15 +00:00
else
2022-05-31 19:08:08 +00:00
numlockx on &
2022-02-26 14:40:15 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which pumopm && if pgrep -x pumopm ; then
echo pumopm already running
2022-02-26 14:40:15 +00:00
else
2022-05-31 19:08:08 +00:00
pumopm &
2022-02-26 14:40:15 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which xfce-polkit && if pgrep -x xfce-polkit ; then
echo xfce-polkit already running
2022-02-26 14:40:15 +00:00
else
2022-05-31 19:08:08 +00:00
xfce-polkit &
2022-02-26 14:40:15 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which nm-applet && if pgrep -x nm-applet ; then
echo nm-applet already running
2022-02-26 14:40:15 +00:00
else
2022-05-31 19:08:08 +00:00
nm-applet &
2022-02-26 14:40:15 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which xwallpaper && xwallpaper --zoom "$(cat "$HOME"/.cache/wallpaper)" &
which xss-lock && if pgrep -x xss-lock ; then
echo xss-lock already running
2022-02-26 14:40:15 +00:00
else
2022-05-31 19:08:08 +00:00
xss-lock plock &
2022-02-26 14:40:15 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which /usr/lib/kdeconnectd && if pgrep -x /usr/lib/kdeconnectd ; then
echo /usr/lib/kdeconnectd already running
2022-03-06 20:43:40 +00:00
else
2022-05-31 19:08:08 +00:00
/usr/lib/kdeconnectd &
2022-03-10 21:25:56 +00:00
disown
fi
2022-05-22 12:11:58 +00:00
which dunst && if pgrep -x dunst ; then
echo dunst already running
2022-03-10 21:25:56 +00:00
else
2022-05-31 19:08:08 +00:00
dunst &
2022-03-06 20:43:40 +00:00
disown
fi
2022-02-26 14:40:15 +00:00
#+end_src
2020-11-13 14:18:43 +00:00
2022-03-06 20:43:40 +00:00
I also have an external sound card, a Scarlet 2i2 G3, that I would
like to use as my default audio output. However, it might not be
always connected, hence the following code:
#+NAME : default-soundcard
#+BEGIN_SRC bash
SOUNDCARD=$(pactl list short sinks | grep "Focusrite")
if [[ -n $SOUNDCARD ]]; then
pactl set-default-sink "$(echo "$SOUNDCARD" | awk '{print $2}')"
fi
#+END_SRC
2020-09-02 11:28:14 +00:00
2021-03-10 12:09:45 +00:00
* Screen utilities
:PROPERTIES:
:CUSTOM_ID: Screen-utilities-f237c566
:END:
** set-screens
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/set-screens
:CUSTOM_ID: set-screens-01bd989a
:END:
~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
2021-10-12 09:31:20 +00:00
set internal "eDP1"
set external "HDMI1"
2021-03-10 12:09:45 +00:00
#+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
2021-10-12 09:31:20 +00:00
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}')
2022-03-01 18:50:02 +00:00
xrandr --output "$external" --primary --auto --mode "$resolution" --left-of "$internal"
2021-10-12 09:31:20 +00:00
end
2021-03-10 12:09:45 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
* cli utilities
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: cli-utilities-ec2120b0
2020-11-29 22:43:20 +00:00
:END:
2021-01-02 12:52:47 +00:00
** Backup
2020-11-29 22:43:20 +00:00
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/backup
:CUSTOM_ID: Backup-68c7c63e
:END:
2021-02-04 13:43:09 +00:00
~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:
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
cp -r $argv[1] $argv[1].bak.(date +"%Y%m%d%H%M%S")
2020-11-13 14:18:43 +00:00
#+END_SRC
2022-04-11 12:50:05 +00:00
** CPU Scaling
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env bash" :mkdirp yes :tangle ~/ .local/bin/cpu-scaling
:CUSTOM_ID: cli-utilities-CPU-Scaling-f64iyk608fj0
:END:
As I am using a laptop, maximum performance isn’ t always what I want.
Sometimes, it’ s just better to have not so great but less
battery-demanding performance. It is possible to achieve this by
modifying the CPU governor with ~cpupower~ . The [[https://wiki.archlinux.org/title/CPU_frequency_scaling#Scaling_governors ][Arch Wiki ]] has, as usual,
some really good documentation on this.
The following script asks the user through ~rofi~ which governor to
apply, and it relies on [[#Askpass-d0d7a8c0 ][~askpass~ ]] to retrieve the user’ s password.
#+begin_src bash
governors=("performance" "powersave" "userspace" "ondemand" "conservative" "schedutil")
governor=$(printf "%s\n" "${governors[@]}" | rofi -dmenu)
sudo -A cpupower frequency-set -g "$governor"
#+end_src
2021-01-02 12:52:47 +00:00
** Development
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: cli-utilities-Development-baec808a
2020-11-29 22:43:20 +00:00
:END:
2021-01-02 12:52:47 +00:00
*** Cppnew :noexport:
2020-11-29 22:43:20 +00:00
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :tangle no
:CUSTOM_ID: Cppnew-964e697b
:END:
2021-02-04 13:43:09 +00:00
=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:
2020-11-13 14:18:43 +00:00
- CMake + [[https://conan.io/ ][Conan ]]
- [[https://mesonbuild.com/ ][Meson ]] + [[https://ninja-build.org/ ][Ninja ]]
- Meson + Ninja + Conan
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
2021-02-04 13:43:09 +00:00
The choice is given to the user which of them to use with options that will be
given to =cppnew= .
2020-11-13 14:18:43 +00:00
First of all, if no arguments were passed, return an error.
#+begin_src fish
2021-10-12 09:31:20 +00:00
if ! count $argv >/dev/null
echo "Missing argument: PROJECT" && return -1
end
2020-11-13 14:18:43 +00:00
#+end_src
2021-02-04 13:43:09 +00:00
Now, let’ s set a couple of variables which will prove useful later on when
trying to set up our project.
2019-12-28 21:10:10 +00:00
2021-01-02 12:52:47 +00:00
*** Cnew
2020-11-29 22:43:20 +00:00
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/cnew
:CUSTOM_ID: Cnew-d9ec9cc4
:END:
2021-02-04 13:43:09 +00:00
=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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
if ! count $argv > /dev/null
echo "Missing argument: PROJECT" && return -1
end
2020-11-13 14:18:43 +00:00
#+END_SRC
Pass the first argument to a switch statement.
#+BEGIN_SRC fish
switch "$argv[1]"
#+END_SRC
2021-02-04 13:43:09 +00:00
If the argument is =-h= or =--help= , then display the help message and exit the
script normally.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case -h --help
man ~/dev/fishfunctions/cnew.man
exit 0
2020-11-13 14:18:43 +00:00
#+END_SRC
Else, the argument is the name of the project the user wants to create.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case '*'
set -g project_name $argv[1]
2020-11-13 14:18:43 +00:00
#+END_SRC
Let’ s close the switch statement.
#+BEGIN_SRC fish
end
#+END_SRC
2021-02-04 13:43:09 +00:00
Now, let’ s copy the template where the user is executing =cnew= from, give it
the name of the project and move to the project.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
cp -r ~/dev/templateC $argv[1]
cd $argv[1]
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
The default files have a placeholder for the name of the project. Let’ s replace
these placeholders with the project’ s name.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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
2020-11-13 14:18:43 +00:00
#+END_SRC
Now, let’ s create a git repository and initialize it.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
git init
git add .
git commit -m "initial commit"
2020-11-13 14:18:43 +00:00
#+END_SRC
And we’ re done!
2019-10-24 21:10:01 +00:00
2021-01-02 12:52:47 +00:00
*** Dart Language Server
2020-11-29 22:43:20 +00:00
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/dart_language_server
:CUSTOM_ID: Dart_Language_Server-18c256b1
:END:
2021-02-04 13:43:09 +00:00
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:
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
/usr/bin/dart $DART_SDK/snapshots/analysis_server.dart.snapshot --lsp
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
So, instead of using the obsolete executable, instead we will be calling the
analysis server as requested.
2019-11-23 16:57:43 +00:00
2021-01-02 12:52:47 +00:00
*** UpdateFlutter
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/UpdateFlutter
:CUSTOM_ID: UpdateFlutter-1e8fbeb7
2020-11-29 22:43:20 +00:00
:END:
2021-01-02 12:52:47 +00:00
This is a simple utility to be ran when the ~flutter~ package is updated.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-10-14 18:05:13 +00:00
** mu-unread
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :tangle ~/ .local/bin/mu-unread
:CUSTOM_ID: cli-utilities-mu-unread-f8t7tf20o8j0
:END:
~mu-unread~ is a very simple utility that simply returns the amount of
unread emails I have through the use of ~mu~ .
2021-11-20 22:04:32 +00:00
As you can see, the output string contains two font switchers for
StumpWM so I can switch from the main font to Siji for the caracter
contained between them: U+E072 (an email icon).
2021-10-14 18:05:13 +00:00
#+begin_src sh
UNREAD=$(mu find "flag:unread AND (maildir:/Inbox OR maildir:/Junk)" | wc -l)
2022-02-04 16:02:30 +00:00
printf "^f2^f0%s" "$UNREAD"
2021-10-14 18:05:13 +00:00
#+end_src
2021-01-02 12:52:47 +00:00
** Pinfo :noexport:
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :tangle no
:CUSTOM_ID: Pinfo-f3644596
2020-11-29 22:43:20 +00:00
:END:
2021-01-02 12:52:47 +00:00
~pinfo~ is a utility that shows system information
2020-04-21 11:05:14 +00:00
2021-11-22 11:46:06 +00:00
** Post scrot script
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :tangle ~/ .local/bin/post-scrot
:CUSTOM_ID: cli-utilities-Post-scrot-script-88ccl7t01aj0
:END:
It is possible to call a script on the resulting image of a ~scrot~
command. Not only do I want to move them to a specific directory, I
2022-02-24 16:13:23 +00:00
also want to be able to see them in ~nsxiv~ (a replacement for ~sxiv~ ) in
case I want to edit the image, copy it or simply delete it (sometimes
I take screenshots by accident).
2021-11-22 11:46:06 +00:00
#+begin_src sh
mv "$@" ~/Pictures/Screenshots/
2022-02-24 16:13:23 +00:00
nsxiv -abfs f "$HOME/Pictures/Screenshots/ $*"
2021-11-22 11:46:06 +00:00
#+end_src
2021-01-02 12:52:47 +00:00
** sshbind
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/sshbind
:CUSTOM_ID: sshbind-756fabb1
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
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~ .
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
ssh -L $argv[1]:$argv[3]:$argv[1] $argv[2] -N
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-02-24 20:40:04 +00:00
2022-02-24 16:13:23 +00:00
** Nsxiv key handler
2022-02-17 23:02:11 +00:00
:PROPERTIES:
:HEADER-ARGS: :mkdirp yes :tangle no :noweb yes
:CUSTOM_ID: cli-utilities-Sxiv-key-handler-atganx41adj0
:END:
2022-02-24 16:13:23 +00:00
One thing I like with ~nsxiv~ is you can trigger different behaviors
based on keypresses. For instance, with my current nsxiv configuration,
2022-02-17 23:02:11 +00:00
if I press the space key followed by a character, it can delete to the
trashcan, delete definitively, or open the current image in GIMP. All
of that is done through one script file stored in
2022-02-24 16:13:23 +00:00
~$HOME/.config/nsxiv/exec/key-handler~ . The fact it reacts to first the
spacebar instead of /Ctrl-x/ is because I use a custom version of nsxiv I
2022-02-17 23:02:11 +00:00
first modified to fit the bépo layout, and then I decided to change
the prefix to fit how I use Emacs and StumpWM. You can read the
2022-02-24 16:13:23 +00:00
PKGBUILD and my nsxiv patch [[https://labs.phundrak.com/phundrak/dotfiles/src/branch/master/Documents/code/PKGBUILDs/sxiv ][in my dotfiles repo ]].
2022-02-17 23:02:11 +00:00
2022-02-24 16:13:23 +00:00
#+header : :shebang "#!/usr/bin/env fish" :tangle ~/.config/nsxiv/exec/key-handler
2022-02-17 23:02:11 +00:00
#+begin_src fish
2022-02-24 16:13:23 +00:00
<<nsxiv-read-files >>
2022-02-17 23:02:11 +00:00
2022-02-24 16:13:23 +00:00
<<nsxiv-switch-statement >>
2022-02-17 23:02:11 +00:00
#+end_src
Here is a step by step explanation of the source code. First, we want
2022-02-24 16:13:23 +00:00
to store in the variable ~FILES~ all the files marked in nsxiv. This is
2022-02-17 23:02:11 +00:00
done with a ~while~ loop and the ~read~ command.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-read-files
2022-02-17 23:02:11 +00:00
#+begin_src fish
while read file
set -g FILES "$file" $FILES
end
#+end_src
We can then read from the first member of ~argv~ which key the user
pressed. Depending on it, we can chose what to execute.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-switch-statement
2022-02-17 23:02:11 +00:00
#+begin_src fish
switch "$argv[1]"
2022-02-24 16:13:23 +00:00
<<nsxiv-trash >>
<<nsxiv-rm >>
<<nsxiv-gimp >>
<<nsxiv-jpeg >>
<<nsxiv-rotate-clockwise >>
<<nsxiv-rotate-counter-clockwise >>
<<nsxiv-yank >>
2022-02-17 23:02:11 +00:00
end
#+end_src
The first option with the letter ~d~ is to move the file to the trash
can. For this, we use the ~trash~ command from ~trash-cli~ .
2022-02-24 16:13:23 +00:00
#+name : nsxiv-trash
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "d"
trash $FILES
#+end_src
In case we want to definitively delete a file without using the trash
can, we can use ~rm~ instead when we press the ~D~ key.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-rm
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "D"
rm $FILES
#+end_src
2022-02-24 16:13:23 +00:00
It’ s not rare I want to modify an image I have open in nsxiv,
2022-02-17 23:02:11 +00:00
especially since screenshots are automatically open in this image
viewer aften they are taken. In that case, a simple command will do.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-gimp
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "g"
gimp $FILES
#+end_src
2022-02-24 16:13:23 +00:00
Often, I use nsxiv to convert an image to a jpeg file, because my
2022-02-17 23:02:11 +00:00
internet connection is not that great and jpeg screenshots are faster
to upload than png screenshots. So what I do is for each file
selected, I take the base name of the file (i.e. remove its
extension), and then I use the ~convert~ command from ~imagemagik~ to
convert it from its original format to a jpg format --- ~imagemagik~
detects the formats based on the extension.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-jpeg
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "j"
for f in $FILES
set basename (echo "$f" | sed 's/\.[^.]*$/ /')
convert "$f" "$basename.jpg"
end
#+end_src
I have two commands to rotate my files, and both only differ by the
angle of rotation. Once again I rely on ~convert~ in both cases. To
rotate clockwise, this is the code needed.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-rotate-clockwise
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "r"
for f in $FILES
convert -rotate 90 "$f" "$f"
end
#+end_src
On the other hand, to rotate counter-clockwise, we need this code:
2022-02-24 16:13:23 +00:00
#+name : nsxiv-rotate-counter-clockwise
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "R"
for f in $FILES
convert -rotate 270 "$f" "$f"
end
#+end_src
Lastly, when I want to copy a file, I just hit the ~y~ key for “yank”
(that’ s a term from Emacs). For that, I rely on ~file~ to tell me the
mimetype of the image, then I can pass it to ~xclip~ along with the
filename to copy it to the clipboard. In this case, we only copy the
first of the selected files since the clipboard cannot hold several
files at once.
2022-02-24 16:13:23 +00:00
#+name : nsxiv-yank
2022-02-17 23:02:11 +00:00
#+begin_src fish
case "y"
set FILE "$FILES[1]"
set TYPE (file -i "$FILE" | sed -r 's|.*(image/[a-z]+);.* |\1|')
xclip -sel clip -t "$TYPE" -i "$FILE"
#+end_src
2021-01-02 12:52:47 +00:00
** Starwars
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/starwars
:CUSTOM_ID: Starwars-654f8637
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
This is a one-liner that allows you to watch Star Wars episode 4 in ASCII art in
your terminal. Here is the code:
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
telnet towel.blinkenlights.nl
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-06-14 18:58:07 +00:00
2021-01-02 12:52:47 +00:00
** Toggle touchpad tapping
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/tttapping
:CUSTOM_ID: Toggle_touchpad_tapping-23348b00
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
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.
2020-05-17 13:04:07 +00:00
2021-02-04 13:43:09 +00:00
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:
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set TPNAME "ELAN0412:00 04F3:3162 Touchpad"
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-10-29 15:51:54 +00:00
2021-01-02 12:52:47 +00:00
Let’ s now get the identifier of the touchpad for ~xinput~ :
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set TPID (xinput list | grep $TPNAME | awk '{print $6}' | sed 's|id=\(.*\)|\1|g')
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Now, let’ s detect the current status of the touchpad:
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set TPSTATUS (xinput list-props $TPID | grep "Tapping Enabled" | \
grep -v "Default" | awk '{print $5}')
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
test [[ $TPSTATUS = "1" ]] && set NEWTPSTATUS 0 || set NEWTPSTATUS 1
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Finally, let’ s update the touchpad’ s options:
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
xinput set-prop $TPNAME "libinput Tapping Enabled" $NEWTPSTATUS
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 12:51:34 +00:00
2021-01-02 12:52:47 +00:00
** Wacom setup
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2022-03-01 18:50:40 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :mkdirp yes :tangle ~/ .local/bin/wacom-setup
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Wacom_setup-331fb024
2020-11-29 22:43:20 +00:00
:END:
2022-03-01 18:50:40 +00:00
I made a small and quick utility to set up my Wacom tablet so it is
only bound to one screen. This is quite easy, we simply need to find
the Wacom stylus’ ID and assign it to the display we want.
2019-10-23 08:18:23 +00:00
2022-03-01 18:50:40 +00:00
#+begin_src sh
ID=$(xinput | grep -oPi "wacom.+stylus.+id= \K([0-9]+)")
SCREEN=$(xrandr -q --current | \
grep -iPo '^([^ ])+(?= connected)' | \
rofi -dmenu -i -p 'Select your display' | \
tr -d '\n')
xinput map-to-output "$ID" "$SCREEN"
#+end_src
2020-11-13 14:18:43 +00:00
2021-01-27 18:03:11 +00:00
* Emacs stuff
:PROPERTIES:
:CUSTOM_ID: Emacs-stuff-8e76efd4
:END:
** Dired
:PROPERTIES:
:CUSTOM_ID: Emacs-stuff-Dired-2eeca9da
:HEADER-ARGS: :shebang "#!/bin/bash" :mkdirp yes :tangle ~/ .local/bin/dired
:END:
#+BEGIN_SRC bash
2022-02-26 14:40:15 +00:00
emacsclient -c -a emacs -e "(dired \"$*\")"
2021-01-27 18:03:11 +00:00
#+END_SRC
** Emacsmail
2021-01-02 12:52:47 +00:00
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/bin/bash" :mkdirp yes :tangle ~/ .local/bin/emacsmail
:CUSTOM_ID: Emacsmail-afffb7cd
:END:
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC bash
2022-02-26 14:40:15 +00:00
emacsclient -c -n -a emacs -e "(browse-url-mail \"$*\")"
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
* Media
:PROPERTIES:
:CUSTOM_ID: Media-f869f942
:END:
** mp42webm
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/mp42webm
:CUSTOM_ID: mp42webm-aeacca58
:END:
2021-02-04 13:43:09 +00:00
This function allows me to convert easily an mp4 video to the webm format.
Nothing too fancy here.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
ffmpeg -i $argv[1] -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis $argv[1].webm
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
** youtube-dl wrappers
:PROPERTIES:
:CUSTOM_ID: Media-youtube-dl-wrappers-8d8f5f71
:END:
*** ytplay
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/ytplay
2021-11-09 12:38:56 +00:00
:CUSTOM_ID: Media-youtube-dl-wrappers-ytplay-z6ka39h0m9j0
2021-01-02 12:52:47 +00:00
:END:
2021-02-04 13:43:09 +00:00
~ytplay~ is a simple script I’ ve written that allows me to play in mpv any
YouTube video at the desired resolution. The script relies on ~dmenu~ (or ~rofi~
in dmenu-mode), ~youtube-dl~ and of course ~mpv~ itself.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set URL (rofi -dmenu -i -p "Video URL")
if test -n "$URL"
set FORMAT \
(youtube-dl --list-formats "$URL" | \
egrep "webm.*[0-9]+x[0-9]+ " | \
awk '{print $3 " " $1}' | \
sort -gu | \
rofi -dmenu -i -p "Resolution" | \
string split " ")
set FCODE $FORMAT[2]
mpv --ytdl-format=$FCODE+bestaudio/best "$URL"
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
I’ ll even add a ~.desktop~ entry for this script:
#+BEGIN_SRC conf-desktop :tangle ~/.local/share/applications/ytplay.desktop :mkdirp yes
2021-10-12 09:31:20 +00:00
[Desktop Entry]
Type=Application
Version=1.0
Name=ytplay (YouTube in mpv)
Comment=Play YouTube videos in mpv
Exec=/home/phundrak/ .local/bin/ytplay
Path=/home/phundrak/ .local/bin
Terminal=false
Categories=Media
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
*** ytdl - a ~youtube-dl~ wrapper
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/ytdl
:HEADER-ARGS:EMACS-LISP: :exports none :tangle no
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-03bd63e0
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
This script is a wrapper around ~youtube-dl~ which I use mainly for archiving
YouTube videos on my NAS (at the time I’ m writing this, I have already 2.1TB
worth of videos archived). The principle behind this script is quite simple: I
want to avoid as much as possible to redownload any video already downloaded in
order to avoid pinging too much YouTube’ s servers, 429 Too Many Requests errors
are really annoying, and it comes really early when you have only a couple of
new videos to download among the few 14k videos already downloaded.
2020-11-13 14:18:43 +00:00
2021-02-04 13:43:09 +00:00
Be aware this script was written for the Fish shell (3.1.0 and above), and makes
use of youtube-dl 2020.03.24 and above, [[https://github.com/jorgebucaran/fish-getopts ][Fish getopts ]] and [[https://github.com/BurntSushi/ripgrep ][ripgrep ]].
2020-11-13 14:18:43 +00:00
2021-01-02 12:52:47 +00:00
**** Setting default values
:PROPERTIES:
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Setting-default-values-da404639
:END:
2021-02-04 13:43:09 +00:00
Some variables in this script will have default values, we do not want to have a
mile-long command each time we wish to download a single video. We’ ll also set
some global variables that won’ t change:
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-default-vars
| Variable Name | Default Value | String? |
|------------------+-----------------------------------------------------------+---------|
| YTDL_SHARED_DIR | $HOME/.local/share/ytdl | no |
| FORMAT_DEFAULT | %(uploader)s/%(upload_date)s - %(title)s.%(ext)s | yes |
| DOWNFILE_DEFAULT | $YTDL_SHARED_DIR/downloaded | no |
| ERRFILE_DEFAULT | $YTDL_SHARED_DIR/video-errors | no |
| LOGFILE_DEFAULT | $YTDL_SHARED_DIR/ytdl.log | no |
| PREFFERED_FORMAT | bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio | yes |
| VERSION | 0.3 | yes |
2020-11-13 14:18:43 +00:00
2021-02-04 13:43:09 +00:00
There is one more default variable pointing to ytdl’ s root directory which
depends on whether the videos directory has a French or English name:
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-default-vars-root
#+BEGIN_SRC fish :tangle no
2021-10-12 09:31:20 +00:00
if test -d "$HOME/Vidéos"
set -g ROOTDIR_DEFAULT "$HOME/Vidéos" # French name
else
set -g ROOTDIR_DEFAULT "$HOME/Videos" # English name
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-default-vars-make
#+BEGIN_SRC emacs-lisp :var vars=ytdl-default-vars
2021-10-12 09:31:20 +00:00
(mapconcat (lambda (var)
(let ((varname (car var))
(varvalue (cadr var))
(string? (string= (nth 2 var) "yes")))
(format "set -g %-16s %s" varname (if string? (format "\"%s\"" varvalue)
varvalue))))
vars
"\n")
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
#+RESULTS : ytdl-default-vars-make
: set -g YTDL_SHARED_DIR $HOME/.local/share/ytdl
: set -g FORMAT_DEFAULT "%(uploader)s/%(upload_date)s - %(title)s.%(ext)s"
: set -g DOWNFILE_DEFAULT $YTDL_SHARED_DIR/downloaded
: set -g ERRFILE_DEFAULT $YTDL_SHARED_DIR/video-errors
: set -g LOGFILE_DEFAULT $YTDL_SHARED_DIR/ytdl.log
: set -g PREFFERED_FORMAT "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio"
: set -g VERSION "0.3"
2020-11-13 14:18:43 +00:00
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish :noweb yes
2021-10-12 09:31:20 +00:00
<<ytdl-default-vars-make() >>
<<ytdl-default-vars-root >>
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We’ ll also create the directory pointed at by ~YTDL_SHARED_DIR~ if it doesn’ t
exist already:
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
mkdir -p $YTDL_SHARED_DIR
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
**** Help message
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Help-message-3773aacd
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
The next step is displaying the help message for the script. For that, just a
long string echo’ d will do, wrapped in the function ~_ytdl_help~ .
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_help
echo "Usage: ytdl [OPTION]... URL [URL]...
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-4, --ipv4
Download with forced IPv4
Default: no
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-6, --ipv6
Download with forced IPv6
Default: no
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-a, --batch-file <file >
File containing URLs to download, one URL per line. Lines starting with
'#', ';' or ']' are considered as comments and ignored.
Default: None
2019-10-23 10:00:28 +00:00
2021-10-12 09:31:20 +00:00
-c, --id-cache <file >
File containing the video IDs that were already downloaded, one ID per
line.
Default: $DOWNFILE_DEFAULT
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-d, --directory <dir >
Root directory in which to download videos.
Default: $ROOTDIR_DEFAULT
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-e, --error-file <file >
File containing the IDs of videos that failed to download, one ID per
line
Default: $ERRFILE_DEFAULT
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-f, --format <format >
Format name for downloaded videos, including path relative to root
directory
Default: $FORMAT_DEFAULT
2020-11-13 14:18:43 +00:00
2021-10-12 09:31:20 +00:00
-l, --logs <file >
File in which to store logs.
Default: $LOGFILE_DEFAULT
2021-01-02 12:52:47 +00:00
2021-10-12 09:31:20 +00:00
-V, --verbose
Show verbose output
Default: no
2021-01-02 12:52:47 +00:00
2021-10-12 09:31:20 +00:00
-v, --version
Show version of ytdl.
2021-01-02 12:52:47 +00:00
2021-10-12 09:31:20 +00:00
-h, --help
Shows this help message"
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We also have the function ~_ytdl_version~ to display the current version of
~ytdl~ :
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_version
echo "ytdl 0.3, developped for fish 3.1.0 and youtube-dl 2020.03.24 or newer"
echo "requires Fish getopts <https://github.com/jorgebucaran/fish-getopts >"
echo "and ripgrep <https://github.com/BurntSushi/ripgrep >"
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
**** Arguments Handling
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Arguments-Handling-1daebbe8
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
The function ~_ytdl_parse_ops~ is a little bit trickier: we use ~getopts~ to
parse the arguments passed to the script in order to get some preferences from
the user. Here is a quick reference on what options are available and what they
do:
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-table-arguments
| Short | Long | Takes a value? | Associated Variable | Default Value | What it does |
|-------+------------+----------------+---------------------+-------------------+----------------------|
| 4 | ipv4 | no | IPV4 | None | Force IPv4 |
| 6 | ipv6 | no | IPV6 | None | Force IPv6 |
| a | batch-file | yes | FILE | None | Batch file |
| c | cache | yes | DOWNFILE | $DOWNFILE_DEFAULT | Cache file |
| d | directory | yes | ROOTDIR | $ROOTDIR_DEFAULT | Root directory |
| e | error-file | yes | ERRFILE | $ERRFILE_DEFAULT | Error logs |
| f | format | yes | FORMAT | $FORMAT_DEFAULT | Filename format |
| l | logs | yes | LOGFILE | $LOGFILE_DEFAULT | Logs |
| V | verbose | no | VERBOSE | 1 | Verbose output |
| v | version | command | None | None | Script version |
| h | help | command | None | None | Display this message |
2020-11-13 14:18:43 +00:00
2021-02-04 13:43:09 +00:00
We can also pass individual YouTube URLs without any options or switches
associated to them, they will be downloaded as part of a single queue.
2020-11-13 14:18:43 +00:00
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-arg-handling-gen
#+BEGIN_SRC emacs-lisp :var args=ytdl-table-arguments
2021-10-12 09:31:20 +00:00
(mapconcat (lambda (arg)
(let* ((short (format "%s" (nth 0 arg)))
(long (nth 1 arg))
(arg? (string= "yes" (nth 2 arg)))
(var (unless (string= "None" (nth 3 arg))
(nth 3 arg))))
(format "case %s %s\n\t%s"
short long
(if var (format "set -g %s %s" var
(if arg? "$value" ""))
(format "_ytdl_ %s && exit"
(if (string= "h" short) "help" "version"))))))
args
"\n")
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
#+RESULTS : ytdl-arg-handling-gen
#+begin_example
case 4 ipv4
2021-05-21 12:36:38 +00:00
set -g IPV4
2021-01-02 12:52:47 +00:00
case 6 ipv6
2021-05-21 12:36:38 +00:00
set -g IPV6
2021-01-02 12:52:47 +00:00
case a batch-file
2021-05-21 12:36:38 +00:00
set -g FILE $value
2021-01-02 12:52:47 +00:00
case c cache
2021-05-21 12:36:38 +00:00
set -g DOWNFILE $value
2021-01-02 12:52:47 +00:00
case d directory
2021-05-21 12:36:38 +00:00
set -g ROOTDIR $value
2021-01-02 12:52:47 +00:00
case e error-file
2021-05-21 12:36:38 +00:00
set -g ERRFILE $value
2021-01-02 12:52:47 +00:00
case f format
2021-05-21 12:36:38 +00:00
set -g FORMAT $value
2021-01-02 12:52:47 +00:00
case l logs
2021-05-21 12:36:38 +00:00
set -g LOGFILE $value
2021-01-02 12:52:47 +00:00
case V verbose
2021-05-21 12:36:38 +00:00
set -g VERBOSE
2021-01-02 12:52:47 +00:00
case v version
2021-05-21 12:36:38 +00:00
_ytdl_version && exit
2021-01-02 12:52:47 +00:00
case h help
2021-05-21 12:36:38 +00:00
_ytdl_help && exit
2021-01-02 12:52:47 +00:00
#+end_example
2021-02-04 13:43:09 +00:00
The following shows how ~getopts~ is used to catch the options and switches
passed to the script:
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-getopts
#+BEGIN_SRC fish :noweb yes :tangle no
2021-10-12 09:31:20 +00:00
getopts $argv | while read -l key value
switch $key
<<ytdl-arg-handling-gen() >>
case _
for v in $value
set -g VIDEOS $VIDEOS $v
end
end
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-arg-set-default-value-gen
#+BEGIN_SRC emacs-lisp :var args=ytdl-table-arguments
2021-10-23 16:24:45 +00:00
(let* ((args (seq-filter (lambda (arg)
(let* ((var (unless (string= "None" (nth 3 arg)) (nth 3 arg)))
(default (format "%s" (nth 4 arg)))
(default (unless (string= "None" default) default)))
(and var default)))
args)))
2021-10-12 09:31:20 +00:00
(mapconcat (lambda (arg)
(let* ((var (nth 3 arg))
(default (format "%s" (nth 4 arg))))
(format "if set -q $%s\n\tset -g %s %s\nend"
var var default)))
args
"\n"))
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
#+RESULTS : ytdl-arg-set-default-value-gen
#+begin_example
if set -q $DOWNFILE
2021-05-21 12:36:38 +00:00
set -g DOWNFILE $DOWNFILE_DEFAULT
2020-11-13 14:18:43 +00:00
end
2021-01-02 12:52:47 +00:00
if set -q $ROOTDIR
2021-05-21 12:36:38 +00:00
set -g ROOTDIR $ROOTDIR_DEFAULT
2021-01-02 12:52:47 +00:00
end
if set -q $ERRFILE
2021-05-21 12:36:38 +00:00
set -g ERRFILE $ERRFILE_DEFAULT
2021-01-02 12:52:47 +00:00
end
if set -q $FORMAT
2021-05-21 12:36:38 +00:00
set -g FORMAT $FORMAT_DEFAULT
2021-01-02 12:52:47 +00:00
end
if set -q $LOGFILE
2021-05-21 12:36:38 +00:00
set -g LOGFILE $LOGFILE_DEFAULT
2021-01-02 12:52:47 +00:00
end
if set -q $VERBOSE
2021-05-21 12:36:38 +00:00
set -g VERBOSE 1
2021-01-02 12:52:47 +00:00
end
#+end_example
2020-11-13 14:18:43 +00:00
2021-02-04 13:43:09 +00:00
Some values need to be set to their default, so let’ s assign them their value if
no user value was passed:
2021-01-02 12:52:47 +00:00
#+NAME : ytdl-arg-set-default-value
#+BEGIN_SRC fish :noweb yes :tangle no
2021-10-12 09:31:20 +00:00
<<ytdl-arg-set-default-value-gen() >>
set -g FORMAT "$ROOTDIR/$FORMAT"
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Both these code blocks are executed in ~_ytdl_parse_ops~ :
#+BEGIN_SRC fish :noweb yes
2021-10-12 09:31:20 +00:00
function _ytdl_parse_ops
<<ytdl-getopts >>
<<ytdl-arg-set-default-value >>
end
2021-01-02 12:52:47 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
**** Logging
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Logging-f4b9815e
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
~_ytdl_log~ is a very simple function used for logging information for the user
in the file pointed to by ~LOGFILE~ . The first argument the function should
receive is its log level. I generally use either ~"INFO"~ or ~"ERR"~ . The second
argument is the message to log.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_log
set -l INFOLEVEL $argv[1]
set -l MSG $argv[2]
set -l LOG (printf "[%s] %s %s\n" $INFOLEVEL (date +"%F %T") $MSG)
printf "%s\n" $LOG >> $LOGFILE
if test $VERBOSE -eq 1
echo $LOG
end
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Download a Single Video
:PROPERTIES:
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Download-a-Single-Video-afedf321
:END:
2021-02-04 13:43:09 +00:00
In order to download a single video, a simple function has been written for this
that will display when downloaded how far it is down the list of videos to be
downloaded and it will add its ID to the file listing all videos downloaded. The
script will also try to download the video according to the ~PREFFERED_FORMAT~
variable, but if the download fails it will download the default format selected
by ~youtube-dl~ . If both downloads fail, the ID of the video will be added to
the list of failed videos. If one of the downloads succeeds, it will remove the
ID from the list of failed downloads.
The first argument of the function is the video ID from YouTube, the second
argument is the position of the video in the queue, and the third argument is
the queue length – can be the amount of videos in a whole YouTube channel, the
amount of videos in a playlist, or simply the amount of YouTube URLs passed as
arguments to the script.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_download_video
set ID $argv[1]
_ytdl_log "INFO" (printf "Downloading video with ID $ID (%4d/%4d)" $argv[2] $argv[3])
if youtube-dl -f $PREFFERED_FORMAT -ciw -o $FORMAT "https://youtube.com/watch?v=$ID"
echo $ID >> $DOWNFILE
else if youtube-dl -ciw -o $FORMAT "https://youtube.com/watch?v=$ID"
echo $ID >> $DOWNFILE
else
_ytdl_log "ERR" "Could not download $VIDEO"
echo $ID >> $ERRFILE
end
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
/Note that this function is not meant to be called without any checks before./
It is meant to be called by ~_ytdl_download_queue~ described below.
2021-01-02 12:52:47 +00:00
**** Download a Queue of Videos
:PROPERTIES:
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Download-a-Queue-of-Videos-6ef8d51f
:END:
2021-02-04 13:43:09 +00:00
One of the main goals of this tool is to check if a video has already been
downloaded. This is why, as you will see below, we use ripgrep to check if the
ID of the video we want to download is already present in the list of downloaded
videos. If not, it will then be downloaded though ~_ytdl_download_video~
described above.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_download_queue
for i in (seq (count $argv))
rg -- $argv[$i] $DOWNFILE 2&> /dev/null
if test $status -ne 0
_ytdl_download_video $argv[$i] $i (count $argv)
end
end
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Download Videos From Arguments
:PROPERTIES:
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Download-Videos-From-Arguments-57a5dac1
:END:
2021-02-04 13:43:09 +00:00
The main aim of this function is to transform the URLs contained in the
arguments passed to the script to a list of IDs usable later on by ~ytdl~ .
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_download_arg_urls
set -g IDs
for VIDEO in $argv
_ytdl_log "Info" "Getting video ID for $VIDEO"
set -g IDs $IDs (youtube-dl --get-id $VIDEO)
end
_ytdl_download_queue $IDs
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Download Videos From a Batch File
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Download-Videos-From-a-Batch-File-0f1382c4
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
The final function to declare before the main body of the script is
~_ytdl_download_batch~ : it will look for each line, ignoring the ones beginning
by ~#~ , ~;~ and ~]~ (just like ~youtube-dl~ ) and will download them, assuming
these are channel URLs or playlist URLs, however it should also work with direct
video URLs.
What this function does is for each line, it will fetch the entierty of the
video IDs found in a playlist or channel. Then, it will look each ID up the list
of already downloaded videos and will add all new IDs to a queue of videos to be
downloaded. It will then pass each new video ID to ~_ytdl_download_video~
directly. Beware that if you pass directly the URL of the channel, such as
~https://www.youtube.com/user/enyay~ if you want to download Tom Scott’ s videos,
it will download everything on the main page of their channel, which means it
will even download videos from playlists they decided to put on their channel’ s
front page, even if it is not theirs. So in that case, we need to append
~/videos~ to any channel URL.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function _ytdl_download_batch
set -q $FILE
if test $status -eq 1
set -g NEW
set CHANNELS (cat $FILE | grep -vE "#|;|\]")
for c in $CHANNELS
_ytdl_log "INFO" "Getting IDs for channel $c"
if test (egrep '\/c\/ |user|channel' (echo $c |psub))
set -g IDS (youtube-dl --get-id "$c/videos")
else
set -g IDS (youtube-dl --get-id $c)
end
_ytdl_log "INFO" "Fetching new videos from channel"
for i in (seq (count $IDS))
printf "\rsearching (%d/%d)" $IDn (count $IDS)
rg -- $IDS[$i] $DOWNFILE 2&> /dev/null
if test $status -ne 0
set -g NEW $IDS[$i] $NEW
end
end
printf "\n"
end
for i in (seq (count $NEW))
_ytdl_download_video $NEW[$i] $i (count $NEW)
end
end
end
2021-01-02 12:52:47 +00:00
#+END_SRC
**** Main Body
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: ytdl-a-youtube-dl-wrapper-Main-Body-8a06cb9e
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
Now that we have all our functions declared, let’ s call them! First, we need to
parse our arguments. We’ ll then download all files passed as arguments. Finally,
we’ ll download videos, playlists and channels specified from a batch file.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
_ytdl_parse_ops $argv
_ytdl_download_arg_urls $VIDEOS
_ytdl_download_batch
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
And that’ s all! If you’ re interested with a very simple interface for
downloading one video once, I wrote a small [[#rofi-ytdl-ff8f789d ][~rofi-ytdl~ ]] script that calls the
~rofi~ utility to specify a single link and download it.
2021-01-02 12:52:47 +00:00
* Plock
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/plock
:CUSTOM_ID: Lock-635fcb38
:END:
2021-02-04 13:43:09 +00:00
~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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set TMPBG /tmp/screen.png
scrot $TMPBG
corrupter -add 0 $TMPBG $TMPBG
i3lock -t -e -f -i $TMPBG
rm $TMPBG
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
* Polybar-launch (Deprecated) :noexport:
:PROPERTIES:
2021-11-23 14:01:06 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env bash" :mkdirp yes :tangle ~/ .local/bin/polybar-launch
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Polybar-launch-36789edc
:END:
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC bash
killall -q polybar
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
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
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC bash
2021-10-12 09:31:20 +00:00
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
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And we’ re done! Let’ s just launch a notification polybar has been relaunched.
#+BEGIN_SRC bash
2021-10-12 09:31:20 +00:00
notify-send "Polybar restarted!" -a "polybar-launch"
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
* Rofi utilities
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-utilities-650338d1
2020-11-29 22:43:20 +00:00
:END:
2021-01-02 12:52:47 +00:00
** askpass
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/askpass
:CUSTOM_ID: Askpass-d0d7a8c0
:END:
2021-02-04 13:43:09 +00:00
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
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
rofi -dmenu -password -no-fixed-num-lines -p (printf $argv[1] | sed s/:/ /)
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
** awiki
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/awiki
:CUSTOM_ID: awiki-7ac5e1d5
:END:
2021-02-04 13:43:09 +00:00
~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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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)
set WPAGE (echo $WPAGE | sed -r 's/\s+/_/g')
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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~ .
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
xdg-open $WLOCATION$WPAGE.html
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
** ConnectWifi :noexport:
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/connect-wifi
:CUSTOM_ID: ConnectWifi-16e5e24a
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
~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~ .
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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")
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Now, if a network was selected, let’ s attempt to connect to it. Otherwise, let’ s
just send a notification no network was selected.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
if test -z $SELECTEDWIFI
notify-send "No WiFi network selected" -u low && exit
end
nmcli c u $SELECTEDWIFI
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
*** TODO fix it
:PROPERTIES:
:CUSTOM_ID: ConnectWifi-fix_it-a4b11503
:END:
** dmenu
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/dmenu
:CUSTOM_ID: Dmenu-527edf04
:END:
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
rofi -dmenu $argv
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
** Emoji picker
:PROPERTIES:
2022-02-04 16:02:30 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env bash" :mkdirp yes :tangle ~/ .local/bin/rofi-emoji
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Emoji_picker-a1c374ec
:END:
2021-02-04 13:43:09 +00:00
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= .
2022-02-04 16:02:30 +00:00
#+BEGIN_SRC bash
2021-10-12 09:31:20 +00:00
grep -v "#" ~/.config/emoji.txt | rofi -dmenu -p "Select emoji" -i | \
2022-02-04 16:02:30 +00:00
awk '{print $1}' | tr -d '\n' | xclip -selection clipboard
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Also, let’ s send a notification telling the user the emoji has been copied!
2022-02-04 16:02:30 +00:00
#+BEGIN_SRC bash
EMOJI=$(xclip -o -selection clipboard | tr -d '\n')
2022-05-19 16:22:31 +00:00
test -z "$EMOJI" && notify-send "No emoji copied" -u low && exit
2022-02-04 16:02:30 +00:00
EMOJI="$EMOJI copied to clipboard"
notify-send -u low "$EMOJI"
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-02-04 13:43:09 +00:00
It is inspired from [[https://www.youtube.com/watch?v=UCEXY46t3OA ][this video ]] from [[https://lukesmith.xyz/ ][Luke Smith ]], rewritten in Fish.
2021-01-02 12:52:47 +00:00
** Partition mounting and unmounting
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-utilities-Partition-mounting-and-unmounting-9492ff60
2020-11-29 22:43:20 +00:00
:END:
2021-01-02 12:52:47 +00:00
*** Rofi-mount
:PROPERTIES:
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/rofi-mount
:CUSTOM_ID: Rofi-mount-ebbebf68
:END:
2021-02-04 13:43:09 +00:00
=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.
2020-11-13 14:18:43 +00:00
2021-01-02 12:52:47 +00:00
**** Get the mountable elements
:PROPERTIES:
:CUSTOM_ID: Rofi-mount-Get_the_mountable_elements-24db7834
:END:
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
begin
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
set -l LFS
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Now, let’ s detect the amount of mountable Android filesystems, and if any are
detected, let’ s read them into a global variable.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set -l a (math (jmtpfs -l | wc -l) - 2)
test $a -ge 0 && jmtpfs -l 2> /dev/null | tail -n $a | read -zg anddrives
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We’ ll do the same for external and internal drives and partitions that can be
mounted here.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
lsblk -rpo "name,type,size,mountpoint" | \
awk '$2=="part"&&$4= =""{printf "%s (%s)\n",$1,$3}' | \
read -zg usbdrives
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
Finally, we look for any CD drive that could be mounted on our device.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
blkid /dev/sr0 | awk '{print $1}' | sed 's/ ://' | read -z cddrives
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And that’ s the end of our first block!
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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).
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set -g TMPDRIVES /tmp/drives
rm -f $TMPDRIVES
touch $TMPDRIVES
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Now, let’ s write what type of drives we can mount in this temporary file.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
test -n "$usbdrives" && echo "USB" >> $TMPDRIVES
test -n "$cddrives" && echo "CD" >> $TMPDRIVES
test -n "$anddrives" && echo "Android" >> $TMPDRIVES
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set -g basemount /media
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
**** Get the mount point
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-mount-Get_the_mount_point-6c4bac06
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
Now, let’ s declare a function that will allow us to chose the drive we want to
mount.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
function getmount
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set -g mp (for d in $basemount
find $d -maxdepth 5 -type d
end | rofi -dmenu -i -p 'Type in mount point.')
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We should verify that something has been actually selected, otherwise we should
abort the script.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
if test -z $mp || test $mp = ""
return 1
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Finally, let’ s close the function
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 10:00:28 +00:00
2021-01-02 12:52:47 +00:00
**** Mount a USB drive, hard drive or partition
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-mount-Mount_a_USB_drive,_hard_drive_or_partition-f5431dbe
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
function mountusb
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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= .
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set -g chosen (echo $usbdrives | \
rofi -dmenu -i -p "Mount which drive?" | \
awk '{print $1}')
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
As usual after a user selection, let’ s verify something has actually been
selected. If not, let’ s abort the script.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
test -z $chosen && return 1
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
getmount
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-01-29 20:23:15 +00:00
2021-02-04 13:43:09 +00:00
Let’ s verify the variable =mp= set in =getmount= is not empty, otherwise abort
the script.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
test -z $mp && return 1
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-10-09 16:06:52 +00:00
2021-02-04 13:43:09 +00:00
Now, let’ s mount it! We’ ll use a switch which will detect the filesystem used so
we know how to mount the partition.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
switch (lsblk -no "fstype" $chosen)
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 12:15:12 +00:00
2021-01-02 12:52:47 +00:00
We have two named case: =vfat= filesystems.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case "vfat"
sudo -A mount -t vfat $chosen $mp -o rw,umask=0000
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-13 14:18:43 +00:00
2021-01-02 12:52:47 +00:00
And =ntfs= filesystems.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case "ntfs"
sudo -A mount -t ntfs $chosen $mp -o rw,umask=0000
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Else, we’ ll let =mount= determine which filesystem is used by the partition
(generally =ext4= ).
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case '*'
sudo -A mount $chosen $mp
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We’ ll also run a =chown= on this newly mounted filesystem so the user can access
it without any issues.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
sudo -A chown -R $USER:(id -g $USER) $mp
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Let’ s close the switch block and send a notification the partition has been
mounted.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
end && notify-send -a "dmount" "💻 USB mounting" "$chosen mounted to $mp."
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And let’ s close the function.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-10-14 16:34:30 +00:00
2021-01-02 12:52:47 +00:00
**** Mount an Android device
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-mount-Mount_an_Android_device-5321f9cd
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
The function that manages to mount Android filesystems is =mountandroid= . Let’ s
declare it.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
function mountandroid -d "Mount an Android device"
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-12-28 21:10:10 +00:00
2021-01-02 12:52:47 +00:00
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
2019-10-23 12:15:12 +00:00
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
set bus (echo $chosen | sed 's/,.*/ /')
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Let’ s temporarily mount our device.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
jmtpfs -device=$chosen $mp
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
echo "OK" | \
rofi -dmenu -i -p "Press (Allow) on your phone screen, or set your USB settings to allow file transfert"
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Now, let’ s get the actual path of our Android filesystem we wish to mount, and
let’ s unmount the previous temporary filesystem.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set newchosen (jmtpfs -l | grep $bus | awk '{print $1 $2}' | sed 's/,$/ /')
sudo -A umount $mp
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Now we cam mount the new filesystem and send a notification if everything went
well.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
jmtpfs -device=$newchosen $mp && \
notify-send -a "dmount" "🤖 Android Mounting" "Android device mounted to $mp."
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And now, we can close our function.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
end
#+END_SRC
2019-10-23 12:15:12 +00:00
2021-01-02 12:52:47 +00:00
**** Mount a CD drive
2020-11-29 22:43:20 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-mount-Mount_a_CD_drive-27278199
2020-11-29 22:43:20 +00:00
:END:
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
function mountcd
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Now, let’ s chose the CD drive we want to mount using =rofi= .
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set chosen (echo $cddrives | rofi -dmenu -i -p "Which CD drive?")
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We’ ll also get the mountpoint thanks to the =getmount= function described
earlier.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
getmount
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And finally, let’ s mount it and send the notification everything went well.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
sudo -A mount $chosen $mp && \
notify-send -a "dmount" "💿 CD mounting" "$chosen mounted."
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Finally, let’ s close our function.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Ask what type of drive we want to mount
:PROPERTIES:
:CUSTOM_ID: Rofi-mount-Ask_what_type_of_drive_we_want_to_mount-0c15cffa
:END:
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
function asktype
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
We will use a switch statement which will use our anwser to rofi about what we
wish to mount.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
switch (cat $TMPDRIVES | rofi -dmenu -i -p "Mount which drive?")
2020-11-13 14:18:43 +00:00
#+END_SRC
2019-10-23 12:15:12 +00:00
2021-02-04 13:43:09 +00:00
If we chose the option "USB", we’ ll mount a hard drive, partition or USB drive.
In which case we’ ll call the =mountusb= function.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case "USB"
mountusb
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
If we chose the "Android" option, the =mountandroid= function is called.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case "Android"
mountandroid
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Else if we chose the "CD" option, we’ ll call the =mountcd= function.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case "CD"
mountcd
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
If nothing is selected, the function will naturally exit. Now, let’ s close our
switch statement and our function.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
end
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Launch the mounting functions
:PROPERTIES:
:CUSTOM_ID: Rofi-mount-Launch_the_mounting_functions-218ad001
:END:
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
switch (wc -l < $TMPDRIVES)
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case 0
notify-send "No USB drive or Android device or CD detected" -a "dmount"
2020-11-13 14:18:43 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
If we only have one line, we have only one type of mountable media. We’ ll pass
this line to a second switch statement.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case 1
switch (cat $TMPDRIVES)
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
This will allow the script to automatically detect what type of media it is, and
mount the corresponding function.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case "USB"
mountusb
case "Android"
mountandroid
case "CD"
mountCD
2021-01-02 12:52:47 +00:00
#+END_SRC
Let’ s close this nested switch case.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
end
#+END_SRC
2019-10-23 12:15:12 +00:00
2021-02-04 13:43:09 +00:00
If we have more than one line, we’ ll have to ask the user what type of media
they want to mount.
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case '*'
asktype
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-02-06 22:29:25 +00:00
2021-01-02 12:52:47 +00:00
Now, let’ s end our switch statement!
2020-11-13 14:18:43 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
end
2020-11-13 14:18:43 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Finally, we’ ll delete our temporary file.
2020-12-28 20:06:18 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
rm -f $TMPDRIVES
2020-12-28 20:06:18 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And with that, this is the end of our script!
2020-12-28 20:06:18 +00:00
2021-01-02 12:52:47 +00:00
*** Rofi-umount
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/rofi-umount
:CUSTOM_ID: Rofi-umount-ddde1667
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
~rofiumount~ is the counterpart of ~rofimount~ for unmounting our mounted
partitions.
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
**** Get the unmountable drives
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-umount-Get_the_unmountable_drives-89c71040
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
First, we will need to list all the drives that can be safely unmounted. Let’ s
run this.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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}')
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Now, let’ s get the android devices that are mounted.
#+BEGIN_SRC fish
set -g androids (awk '/jmtpfs/ {print $2}' /etc/mtab)
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And let’ s get the CD drives that are mounted.
#+BEGIN_SRC fish
set -g cds (awk '/sr0/ {print $2}' /etc/mtab)
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
We’ ll store all of our information in a temporary file, =/tmp/undrives= .
#+BEGIN_SRC fish
set -g undrivefile /tmp/undrives
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Let’ s make sure we begin with a clean, empty file.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
rm -f $undrivefile
touch $undrivefile
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Depending on if the related variables are set, write the different types of
mounted drives in the temporary file.
2020-11-29 22:43:37 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
test -n "$drives" && echo "USB" >> $undrivefile
test -n "$cds" && echo "CD" >> $undrivefile
test -n "$androids" && echo "Android" >> $undrivefile
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Unmount disk partitions
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-umount-Unmount_disk_partitions-0d425a47
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
The function =unmountusb= will take care of unmounting any drive we can safely
unmount. First, let’ s declare the function.
2020-11-29 22:43:37 +00:00
#+BEGIN_SRC fish
2021-01-02 12:52:47 +00:00
function unmountusb
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Let’ s chose the drive to unmount with rofi.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set chosen (echo $drives | \
rofi -dmenu -i -p "Unmount which drive?" | \
awk '{print $1}')
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-02-04 13:43:09 +00:00
Let’ s verify if the user actually selected any drive. If no, let’ s abort the
script.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
test -z "$chosen" && exit 0
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Now, let’ s unmount the chosen drive and send a notification if it has been done.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
sudo -A umount $chosen && \
notify-send "💻 USB unmounting" "$chosen unmounted." -a "dumount"
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Now, let’ s close the function.
#+BEGIN_SRC fish
end
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
**** Unmount Android device
:PROPERTIES:
:CUSTOM_ID: Rofi-umount-Unmount_Android_device-ae1d5904
:END:
2021-02-04 13:43:09 +00:00
The function =unmountandroid= will take care of unmounting any mounted Android
device. First, let’ s declare our function.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
function unmountandroid
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Let the user choose which Android device to unmount.
#+BEGIN_SRC fish
set chosen (echo $androids | rofi -dmenu -i -p "Unmount which device?")
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
We’ ll verify the user chose any device.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
test -z "$chosen" && exit 0
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-02-04 13:43:09 +00:00
If a device has been chosen, let’ s unmount it and send a notification it has
been successfuly unmounted.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
sudo -A umount -l $chosen && \
notify-send "🤖 Android unmounting" "$chosen unmounted." -a "dumount"
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Finally, let’ s close the function.
#+BEGIN_SRC fish
end
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
**** Unmount CD drive
:PROPERTIES:
:CUSTOM_ID: Rofi-umount-Unmount_CD_drive-369a2f61
:END:
2021-02-04 13:43:09 +00:00
=unmountcd= will take care of unmounting any mounted CD drive. Let’ s declare
this function.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
function unmountcd
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
As before, let the user chose which CD drive to unmount.
#+BEGIN_SRC fish
set chosen (echo "$cds" | rofi -dmenu -i -p "Unmount which CD?")
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
We’ ll verify the user chose any device.
2020-11-29 22:43:37 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
test -z "$chosen" && exit 0
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
If a drive has been chosen, let’ s unmount it and send a notification it has been
successfuly unmounted.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
sudo -A umount -l $chosen && \
notify-send "💿 CD unmounting" "$chosen unmounted." -a "dumount"
2021-01-02 12:52:47 +00:00
#+END_SRC
Now, let’ s close the function.
#+BEGIN_SRC fish
end
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
**** Ask what type of drive to unmount
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Rofi-umount-Ask_what_type_of_drive_to_unmount-6287af48
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
function asktype
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-02-04 13:43:09 +00:00
Let’ s create a switch statement to which will be passed the selection of the
user from rofi.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
switch (cat $undrivefile | rofi -dmenu -i -p "Unmount which type of device?")
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-02-04 13:43:09 +00:00
Three types of values can be returned: "USB", "CD", or "Android". These values
will be used to launch their corresponding function.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case 'USB'
unmountusb
case 'CD'
unmountcd
case 'Android'
unmountandroid
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Let’ s close the switch statement.
#+BEGIN_SRC fish
end
#+END_SRC
Let’ s now close the function.
#+BEGIN_SRC fish
end
#+END_SRC
**** Launch the unmounting functions
:PROPERTIES:
:CUSTOM_ID: Rofi-umount-Launch_the_unmounting_functions-7c48a928
:END:
2021-02-04 13:43:09 +00:00
Now back to the body of our script, let’ s input in a switch case the number of
lines contained in our temporary file.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
switch (wc -l < $undrivefile)
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-02-04 13:43:09 +00:00
If the file containes no lines. i.e. it is empty, nothing is to be unmounted.
Let’ s inform the user of that.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case 0
notify-send "No USB drive or Android device or CD to unmount" -a "dumount"
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Else, if there is only one type of drive, we’ ll automatically let our script
choose based on the content of this sole line.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
case 1
switch (cat $undrivefile)
case 'USB'
unmountusb
case 'CD'
unmountcd
case 'Android'
unmountandroid
end
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
And if there are more types than one, let’ s ask the user.
#+BEGIN_SRC fish
case '*'
asktype
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Let’ s close our main switch statement.
#+BEGIN_SRC fish
2020-11-29 22:43:37 +00:00
end
#+END_SRC
2021-01-02 12:52:47 +00:00
And finally, let’ s delete our temporary file.
#+BEGIN_SRC fish
rm -f $undrivefile
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
** Rofi-pass
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/rofi-pass
:CUSTOM_ID: Rofi-pass-8335357f
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
=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.
2021-01-02 12:52:47 +00:00
2021-02-04 13:43:09 +00:00
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.
2020-11-29 22:43:37 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
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
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
Now, let’ s get the list of the passwords that exist in our =pass= repository.
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set passwords (find $HOME/.password-store -type f -name "*.gpg" | \
string replace -r ".*.password-store/" "" | \
string replace -r ".gpg" "" | sort)
2021-01-02 12:52:47 +00:00
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
Let the user choose which password they wish to select.
2020-11-29 22:43:37 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
set password (for elem in $passwords
echo $elem
end | rofi -dmenu -i -p "Select your password")
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Let’ s verify we actually selected a password and not just exited. If no password
was selected, let’ s simply exit the script.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish
2021-10-12 09:31:20 +00:00
if test -z $password
exit
end
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Depending on the arguments passed earlier, we might want some different
behavior.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC fish :noweb yes
2021-10-12 09:31:20 +00:00
if test $TYPE = "yes"
<<rofi-pass-type >>
else
<<rofi-pass-copy >>
end
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
The default behavior is to copy the password to the clipboard for 45 seconds, so
let’ s do that.
2021-01-02 12:52:47 +00:00
#+NAME : rofi-pass-copy
#+BEGIN_SRC fish :noweb yes :tangle no
2021-10-12 09:31:20 +00:00
pass show -c $password 2> /dev/null
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
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.
2021-01-02 12:52:47 +00:00
#+NAME : rofi-pass-type
#+BEGIN_SRC fish :noweb yes :tangle no
2021-10-12 09:31:20 +00:00
set -l IFS
<<rofi-pass-type-get-password >>
printf %s $pass | xvkbd -file -
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
To correctly get the password from ~pass~ , we need to parse the output and only
get the first line, hence the following command.
2021-01-02 12:52:47 +00:00
#+NAME : rofi-pass-type-get-password
#+BEGIN_SRC fish :tangle no
set pass (pass show $password | string split -n \n)[1]
#+END_SRC
2020-11-29 22:43:37 +00:00
2021-01-02 12:52:47 +00:00
** rofi-ytdl
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env bash" :mkdirp yes :tangle ~/ .local/bin/rofi-ytdl
:CUSTOM_ID: rofi-ytdl-ff8f789d
2020-11-29 22:43:37 +00:00
:END:
2021-02-04 13:43:09 +00:00
This is just a simple wrapper around [[#ytdl-a-youtube-dl-wrapper-03bd63e0 ][ytdl ]] so I can easily download a video from
rofi, which we’ ll use first to retrieve the URL of the video we want to
download, be it from YouTube or other website supported by ~youtube-dl~ .
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC bash
2021-10-12 09:31:20 +00:00
URL=$(echo "Video to download:" | rofi -dmenu -i -p "Video to download:")
2021-01-02 12:52:47 +00:00
#+END_SRC
2021-02-04 13:43:09 +00:00
Now, if the variable ~URL~ is not empty (i.e. the user specified a link and did
not abort the operation), we’ ll proceed to teh download. Before it begins, we’ ll
send a notification saying the download is about to begin. When the ~ytdl~
process ends, we’ ll also send a notification notifying the user on the success
or failure of the download.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC bash
2021-10-12 09:31:20 +00:00
if [ -n "$URL" ]; then
notify-send -u normal "YTDL" "Starting downloading\n$URL"
2022-02-04 16:02:30 +00:00
if [[ $(ytdl "$URL") ]]
then
notify-send -u normal "YTDL" "Finished downloading!"
else
notify-send -u critical "YTDL" "Failed downloading\n$URL"
fi
2021-10-12 09:31:20 +00:00
fi
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
* Wallpaper utilities
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Wallpaper-utilities-5c05d470
2020-11-29 22:43:37 +00:00
:END:
2021-01-02 12:52:47 +00:00
** pape-update
:PROPERTIES:
2021-01-04 13:04:23 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :mkdirp yes :tangle ~/ .local/bin/pape-update
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: pape-update-bdecbadf
:END:
This little tool sets a random wallpaper using xwallpaper.
2021-01-04 13:04:23 +00:00
#+BEGIN_SRC sh
2021-10-12 09:31:20 +00:00
PAPESDIR=$HOME/Pictures/Wallpapers
2022-02-04 16:02:30 +00:00
PAPE=$(find "$PAPESDIR" -type f | sort -R | tail -1)
set-pape "$PAPE"
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
** Select wallpaper
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Wallpaper-utilities-Select-wallpaper-42f477a9
:HEADER-ARGS: :shebang "#!/usr/bin/env sh" :mkdirp yes :tangle ~/ .local/bin/select-pape
2020-11-29 22:43:37 +00:00
:END:
2022-02-24 16:13:23 +00:00
This script is based on what ~nsxiv~ can do as an image viewer as well as
2021-02-04 13:43:09 +00:00
xwallpaper.
2021-01-02 12:52:47 +00:00
#+BEGIN_SRC sh
2022-02-24 16:13:23 +00:00
PAPE=$(nsxiv -orbft ~/Pictures/Wallpapers/ *)
2022-02-04 16:02:30 +00:00
set-pape "$PAPE"
2021-01-04 14:00:40 +00:00
#+END_SRC
** Set a wallpaper
:PROPERTIES:
2022-02-04 16:02:30 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env bash" :mkdirp yes :tangle ~/ .local/bin/set-pape
2021-01-04 14:00:40 +00:00
:CUSTOM_ID: Wallpaper-utilities-Set-a-wallpaper-27eda9e6
:END:
2021-02-04 13:43:09 +00:00
This utility is not meant to be called by the user directly, but rather by
scripts that may be written by the user. Its role is simple: check if the
provided wallpaper exists and if it is an image. If both requirements are met,
the path to this image is then stored in ~$XDG_CACHE_HOME/wallpaper~ , or if this
variable is empty in ~$HOME/.cache/wallpaper~ .
2021-01-04 14:00:40 +00:00
#+BEGIN_SRC sh
2022-02-04 16:02:30 +00:00
CACHEFILE=$([ -n "$XDG_CACHE_HOME" ] && echo "$XDG_CACHE_HOME/wallpaper" || echo "$HOME/ .cache/wallpaper")
2021-10-12 09:31:20 +00:00
[[ -f $1 ]] && \
2022-02-04 16:02:30 +00:00
grep image <(file -b --mime-type "$1") && \
echo "$1" > "$CACHEFILE" \
&& xwallpaper --zoom "$1"
2020-11-29 22:43:37 +00:00
#+END_SRC
2021-01-02 12:52:47 +00:00
* Weather
2020-11-29 22:43:37 +00:00
:PROPERTIES:
2022-05-08 21:15:26 +00:00
:HEADER-ARGS: :shebang "#!/usr/bin/env fish" :mkdirp yes :tangle ~/ .local/bin/we
2021-01-02 12:52:47 +00:00
:CUSTOM_ID: Weather-4ed00bb0
2020-11-29 22:43:37 +00:00
:END:
2022-05-08 21:15:26 +00:00
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.
#+BEGIN_SRC fish
if count $argv > /dev/null
set -l SEARCH (string join '+' $argv)
curl http://v2.wttr.in/ ~$SEARCH
2021-10-12 09:31:20 +00:00
else
2022-05-08 21:15:26 +00:00
curl http://v2.wttr.in/Aubervilliers
end
2020-11-29 22:43:37 +00:00
#+END_SRC