Skip to content

Instantly share code, notes, and snippets.

@janfri
Created March 4, 2019 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janfri/3d26c0f2c2da29ad920a84d5ca57a2ef to your computer and use it in GitHub Desktop.
Save janfri/3d26c0f2c2da29ad920a84d5ca57a2ef to your computer and use it in GitHub Desktop.
# encoding: utf-8
# frozen_string_literal: true
# Copyright (c) 2019 by Jan Friedrich <janfri26@gmail.com>
# License: Public Domain
module Ansicolor
@codes = {}
%i(bold faint italic underline).each_with_index do |mode, i|
@codes[mode] = 1 + i
end
%i(black red green yellow blue magenta cyan white).each_with_index do |color, i|
@codes[color] = 30 + i
@codes[format('@%s', color).to_sym] = 40 + i
@codes[format('light%s', color).to_sym] = 90 + i
@codes[format('@light%s', color).to_sym] = 100 + i
end
class << self
def apply str, *codes
codes.flatten.each do |c|
num = @codes[c.to_sym]
fail format('Code %s not supported.', c) unless num
str = format("\e[%dm%s", num, str)
end
format("%s\e[0m", str)
end
def codes
@codes.keys
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment