Tuesday, January 31, 2017

Google from the command line

It occurred to me that for Google searches where I don't need suggestions I can do a search by hitting a URL from the command line with this one-line shell script:

% cat gg
open https://www.google.com/search?q="$*"

Let's make one for Wikipedia, too:

% cat wk
open http://en.wikipedia.org/wiki/"$*"

Yup, I should generalize those to a script that looks at its name and maps that to an appropriate URL. Here it is:

% cat hitit
which=$(basename $0)
case $which in

open "$url"

Let's add a couple of symlinks in my sbin directory:

$ ln -s hitit gg
$ ln -s hitit wk

Now, instead of

  1. Switch to Chrome (I use cmd-, 3 with Keyboard Maestro)
  2. Type cmd-L to get to the address field
  3. Type the search string, or use a Chrome "search engine" like w to hit Wikipedia.
I can do this:
% gg astroboy
% wk ruby lang

The above use open(1) on OS X but on Cygwin, which I may be moving back to, I'd hit URLs from the command line with this script:

% cat ffx
"/c/Program Files/Mozilla Firefox/firefox.exe" $*

Sunday, January 29, 2017

uniq(1) -- not just for sorted input

For years I've mostly used uniq -c with sorted input but I've recently realized that uniq is great for vertical compression/counting.

Here I pipe clipboard contents into uniq -c to see how many blank lines appear between the application of changes 6881 and 6882:

 % cb | uniq -c
      1 applying 6881, event id = 148047427, INSERT_TEXT,6239,0,6240,0,[
      1 ]
      1 applying 6882, event id = 148047428, INSERT_LINES,6240,0,7479,0,[
      1 ]
      1 applying 6883, event id = 148047429, INSERT_TEXT,7479,0,7480,0,[
      1 ]
      1 2017-01-29 17:25:00,762 LimitedEditCodeServiceImpl WARN  - Exception applying deltas to program text
      1 java.lang.IndexOutOfBoundsException: Index: 7479, Size: 6244
      1         at java.util.ArrayList.rangeCheck(ArrayList.java:635)
      1         at java.util.ArrayList.get(ArrayList.java:411)
      1         at org.cloudcoder.app.shared.model.TextDocument.getLine(TextDocument.java:59)
      1         at org.cloudcoder.app.shared.model.ApplyChangeToTextDocument.apply(ApplyChangeToTextDocument.java:41)
      1         at edu.arizona.cs.practice.codeextraction.LimitedEditCodeServiceImpl.doLoadCurrentText(LimitedEditCodeServiceImpl.java:103)
      1         at edu.arizona.cs.practice.codeextraction.Test1.main(Test1.java:112)

Below I use uniq -c to collapse repeated lines in query output:

% cb | uniq -c
      1 +----------+---------------------+-----------------+------+
      1 | username | date                | testname        | type |
      1 +----------+---------------------+-----------------+------+
      1 | zjones   | 2017-01-19 09:22:56 | number2letter   |    2 |
      1 | zjones   | 2017-01-19 09:22:56 | number2letter   |    0 |
      2 | zjones   | 2017-01-19 09:23:16 | number2letter   |    0 |
      1 | zjones   | 2017-01-19 09:25:06 | number2letter   |    0 |
      2 | zjones   | 2017-01-19 09:25:08 | number2letter   |    0 |
      1 | zjones   | 2017-01-19 09:25:09 | number2letter   |    0 |
      2 | zjones   | 2017-01-19 09:25:10 | number2letter   |    0 |
      3 | zjones   | 2017-01-19 09:25:11 | number2letter   |    0 |
      3 | zjones   | 2017-01-19 09:25:12 | number2letter   |    0 |
      2 | zjones   | 2017-01-19 09:25:18 | number2letter   |    0 |
      1 | zjones   | 2017-01-19 09:25:31 | number2letter   |    0 |

Friday, January 28, 2011

Please consider the environment before printing this email!

Today's irony: Some people have an email signature that includes an image of a recycling symbol and the line "Please consider the environment before printing this email! Thank you" In a case at hand, that image added 3,297 bytes to the message, which had 118 bytes of true content.

I wonder what the worldwide daily total carbon footprint of all those little recycling images is.

Wednesday, October 8, 2008

Old book clarifies Cairngorm

I have to admit that while I'd read a few things about Cairngorm, it still seemed murky to me. The fog cleared when I read chapter 20 in Developing Rich Clients with Macromedia Flex. It's interesting that although the book predates Cairngorm, its description of techniques that Cairngorm later embodied made things click for me.

This book has cleared some other Flex fogbanks for me, too. I think it's truly a gem that's slipping into the sands of time.

Caveat emptor: the book is based on Flex 1.0/1.5, with ActionScript 2.0 -- you'll need do some translation as you read it, and some things simply no longer apply. But I think it's still well worth the money.

Getting Started with Adobe Flex

I hope to make it more presentable when time permits but FWIW, here are some rough notes on some resources to help people get started with Adobe Flex.

Saturday, October 4, 2008

Chick Magnet

While waiting in line at Subway yesterday, an attractive woman noticed the book I was reading and struck up a conversation with me. The book? "Official Rules of the NFL"

I've never gotten that sort of attention with "Java Persistence with Hibernate" or "JUnit in Action", to name two of many.

Among many other things I learned that the ball must a Wilson. Period.

Sunday, March 30, 2008

Fun with Cygwin's /dev/clipboard

I'd say that /dev/clipboard is surely among one of the least known handy things on Cygwin. The idea is simple -- the Windows clipboard is accessible as a file, /dev/clipboard.

I often use /dev/clipboard to bridge the gap between GUI tools and line-oriented tools. For example, if you're wondering how many words are in a section of text in a document you're looking at, you can put that text on the clipboard and then do this:
wc -w /dev/clipboard
If you want to insert the output of ls into a document you're editing, send it to the clipboard,
ls *.java > /dev/clipboard
and then paste it in. (Yes, this task is simpler with Emacs, vi, et al.!)

You can save some typing by symlinking /cb to /dev/clipboard:
ln -s /dev/clipboard /cb
Some non-Cygwin programs don't interact well with /dev/clipboard. For example, directing the output of javap to the clipboard leaves it unchanged:
% date > /cb
% javap Hello >/cb
% cat /cb
Sun Mar 30 15:23:24 USMST 2008
To compensate, I wrote a trivial script, tocb:
% cat tocb
cat >/dev/clipboard
Instead of redirecting output to /cb, I pipe into tocb:
% javap Hello | tocb
% cat /cb
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
I also wrote a trivial counterpart for tocb named fromcb:
% cat fromcb
cat /dev/clipboard
Incidentally, Mac OS X has similar commands named pbcopy and pbpaste.

I got to wondering if a single script, call it "cb", could act like fromcb when on the left end of a pipeline, and act like tocb on the right end of a pipeline. Like this:
% cal | cb # puts output of cal onto the clipboard
% cb | wc # puts the clipboard contents onto standard output
Cygwin supports the isatty(file_descriptor) library function, which queries whether the specified file descriptor is connected to a terminal. (Some programs, ls to name one, change their behavior depending on whether they're writing to a terminal.) Linux has isatty(1), which simply wraps isatty(3) in a program. My Cygwin installation doesn't have isatty(1), and I was too lazy to see if it's in some package I haven't installed, but it's trivial to write:
% cat isatty.c
main(int argc, char **argv)
With that in hand it's easy to write a cb that that reads or writes the clipboard depending on which end of a pipeline it's on. Here it is:
% cat cb
if isatty 0
cat /dev/clipboard
cat >/dev/clipboard
% cal | cb
% cb | cat -n
1 March 2008
2 Su Mo Tu We Th Fr Sa
3 1
4 2 3 4 5 6 7 8
5 9 10 11 12 13 14 15
6 16 17 18 19 20 21 22
7 23 24 25 26 27 28 29
8 30 31
You can put cb on both ends of a pipeline to transform the clipboard contents:
% date | cb
% cb | tr a-z A-Z | cb
% cb
SUN MAR 30 16:03:17 USMST 2008
Maybe there is nothing new under the sun -- I just now noticed that xsel (c. 2001) apparently uses isatty to determine whether it should fetch or set clipboard contents. :)