Created
March 4, 2019 19:59
-
-
Save janfri/3d26c0f2c2da29ad920a84d5ca57a2ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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