ZSH completions for teamocil/itermocil

I’ve noticed that even with installed OhMyZSH I don’t get proper completions for an itermocil in Z-shell (ZSH) terminal – proper completions being list of names of files from ~/.teamocil folder, sans extensions.

I’ve looked at OhMyZSH plugins and wrote my own for itermocil – a file ~/.oh-my-zsh/plugins/itermocil/itermocil.plugin.zsh with this content:

_itermocil () {
  compadd -- `ls -1 ~/.teamocil/ | rev | cut -d "." -f 2- | rev`
}

compdef _itermocil itermocil

And added it to plugins environment variable in .zshrc.

However, while doing so I’ve noticed that my .zshrc already contained this line:

compctl -g '~/.teamocil/*(:t:r)' teamocil

Googling a bit revealed that (:t:r) modifier to * wildcard are the basename sans extension modifiers for wildcard expansion in ZSH.

Which meant all I really needed was the same line in my .zshrc again, only this time with itermocil at the end instead of teamocil.

Advertisements

Move MP3s into folders by “Artist – Album – Year” in OS X

Quasi one-liner:

for filename in *.mp3; do; ART=$(mdls -name kMDItemAuthors -raw $filename | tr -d '\n"()' | awk '{$1=$1};1'); ALB=$(mdls -raw -name kMDItemAlbum $filename); YR=$(mdls -raw -name kMDItemRecordingYear $filename); echo $ART - $ALB - $YR - $filename; mkdir -p "$ART - $ALB - $YR"; mv $filename "./$ART - $ALB - $YR/"; done

Same, but on multiple lines:

for filename in *.mp3
do
  ART=$(mdls -name kMDItemAuthors -raw $filename | tr -d '\n"()' | awk '{$1=$1};1')
  ALB=$(mdls -raw -name kMDItemAlbum $filename)
  YR=$(mdls -raw -name kMDItemRecordingYear $filename)
  echo $ART - $ALB - $YR - $filename
  mkdir -p "$ART - $ALB - $YR"
  mv $filename "./$ART - $ALB - $YR/"
done

GPhoto2 Server – web interface for GPhoto2

There is an awesome UNIX/Linux utility called GPhoto2 (www.gphoto.org) that allows one to control DSLRs via USB. The amount of control is quite significant – one can make shots, download files and/or previews of photos, control aperture, exposure, ISO and many other settings that DSLRs expose (and even control focus servo, though with a bit of a hassle there).

The utility calls for a web interface – ever since I saw it I though how good would it be to control DSLR via WiFi from something like a Raspberry Pi (with battery) running GPhoto2. So I’ve started working a web interface for GPhoto2 – GPhoto2Server ( github.com/mvmn/gp2srv ), written in Java.

And now I’ve got first alpha version working. So I made a little video of it.

In order to get GPhoto2 on Raspberry Pi I’ve used this awesome project: github.com/gonzalo/gphoto2-updater.

And in order to make Raspberry Pi create own WiFi network I’ve used tutorial+distribs of hostapd from Adafruit: learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/overview.

BASH script and regexp of the day

Here’s my BASH script and regexp of the day.
I’m not particularly proficient in BASH scripting (in fact, not that proficient at all), so sorry if there are some obvious improvements to be made. But I suppose you can find the script useful anyway.

The purpose of the script is to get  values of a column of a CSV file by column name. The CSV file is comma-separated (not Excel style semicolon separated FYI) and may contain quoted values with commas, and double-quote escaped quotes (i.e “this is a value”,”this is another value with “” (quote) symbol”).

Parameters are: <file name> <column name>. Additional parameters -n or -u can be used to either output line numbers or output only unique values. Note: you can’t use both at the same time, -u takes precedence.

Also the script will output you the number of the column.

The script was written/tested against GNU grep (version 2.12 in particular). It doesn’t work correct for column #1 on OS X due to weird way BSD grep treats condition “^x{1}”. Installing GNU grep for OS X from sources like Rudix or Homebrew is recommended for Mac users.

You’re welcome (-:

unique=
addp=

for p in "$@"
do
     if [ "$p" == "-u" ]
     then
          unique="true"
     fi
     if [ "$p" == "-n" ]
     then
          addp=-n
     fi
done;

colMatch=$(head -n 1 $1 | grep -E "(,|^).*$2[^,]*" -o)

if [ "$colMatch" == "" ]
then 
	echo "Column not found."
else
	colNumber=$(head -n 1 $1 | grep -E "(,|^).*$2[^,]*" -o | grep "," -o | wc -l | grep -E "[0-9]+" -o)
	echo "Column number: $colNumber. Column name: $(head -n 1 $1 | grep -o -E "^((\"[^\"]*\")*,|[^,\"]*,){$colNumber}((\"[^\"]*\")*|[^,\"]*)" | grep -o -E '(^|,)((\"[^\"]*\")*|[^,\"]*)$')"

	if [ "$unique" == "true" ]
	then
		tail -n "+2" $1 | grep -o -E "^((\"[^\"]*\")*,|[^,\"]*,){$colNumber}((\"[^\"]*\")*|[^,\"]*)" | grep -o -E '(^|,)((\"[^\"]*\")*|[^,\"]*)$' | sort | uniq
	else
		tail -n "+2" $1 | grep -o -E "^((\"[^\"]*\")*,|[^,\"]*,){$colNumber}((\"[^\"]*\")*|[^,\"]*)" | grep $addp -o -E '(^|,)((\"[^\"]*\")*|[^,\"]*)$'
	fi
fi

Example of usage with results (tested on Ubuntu linux):
Continue reading

Reinventing the wheel – custom Dates API

I’ve started on the re-implementation of command-line java calendar tool in order to provide support for relative dates and other features, and while working on it I got a bit carried away with implementation of what turned out to be no less that custom dates API+implementation.

It’s pretty basic, and was done more as an exercise than for any particular need, but still I believe some interesting concepts can be looked up from it.

Though not yet complete, the code of JCCal was already committed to GitHub: https://github.com/mvmn/jccal . The dates API+impl classes can be looked up there already (in package x.mvmn.util.dates). Please also check the README there for general explanations.

I won’t be doing much explanation here, since it would mostly be copy-pasting of the readme for general things, and copypasting of actual code for the details. As someone said, “The truth is in the code” – so I encourage anyone who is interested to actually look up the code.