Tuesday, March 13, 2012

Searching the command line history in bash

With as much as I've been using bash lately, it's weird that it's taken me this long to stumble across a need for searching the command-line history. But it happened today, and it was easy to do, as soon as someone told me how to do it. I had a command I'd run a few times over a week ago, and in the intervening time I'd run thousands of other commands. I could figure out all the parameters to this command if I wanted to, but the Second Rule of Programming states that "Programmers are Lazy," so I was going to find a better way. And it's pretty simple:

Hit CTRL+R at the command prompt, then start typing what you remember of the command. When the one you want shows up, hit Enter to run it or Escape to have a chance to edit it. Simple as that.

I really enjoy using all the *nix operating systems and the power you get in the shell environment. However, the biggest drawback is that using the shell environment is not intuitive like a GUI can be (if designed correctly, of course). Chances are, you can do whatever it is you need to do, because someone else needed to do it first and went and made that possible. But you need to go and learn it yourself, because there's not a whole lot you can divine from a screen blank except for a cryptic '$> ' and a blinking cursor with zero prior knowledge. That's fine by me, as long as such knowledge is as readily available as possible when I need it. I'd much prefer that to a ridiculously complicated ribbon interface with a buttons for so many things that finding the button I need takes longer than asking Google to find it for me. I mean, if I'm Googling anyway, it's easier for me to remember a command name or a keystroke combo than a series of menus or ribbon tabs to drill through. I'd further argue that executing a known command in a shell or a keystroke combo is an O(1) operation, whereas executing a command from a menu or a ribbon interface is O(log(n)), with n being the number of available menu options or ribbon buttons. With few possible commands (like, say, MS Paint), it's not a big deal. With a ton (like, say, MS Word), it can be a nightmare if you need something fairly obscure.

Friday, March 2, 2012

Automated Testing with MonoTouch on an iPad: Revisited

Back in December, I wrote this post about my efforts to set up automated testing on an iPad in MonoTouch. I updated that a couple of weeks ago, noting that there was now a --launchdev option which might make all of that unnecessary, so long as your test app didn't need any data. Unfortunately, mine did. However, as of MonoTouch 5.2.5 (or potentially earlier, but 5.2.5 is when I noticed it), I saw a new command-line option appear for the mtouch program: --argument. It's description? "Launch the app with this command line argument. This must be specified multiple times for multiple arguments." Could this be a way I could ditch the separate launcher app and related AppleScript incantations completely? The short answer is yes. The long answer follows, after the break.

Saturday, February 18, 2012

C# Initializers

C# generic collections offer a lot of flexibility and power for organizing your program's data. However, if you need to set one up manually, you may have written code that looks something like this:

List<string> list1 = new List<string>();
list1.Add("list1 item1");
list2.Add("list1 item2");
list2.Add("list1 item3");

List<string> list2 = new List<string>();
list2.Add("list2 item1");
list2.Add("list2 item2");
list2.Add("list2 item3");

List<string> list3 = new List<string>();
list2.Add("list3 item1");
list2.Add("list3 item2");
list2.Add("list3 item3");

Dictionary<string, List<string>> dictionary = new Dictionary<int, List<string>>();
dictionary.Add("list1", list1);
dictionary.Add("list2", list2);
dictionary.Add("list3", list3);

Wouldn't it be nice if you could declare a dictionary in one statement? Or a list? Or an array? Or even an entire class, complete with all data members, which may themselves be other classes, lists, dictionaries, or arrays? Well, turns out that you can. Find out how after the break!

Friday, February 17, 2012

Customizing Appearance of Disabled UIBarButtonItems

Have you ever wanted to change the appearance of a disabled UIBarButtonItem? It seems to be a fairly common issue without many good solutions - at least, until iOS 5.0, anyway. Perhaps you've tried this one:

UIBarButtonItem button = 
    new UIBarButtonItem("Untitled 1", UIBarButtonItemStyle.Plain, null);
button.TintColor = UIColor.White;
button.Enabled = false;

It all works fine, up until you disabled the button, at which point your button becomes a very faded version of whatever you had in there. Normally, that's what you want to have happen, because dimming the button is a good indication to the user that the button cannot be pressed at the current time. However, there may be situations where that isn't true...

For example, I have a toolbar with a document title in the center. It's a UIBarButtonItem, so that it can detect touches, and respond by showing a UITextField that allows the document title to be edited. The document editor has two modes: an edit mode, and a run mode. In run mode, I don't want the title to be edited, so I disable the button. However, I still want it to be nice and visible, like a title should be. I also wanted to avoid the obvious workaround of swapping out my title button for another non-functional button with the same content. It would work, but it would kind of break some of the behind-the-scenes stuff we're doing with our user interaction model, and would require me to maintain the state of two separate buttons with the title. Also, a button that is enabled has a little sparkly animation when it is touched, to let you know that it is touchable and that the system recognized that you touched it. The two-button workaround means I would still have that animation, and gives the user the impression that they can still edit the title, but it's not working right.

So, what did I do? Find out after the break.

Sunday, February 12, 2012

Easy Permutation Calculation in Python

I was recently doing some Python programming, and had to compute all possible combinations of items from two separate lists. At first, I thought, I'll just do it this way:

list1 = ['a', 'b', 'c']
list2 = ['x', 'y', 'z']
for i in list1:
    for j in list2:
        print i, j
a x
a y
a z
b x
b y
b z
c x
c y
c z

However, my loop body was something that was a little more complicated than a print, and couldn't be done well inside of a loop like that. And besides - this is Python we're talking about. Surely there is a better way!

Well, there is - and don't call me Shirley. Details coming up after the break!

Sunday, January 29, 2012

Web Form Automation with Selenium WebDriver

Have you ever had a tedious task involving a web site form that you have wanted to automate, but couldn't? Or perhaps you wanted to automatically test a website you were making? Well, there's a fairly well-known tool for browser automation called Selenium. I'd heard of it before, and was looking into it when I had a project where I want to take some JavaScript based pages, run them through a JavaScript engine so they could generate the HTML I needed and then save the results. But until now, I had never had a chance to use it. It's powerful, and there's a lot you can do with it that I won't cover. But the basics will get you a long way.

More after the break.

Saturday, January 21, 2012

Composing Emails and Sharing Files in MonoTouch

Lately, I had to add the ability to import and export data from a MonoTouch application via email. Thanks to everything you get in the iOS SDK for free, this is actually relatively easy. But you do need to know all the places you need to touch to make this happen. You'll need to:

  • Modify your info.plist to specify the file types your application supports
  • Update your application delegate to override the HandleOpenUrl method
  • Update your view controller to show a mail composer
  • Add code to your view controller to import a file that is passed to it and display it

Many thanks to this this tutorial on email sharing to get me going on this.

Details after the break.

Sunday, January 15, 2012

Bash Basics

I've been doing a lot of bash scripting lately.  I haven't really done any bash scripting since, oh, 2007 or so, and nothing to complicated even then, so it's been an interesting learning / remembering experience for me.  Hopefully writing it down this time will make it easier next time.  Here's a preview of what you'll find here:

  • Defining and using variables
  • Variable and arithmetic expansions
  • Arrays
  • If statements
  • While loops
  • For loops
  • Input redirection, output redirection, and pipes
  • Regex tools: grep, sed
  • Command substitution
  • Useful commands