Saturday, September 12, 2015

The Eh (Canadian) programming language

I know a guy who is Canadian and, just for fun, I wrote a programming language for him.

The language is the Eh programming language.   The syntax is similar to JavaScript, except that instead of a line ending with a semicolon, it ends with a comma, a space, the word "eh" and a question mark (?).  For example:
var src = 'script', eh?
var s = '', eh?
s += 'Hello', eh?
s += ' ', eh?
s += 'World', eh?
s += ' from ' + src, eh?
console.log(s), eh?
This will print the following to the JavaScript console:
Hello World from script
An Eh script can be included inline using the "text/eh" type:

Or loaded from an external script file:

Here's the source code for the Eh compiler (in JavaScript) named eh.js:
// eh compiler
window.onload = function() {
  // execute eh code
  function executeEh(src) {
    eval(src.replace(/,\seh\?/g, ';'));
  }
  // load eh scripts
  var scripts = document.getElementsByTagName('script');
  for (var s=0; s < scripts.length; s++) {
    if (scripts[s].type === 'text/eh') {
      if (scripts[s].src) {
        // execute external eh scripts
        var extScript = new XMLHttpRequest();
        extScript.open("GET", scripts[s].src, true);
        extScript.send();
        extScript.onreadystatechange = function() {
          if ((extScript.readyState== 4)
              && (extScript.status == 200)) {
            executeEh(extScript.responseText);
          }
        };
      } else {
        // execute inline eh scripts
        executeEh(scripts[s].innerHTML);
      }
    }
  }
}
Eh supports all major JavaScript libraries and APIs.

Here's a Plunker to demonstrate: http://plnkr.co/edit/ASrEWq3uoZgi6TwolGLd

If you make an Eh script, comment below and let me know!

Tuesday, September 1, 2015

Put your Git branch in your Mac or Linux prompt

There is a lot of examples out there but here's a basic way to show your Git branch in your profile.

First, edit your .bash_profile.
$ vi .bash_profile
Press "i" to put vi in "insert mode".  Then, add the following to your .bash_profile:
ps1_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\w\$(ps1_git_branch)\$ "
After making this change and starting a new Terminal, the prompt will show the following in non-Git folders:
~/Documents $
In Git folders, the prompt will show the following (if you are on the "master" branch):
~/Documents/git/code (master)$
Very convenient.