jasonwryan.com

Miscellaneous ephemera…

Autotext Function

Once you have your environment set up; your window manager patched exactly the way you want, same for your editor and even your kernel builds automated, then you either start from scratch and learn a whole lot more, or you start to focus on the really small details. The endless polishing that is bred of a mania for automation and customisation and the liberating freedom of using software that allows, and even encourages, this approach.

Since I started using a couple of basic functions for managing my note taking, I have been conscious of the way I can use this tool to make my workflow a little less onerous.

One of the things I find myself doing a lot is reusing the same snippets of text; either prose in work documents, or links to relevant articles on the Arch Wiki and Forums. It is simple enough to add this material to my ~/.notes, but retrieval has always been—for the text I reuse frequently—unwieldy.

How many times do you really want to open the file, search for the relevant excerpt, highlight it and then copy it to the system clipboard before closing the file and pasting it into your email or a web form? I must have logged several thousand before I finally decided to do something about it.

I now have a couple of different files in ~/.notes/ depending upon the context; the example I’ll use is the one for the Arch Forums kept, naturally enough, at $HOME/Sync/notes/arch (I symlink to ~/.notes so that the directory is synched using BitTorrent Sync).

This is just a simple text file with all of the links, guidance and wisdom that I generously share with those people, mostly new to the community, who have yet to embrace the opportunity to commit the Forum Etiquette to memory. The format of each file is the same and is pretty basic:

# rules sticky
https://bbs.archlinux.org/viewtopic.php?id=130309

# smart questions
[url=http://www.catb.org/esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url]

# arch only
https://wiki.archlinux.org/index.php/Forum_Etiquette#Arch_Linux_Distribution_Support_ONLY

I use the commented title to identify the desired piece of text and then just copy it to the clipboard, ready to pasted into a post that will undoubtedly be gratefully received by the infractor:

$HOME/.bashrc
1
2
3
4
5
6
7
8
9
10
# board snippets
bbs() {
  file="$HOME/.notes/arch"
  if (( $# == 1 )); then
    local opt="$1"
    awk -v line="$opt" 'index($0, line) { getline; print }' "$file" | xsel -b
  else
    awk '/^# /' "$file"
  fi
}

So, on the rare occasion that I need to remind someone that on the Arch boards we only support Arch Linux (I know, quite the revelation…), I just open a scratchpad and enter bbs only and then Shift+Insert the text into the post and I am done. Not passing an argument just prints the commented titles in the file in the event that I forget what the damn thing is called.

I have a similar setup for work, with a couple of files that feature longer pieces of text that I find myself reusing for proposals, email responses and other administrivia. It’s a simple enough approach, but it works well and does lend a certain satisfaction to the otherwise tedious business of writing boilerplate.

Notes

Flickr Creative Commons image how many cans by shrapnel1

Scripting with udev

One of the most satisfying aspects of running free and open source software is the ability to be able to continually tinker with your setup, limited only by your imagination and ability. The more you do tinker, the smaller the gap between the former and the latter, as each small project inevitably leads you into a deeper understanding of various aspects of your system1 and how you can customize that system to suit your exact requirements.

Over the last couple of days, I have been playing with udev, the kernel device manager, as I was attempting to run a script once a specific USB drive was plugged in. It turns out, as is so often the case, that udev is only part of the picture…

As both my work and personal laptops have relatively small SSDs, I carry around my music on a 1TB external drive. As the drive only contains .flac files, I wanted to automate the process of rsync’ing music from my desktop to the drive and, for the laptops, repopulating the symlinks to ~/Music/ when the drive was plugged in.

My first thought was a rule in /etc/udev/rules.d/, using RUN+=. There are any number of blog posts espousing this approach and, as I quickly discovered, they are all wrong. The problem with using this key is that, as the man page makes clear, it is not designed for long running programs:

This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device.

Starting daemons or other long running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished.

The problem, as it manifest for me, was the drive would be blocked from mounting until after the script had run, meaning rsync or my symlinks would have no target. There are various “workarounds” on the web for this, including using two scripts, one to trigger the other2. Even for me, this seemed like a Pyrrhic victory.

The correct way to do this, as I found once I uncovered this thread on the Arch boards where WonderWoofy and 65kid helpfully pieced it together, is to use SYSTEMD_WANTS. As it is described in the manual:

THE UDEV DATABASE
The settings of device units may either be configured via unit files, or directly from the udev database (which is recommended). The following udev properties are understood by systemd:

SYSTEMD_WANTS=
Adds dependencies of type Wants from this unit to all listed units. This may be used to activate arbitrary units, when a specific device becomes available. Note that this and the other tags are not taken into account unless the device is tagged with the "systemd" string in the udev database, because otherwise the device is not exposed as systemd unit.

So, I edited /etc/udev/rules.d/90-usb-music.rules to remove the RUN+= key, using a systemd service file instead, like so3:

1
SUBSYSTEMS=="usb", ATTRS{idProduct}=="1905", ACTION=="add", ENV{SYSTEMD_WANTS}=="upmusic.service"

And then wrote the corresponding service file to have systemd hand off to the bash script:

/usr/lib/systemd/system/upmusic.service
1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Update music links from Apollo
Requires=media-Apollo.mount
After=media-Apollo.mount

[Service]
ExecStart=/home/jason/Scripts/upmusic

[Install]
WantedBy=media-Apollo.mount

As I mentioned in my post on my simple unmounting script, I use a naming convention for all my USB drives. In this case, my music is stored on Apollo, and it is auto-mounted with udiskie. In the service file, systemd uses a hyphen instead of a forward slash, so the correct designation is media-Apollo.mount.

Then it is just a matter of enabling the service with systemctl enable upmusic and, whenever Apollo is plugged in to either my desktop of laptop, the appropriate script will run and either update the files on Apollo or the symlinks on one of the laptops.

Notes

  1. This shouldn’t be taken as any sort of claim of expertise or deep understanding of this, or any other, part of my system. See below.
  2. Which is why you should never trust anything written by bloggers…
  3. The definitive reference for udev rules remains Writing udev rules

Creative Commons image on Flickr by Jacob Garcia.

Ad Blocking with Hostsblock

The relentless commercialization of traditional holidays, not just the December variety but all of them now, means that what was ostensibly an occasion for celebrating your particular flavour of $deity, pagan ritual or just an opportunity to reconnect with your wider family, has been co-opted to the worship of the most pernicious of all cults, consumerism.

Concomitant with this is the increasing corporatisation of the Internet; the deliberate and seemingly ineluctable effort by a relatively small number of global interests to turn most of the Internet into something more like cable television. As someone who has not lived with a television for decades precisely because I don’t want to live in a Skinner box that is solely designed to condition me to compliantly purchase more product, I find this rankly offensive (in the sense of morally repugnant as well as a coordinated and remorseless assault).

With the escalation of both the level and nauseating intensity of advertising around the “holiday season” and the almost hysterical exhortations to purchase more happiness, I decided that the one thing that I would be really thankful for this Christmas was better ad blocking.

I had been using Privoxy and an AUR script, blocklist-to-privoxy but I was still experiencing a lot of ads sneaking through (particularly local ones) and some unintended side effects of using Privoxy for this job, so I decide to give Jake VanderKolk’s Hostsblock a shot.

I went with the basic, entry level setup: kwakd for serving blank HTML in place of ads and hostsblock to write to my /etc/hosts. I didn’t see the need for dnsmasq and, after a week or so or using it haven’t seen the need to revisit that decision.

Suffice to say, it is working brilliantly. Where once web pages were festooned with garish advertisements promising me ripped abdominals, tropical holidays and the lasting serenity that only Apple products can really truly deliver, I now have glorious whitespace

There are a couple of other factors to consider with Hostsblock. There is a very simple command line interface for managing black and white listing of websites, and the various “content distribution networks” that infest most commercial sites like fleas. You can easily allow advertisements on or from sites and organizations that you want to support, while forever muting the inane drivel from the likes of Failbook et al.

Installation and setup are very straightforward, with simple instructions on the Hostsblock site. There is also an active thread on the Arch boards where Jake and a couple of others are extremely helpful.

If you are feeling listless, run-down and lacking in energy, why not try Hostsblock? It will make your web pages brighter, speed up your page loads, protect your privacy and make you insanely popular. Try Hostsblock today!

Notes

ad free, a Creative Commons image by Louisa Billeter on Flickr.