Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: v1.0.7
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 120 lines (92 sloc) 4.618 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
== Term::ANSIColor - ANSI escape sequences in Ruby

=== Description

This library can be used to color/uncolor strings using ANSI escape sequences.

=== Author

Florian Frank mailto:flori@ping.de

=== License

This is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License Version 2 as published by the Free
Software Foundation: www.gnu.org/copyleft/gpl.html

=== Download

The latest version of this library can be downloaded at

* http://rubyforge.org/frs?group_id=391

The homepage of this library is located at

* http://term-ansicolor.rubyforge.org

=== Examples

The file examples/example.rb in the source/gem-distribution shows how
this library can be used:
 require 'term/ansicolor'
 
 # Use this trick to work around namespace cluttering that
 # happens if you just include Term::ANSIColor:
 
 class Color
   class << self
     include Term::ANSIColor
   end
 end
 
 print Color.red, Color.bold, "No Namespace cluttering:", Color.clear, "\n"
 print Color.green + "green" + Color.clear, "\n"
 print Color.on_red(Color.green("green")), "\n"
 print Color.yellow { Color.on_black { "yellow on_black" } }, "\n\n"
 
 # Or shortcut Term::ANSIColor by assignment:
 c = Term::ANSIColor
 
 print c.red, c.bold, "No Namespace cluttering (alternative):", c.clear, "\n"
 print c.green + "green" + c.clear, "\n"
 print c.on_red(c.green("green")), "\n"
 print c.yellow { c.on_black { "yellow on_black" } }, "\n\n"
 
 # Anyway, I don't define any of Term::ANSIColor's methods in this example
 # and I want to keep it short:
 include Term::ANSIColor
 
 print red, bold, "Usage as constants:", reset, "\n"
 print clear, "clear", reset, reset, "reset", reset,
   bold, "bold", reset, dark, "dark", reset,
   underscore, "underscore", reset, blink, "blink", reset,
   negative, "negative", reset, concealed, "concealed", reset, "|\n",
   black, "black", reset, red, "red", reset, green, "green", reset,
   yellow, "yellow", reset, blue, "blue", reset, magenta, "magenta", reset,
   cyan, "cyan", reset, white, "white", reset, "|\n",
   on_black, "on_black", reset, on_red, "on_red", reset,
   on_green, "on_green", reset, on_yellow, "on_yellow", reset,
   on_blue, "on_blue", reset, on_magenta, "on_magenta", reset,
   on_cyan, "on_cyan", reset, on_white, "on_white", reset, "|\n\n"
 
 print red, bold, "Usage as unary argument methods:", reset, "\n"
 print clear("clear"), reset("reset"), bold("bold"), dark("dark"),
   underscore("underscore"), blink("blink"), negative("negative"),
   concealed("concealed"), "|\n",
   black("black"), red("red"), green("green"), yellow("yellow"),
   blue("blue"), magenta("magenta"), cyan("cyan"), white("white"), "|\n",
   on_black("on_black"), on_red("on_red"), on_green("on_green"),#
   on_yellow("on_yellow"), on_blue("on_blue"), on_magenta("on_magenta"),
   on_cyan("on_cyan"), on_white("on_white"), "|\n\n"
 
 print red { bold { "Usage as block forms:" } }, "\n"
 print clear { "clear" }, reset { "reset" }, bold { "bold" },
   dark { "dark" }, underscore { "underscore" }, blink { "blink" },
   negative { "negative" }, concealed { "concealed" }, "|\n",
   black { "black" }, red { "red" }, green { "green" },
   yellow { "yellow" }, blue { "blue" }, magenta { "magenta" },
   cyan { "cyan" }, white { "white" }, "|\n",
   on_black { "on_black" }, on_red { "on_red" }, on_green { "on_green" },
   on_yellow { "on_yellow" }, on_blue { "on_blue" },
   on_magenta { "on_magenta" }, on_cyan { "on_cyan" },
   on_white { "on_white" }, "|\n\n"
 
 # Usage as Mixin into String or its Subclasses
 class String
   include Term::ANSIColor
 end
 
 print "Usage as String Mixins:".red.bold, "\n"
 print "clear".clear, "reset".reset, "bold".bold, "dark".dark,
   "underscore".underscore, "blink".blink, "negative".negative,
   "concealed".concealed, "|\n",
   "black".black, "red".red, "green".green, "yellow".yellow,
   "blue".blue, "magenta".magenta, "cyan".cyan, "white".white, "|\n",
   "on_black".on_black, "on_red".on_red, "on_green".on_green,
   "on_yellow".on_yellow, "on_blue".on_blue, "on_magenta".on_magenta,
   "on_cyan".on_cyan, "on_white".on_white, "|\n\n"
 
 symbols = Term::ANSIColor::attributes
 print red { bold { "All supported attributes = " } },
   blue { symbols.inspect }, "\n\n"
 
 print "Send symbols to strings:".send(:red).send(:bold), "\n"
 print symbols[12, 8].map { |c| c.to_s.send(c) }, "\n\n"
 
 print red { bold { "Make strings monochromatic again:" } }, "\n"
 print [ "red".red, "not red anymore".red.uncolored,
   uncolored { "not red anymore".red }, uncolored("not red anymore".red)
     ].map { |x| x + "\n" }
Something went wrong with that request. Please try again.