Bash History Tip

In order to redeem myself for neglecting my blog, here’s a quick productivity tip for Bash. I’ve been using this for the past few months, and every time I log on to a server that doesn’t have this, I wonder how I ever functioned on the command line.

It’s 6 simple lines in ~/.inputrc:

# change up/down arrows to be search
  "\e[A": history-search-backward
  "\e[B": history-search-forward
  "\e[C": forward-char
  "\e[D": backward-char
  set show-all-if-ambiguous on
  set completion-ignore-case on

All it does is change the arrow keys to become command history search. So now, if I type se and hit up, instead of getting the previous command I had run, I get service httpd restart. Or svn c which gets me svn commit -F ../whatidid.txt.

This is especially helpful for toggling back and forth between commands. Usually when toggling between two or three commands, they inevitably get out of sync – hitting the up arrow twice brings you to the wrong command, and you have to consciously look for it. Now, the first character of each command guarantees you the right one.

Could I use aliases for all of these? I most certainly could. But with this simple .inputrc change, I don't even think about aliases anymore. Like all .inputrc directives, it also works for the MySQL command line client and other readline-derived prompts.

If you use the command line like I do, this will likely save what feels like hours of your life.

 

PHP Formatter

The best part about the holidays is the free time to write fun code. I’ve wanted to write a PHP formatter since I first used Go’s gofmt, so this week it was a pretty easy choice.

The code is available on GitHub: PHP Formatter, and licensed under the MIT License like most of my code.

It uses PHP’s built-in token_get_all function to tokenize the PHP into operators, keywords, strings, HTML, and whitespace, and then steps through these tokens, focusing mostly on rearranging the whitespace. All whitespace (outside of strings and HTML, of course) is stripped except for multiple sequential newlines which are reduced to a single newline. After that, “correct” whitespace is added back into the code.

In order to avoid creating an unruly mess, it then goes through and ensures that no line is longer than 97 characters unless absolutely necessary (i.e. it doesn’t automatically break strings up yet). For consistency sake, it also re-arranges sequential single-line comments to the same line length.

Additionally, PHP Formatter will format inline HTML for indenting, although the HTML formatting is much more permissive, with whitespace for the most part being left untouched. The indenting rule here is that each line that has an opening tag is indented once (regardless of how many tags are opened on that line), and likewise each line with a closing tag is unindented. For this to work, closing tags are rearranged to remain on the same line as the other corresponding open tags were originally. If a closing tag occurs out of order, unclosed tags will be closed to help promote valid markup.

As an added perk, inline JavaScript (through script tags) will be formatted using the JS-Beautify PHP port if it’s available alongside the PHP Formatter. I forked this in order to make it ignore PHP tags inside the JS as though they were just comments, since it’s not actually valid JavaScript, although that’s only necessary if you mix PHP and JS.

All in all, this actually took me a fair bit longer than I thought it would – I was expecting to get a lot more done today and yesterday outside of this – as it turns out that nearly everything about the way we format code is an edge-case, with the word “exception” (or worse “the one exception”) appearing all too often in the comments. Nonetheless, save for a few quirks, it effectively formats code the way I’ve been trying to, and does so with minimal effort on the developer’s part.

 

Why I’m a Programmer

I’ve been thinking a lot lately about why and how I chose to be a programmer. In doing so, I discovered an interesting fact: as of next week, I will have been writing software for more than half of my life. That actually makes figuring out the original decision process somewhat difficult, because honestly I don’t have too many memories that predate being a programmer.

Perhaps this sheer volume of time illustrates rather poignantly how great programming is. When I first started programming, I was 10 (let’s not count writing HTML and copying and pasting Perl scripts around from a few years prior to that, since that only barely counts as programming). Within several months, I had released my first open source project with some 20,000 lines of code for the world to see.

Read More…

Google Doodle Basketball – Automatic 45 Points

Enamored by Google Doodle Basketball, but unable to get more than 42 points, I was intrigued at the upper limit of the game’s score. I knew for sure you could get away another shot past 42 once the balls move off the screen, but I was never able to get there.

The easiest solution was to write 40 lines of JavaScript to win for me. I spent some time playing around with the shot times, and was unable to get more than 45. But at least now I have a script that gets 45 points automatically every single time:

Read More…

Unit Testing in C++

I was chatting with an old friend this morning who was expressing frustration about the fact that C++ lacks a well-documented and easy to use unit testing framework. All he wanted was a way to write annotated tests to confirm that some parsing routines of his did indeed parse the correct values out of various known edge cases as he continued to expand on the parser. He said he had spent an hour or two searching and trying various libraries, but they were all overkill for what he was trying to do.

I suggested that perhaps the reason that there aren’t any good micro unit testing libraries available for C++ is that you could write your own faster than you could find one, and so everybody just did that; actually, my words were closer to “you could probably do it in a half an hour”. He wasn’t convinced, so I went ahead and did it.

Read More…

Living in Canada? Check Out Well.ca

I recently discovered Guelph-based Well.ca and want to share the word: Their service rocks.

I placed an order for shampoo and some other stuff on Saturday, about $15 with free shipping. They processed it and sent me a tracking number for it Monday evening, and the parcel was delivered in my mailbox at 8:35am Tuesday morning, all neatly packed and wrapped up in recyclable paper and boxing.

Read More…

HTML5 Helicopter

My girlfriend, Michelle, is spending this summer learning to code with the intention of creating a startup. She finished her last exam of the year on Thursday (analytical chemistry) and has already started pursuing programming.

Prior to this weekend, Michelle had dabbled in PHP and HTML/CSS here and there, and had a one-semester class in Matlab for her Chemistry program. So when she said she wanted to learn Javascript this weekend, I thought that writing an old-school helicopter game using Canvas in HTML5 and getting it live by the end of the weekend would be a reasonable goal.

Always looking to impress – and quite frankly, from my perspective learning Javascript is easier, or at least way more fun, than writing three chemistry exams in four days – here it is: HTML5 Helicopter.

Read More…

Introducing SDFRY, The Modern Programming Language

Backstory

Programming languages have stagnated. 1958 was a great year for language development, as was 1995, depending on who you ask. The only problem is 17 years have passed since then and we’re still trying to shoe-horn software development principles into languages not built for them.

It seems that we’re primed for a revolution. Not one rooted in languages picking up steam despite being older than the average Valley CTO, but new languages that make us really think.

Thus, I introduce SDFRY (pronounced “super deep fry”) that strives to optimize writing software for humans, not for computers, named after its fundamental rule: Seriously, Don’t Freaking Repeat Yourself. Designed without buzz-words like “dynamically typed” or “purely functional”, SDFRY fills a gaping void by being a pragmatic every-day language built for developers.

Behold the power of a truly expressive language that eliminates the dry aspects of programming.

Demo

Play Connect Four | View Code

Read More…

1 2  Scroll to top