Skip to content

Instantly share code, notes, and snippets.

@happymcplaksin
Created September 15, 2011 15:09
Show Gist options
  • Save happymcplaksin/1219502 to your computer and use it in GitHub Desktop.
Save happymcplaksin/1219502 to your computer and use it in GitHub Desktop.
# vim: set sw=4 sts=4 et tw=0 :
# Local monkeypatching to string.
class String
# get/kill first word
def firstword
self.sub(/^\s*([\S]+).*/,'\1').strip
end
def rmfirstword
self.sub(/^\s*[\S]+\s*/,'')
end
def word2
self.rmfirstword.firstword
end
# get/kill last word
def lastword
self.sub(/.*\s+([\S]+)\s*$/,'\1')
end
def rmlastword
self.sub(/(.*)\s+[\S]+\s*$/,'\1')
end
def to_a
self.split(/\n/)
end
@@ansi = true
def self.ansi=(bool)
@@ansi = bool
end
def self.ansi
@@ansi
end
def colorize(text, color_code)
if @@ansi
"\e[#{color_code}m#{text}\e[0m"
else
text
end
end
def bright
colorize(self, "1")
end
# 2 is "faint" but isn't widely supported and ends up being ul
# 3 is "italic" " " swap
def ul
colorize(self, "4")
end
def blink
colorize(self, "5")
end
# 6 is "fastblink" but is only for MS-DOS(?)
def swap
colorize(self, "7")
end
# 8 is "conceal" and, you guessed it, isn't widely supported
def black
colorize(self, "30")
end
def red
colorize(self, "31")
end
def green
colorize(self, "32")
end
def yellow
colorize(self, "33")
end
def blue
colorize(self, "34")
end
def magenta
colorize(self, "35")
end
def cyan
colorize(self, "36")
end
def white
colorize(self, "37")
end
def blackBg
colorize(self, "40")
end
def redBg
colorize(self, "41")
end
def greenBg
colorize(self, "42")
end
def yellowBg
colorize(self, "43")
end
def blueBg
colorize(self, "44")
end
def magentaBg
colorize(self, "45")
end
def cyanBg
colorize(self, "46")
end
def whiteBg
colorize(self, "47")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment