Sorry, I should update the article with the better solution, it's just a few more lines:
https://github.com/bag-man/dotfiles/blob/master/bashrc#L116-...
https://github.com/bag-man/dotfiles/blob/master/bashrc#L116-...
https://gist.github.com/chew-z/db310b0e3184ae335e24d2ec7087a...