Tuesday, April 1, 2014

Watch

So this is pretty cool.


The watch command (available linux and on Mac via brew) will run a program every n seconds and display the results in a terminal.

For example, the following common will show the display above with human readable disk free space on your system. But since the command can be anything you want this is a pretty powerful tool.

watch -n 3 df -h

Wednesday, March 12, 2014

Checkout out your DB tables

I was doing some DB work today and wanted to be able to sort all DB tables based on the date they were created. Turns out you can do some neat stuff by looking in the information_schema.tables. For example this shows all the InnoDB tables.


select `table_schema`, `table_name`, `create_time`  FROM information_schema.tables where engine = 'InnoDB' order by create_time desc  ;

Wednesday, February 6, 2013

Configuring emacs to send iCloud mail on Mac OS X

Pic from ajc1 on Flikr
It's handy to be able to send emails from emacs, and this guide will show how to set up SMTP via an iCloud email account.

Step 1. Install gnutls

iCloud requires you to send emails over secure channel, and emacs supports sending email with starttls or gnutls. gnutls is available through brew

To install it is easy:

brew install gnutls

Wait a few minutes while your Mac gets hot downloading and compiling!

Step 2. Create an authinfo file

emacs can look in a file ~/.authinfo to find your login credentials, so create that file and fill in the blanks.

touch ~/.authinfo
chmod 600 ~/.authinfo

The contents of the file should read:

machine smtp.mail.me.com port 587 login YOURNAME@icloud.com password YOURPASSWORD
Step 3. Configure emacs

Add the following to your .emacs file:


(setq
 send-mail-function 'smtpmail-send-it
 message-send-mail-function 'smtpmail-send-it
 user-mail-address "YOURNAME@icloud.com"
 user-full-name "YOUR FULLNAME"
 smtpmail-starttls-credentials '(("smtp.mail.me.com" 587 nil nil))
 smtpmail-auth-credentials  (expand-file-name "~/.authinfo")
 smtpmail-default-smtp-server "smtp.mail.me.com"
 smtpmail-smtp-server "smtp.mail.me.com"
 smtpmail-smtp-service 587
 smtpmail-debug-info t
 starttls-extra-arguments nil
 starttls-gnutls-program (executable-find "gnutls-cli")
 smtpmail-warn-about-unknown-extensions t
 starttls-use-gnutls t)

Note that your gnutls program may be in a different spot. Find it with:

mdfind -name gnutls-cli 
Step 4. Testing

To compose an email C-x m

Enter an email and hit C-c c to send it.

If it works, great! If not switch to the *Messages* buffer for hints on what may have gone wrong.

Step 5. Sending emails from elisp code



(message-mail recipient subject)
(message-send-and-exit)))))


Sunday, April 29, 2012

find grep on Mac OS X

On linux machines I search files using find, egrep and xargs as follows:

  find . -name "*.cpp" | xargs -i egrep -iHn "some search string" {}

this outputs any matches with the filename and number and also disables case dependency.

On my Mac it doesn't work. I tried reverting to egrep -r (to search recursively) instead, but that doesn't work. It just fails silently too. I tried installing findutils with brew to see if that helped, as often gnu tools are more up to date in brew than in the Apple version, but that didn't help.

So after some fiddling I found that the syntax below works:


  find . -name "*.cpp" | xargs egrep -iHn "some search string"

Only subtly different!

Actually, hold up, this does not work for filenames that have spaces in them. :(

Try this instead:

find . -type f -print0 | xargs -0 egrep -iHn "some search string"

J.

Thursday, March 22, 2012

Starting a program to run in the background from a DOS prompt


Picture by ro_buk on Flickr


From a bash shell you can run a command in the background by adding an "&" to the end of the command, but how do you do the same thing in Windows?

Using the START command lets you run a task in the background (maximised or minimised), or in the foreground.

For example the following would run memcached in the background minimized.

C:\>start /min D:\platform\memcached.exe

Microsoft's documentation is below, but it seems the options have changed. /m does not work but /min does.

Microsoft's documentation on START

Monday, December 26, 2011

Making a emacs lisp expression expand itself to XML

My response to the blog article An Emacs Programming Challenge

The goal is to make the emacs lisp record below execute itself and produce XML. My lisp has gotten rusty so it took me a couple of hours to get this working, but I think I got there...

You can see it properly formatted and coloured here

It's fairly straightforward. The only complexity is the use of lexical-let. emacs lisp is dynamically scoped, so in the function I create for each keyword, where it prints the symbol name is a free variable at runtime. So if sym-name is not defined you'll get an error, otherwise you'll get whatever it is defined as instead of the value you wanted.

By using lexical-let any variables are bound lexically, that is stored in an environment that stays with the function we create when it is executed (a closure).
(require 'cl) ;; uses lexical-let

(defun make-xml-izer(name)
"Given a symbol NAME this makes a function that outputs an xml string for that
symbol and using fset binds it to the same symbol so it becomes executable.
This pollutes the global function namespace so be careful with which names you
pass in"
(lexical-let ((sym-name name))
(fset (intern name)
(lambda(&rest input)
"returns a string representing the xml encoding of the input sexp"
(let ((res (format "<%s>" sym-name)))
(dolist (item input)
(if (listp item)
(setf res (concat res (eval item)))
(setf res (format "%s%s" res item))))
(setf res (format "%s" res sym-name))
res)))))

;; executing this makes all the symbols in the list below executable
(mapcar (lambda(s)
(make-xml-izer
(symbol-name s)))
'(record date millis sequence logger level class method thread emessage exception frame line))

;; the sample record
(record
(date "2005-02-21T18:57:39")
(millis 1109041059800)
(sequence 1)
(logger nil)
(level 'SEVERE)
(class "java.util.logging.LogManager$RootLogger")
(method 'log)
(thread 10)
(emessage "A very very bad thing has happened!")
(exception
(emessage "java.lang.Exception")
(frame
(class "logtest")
(method 'main)
(line 30))))

Saturday, July 30, 2011

eredis update

I've been busy on my emacs redis client eredis and it now supports the entire API. It still needs a bit more polish but it should be a workable Redis client now, and I will continue to play with the org-mode table integration which I think has a lot of potential uses: for example making a gui to edit server parameters in just a few seconds.

Also made a new video showing some of the new org table creating commands and the monitor mode that shows the Redis commands as they are run on the server: