Two days ago @amussey posted up this repo to his fb: milkbikis/powerline-shell

The goal of the project appears to be trying to make powerline~esk prompt for most shells, Bash, Zsh, Fish.

Picture of powerline-shell

So that’s pretty cool, and something different. I’ve been looking for something new to do with my shell, why not see what I can do.

Right away it’s obvious that this prompt is built off segments that get run, and wouldn’t you know there was a ruby one. Unfortunately, it needed some love and care because it was lazily copied from the PHP segment, and it was relying on actually executing code within an interpreter to get us the version. Oh, and I’ve never written a lick of Python in my life.

Immediately jumping into this project I knew I had previously worked on a rbenv prompt, and that I had the shell code to do this: ruby -v | sed 's/ (.*//', and it appeared that I might be able to chain subprocess method calls to get us this data.

Using this API entry I had enough examples to create this:

import subprocess
def add_ruby_version_segment():
    try:
        p1 = subprocess.Popen(["ruby", "-v"], stdout=subprocess.PIPE)
        p2 = subprocess.Popen(["sed", "s/ (.*//"], stdin=p1.stdout, stdout=subprocess.PIPE)
        version = p2.communicate()[0].rstrip()
        powerline.append(version, 15, 1)
    except OSError:
        return

add_ruby_version_segment()

Git Pull Request

So not bad - This works for getting us the ruby version and patch into prompt. I’d use it for a couple hours and then realize, this thing is useless in an RVM environment because there is no gemset information in my prompt:

import subprocess
def add_ruby_version_segment():
    try:
        p1 = subprocess.Popen(["ruby", "-v"], stdout=subprocess.PIPE)
        p2 = subprocess.Popen(["sed", "s/ (.*//"], stdin=p1.stdout, stdout=subprocess.PIPE)
        version = p2.communicate()[0].rstrip()
        if os.environ.has_key("GEM_HOME"):
          gem = os.environ["GEM_HOME"].split("@")
          if len(gem) > 1:
            version += " " + gem[1]
        powerline.append(version, 15, 1)
    except OSError:
        return

add_ruby_version_segment()

Git Pull Request

And now we have the data we need for the prompt, sweet.

Final Product

However, after writing this, it seems obvious that I could improve this submission by refactoring out the sed command into text manipulation to save a couple clock cycles, and there might be a way to refactor the nested if statements too, not sure.