A very simple ANSI manipulation library, checkout the examples below:
require 'rubygems' require 'isna' "Hello World".to_ansi "Hello World".to_ansi.red puts "Hello World".to_ansi.red puts "Hello World".to_ansi.underline.blue puts "Hello World".to_ansi.blink.yellow puts "Hello World".to_ansi.cyan puts "Hello World".to_ansi.negative.cyan puts "Hello World".to_ansi.green_background.yellow puts "Hello World".to_ansi.green_background.bright.red puts "Hello World".to_ansi.green_background.red puts "Hello World".to_ansi.green_background.dark.red # *bright* and *dark* only work when a background is applied. puts "Normal String" + ' concatenated-with-a ' + "Ansi String".to_ansi.green.to_s
I’ve just added the very basic types, but you should be able to extend it pretty easily, if you think those changes should be on the core-gem send them over and I’ll apply those for you. (and add your name in the credits section) :)
* black * red * green * yellow * blue * pink * cyan * white
* black_background * red_background * green_background * yellow_background * blue_background * pink_background * cyan_background * white_background
* normal * bright * dark * italic * underline * blink * fast_blink * negative
You can create custom ansi combos easily! In the following example am replicating the exact same behaviour that some in-library functions do. But I am in control and manually set every single value I want to change. Check out the example :)
require 'rubygems' require 'isna' "Hello World".to_ansi.change_type(5).change_background(44).change_color(33) "Hello World".to_ansi.blue_background.blink.yellow puts "Hello World".to_ansi.change_type(5).change_background(44).change_color(33) puts "Hello World".to_ansi.blue_background.blink.yellow
Want to extend and create your own cool formated methods? Perhaps you want to add xterm colors +(255) colors would be great right? The snippet below shows an easy way to extend isna yet keeping a clean and organized separation of code, and its quite simple :)
require 'rubygems' require 'isna' module MySpecialExtension def blinky_blue_1 change_type(5).change_background(44).change_color(33) end def blinky_blue_2 blue_background.blink.yellow end end Isna::String.send(:include, MySpecialExtension) "Hello World".to_ansi.blinky_blue_1 "Hello World".to_ansi.blinky_blue_2 puts "Hello World".to_ansi.blinky_blue_1 puts "Hello World".to_ansi.blinky_blue_2
If you are thinking about extending look at the internals of this library. Specially Behaviours, Colors, and Backgrounds it’ll give you a good idea on how to code/organize your extensions. :)
This is all the code that isna uses to extend strings.
class String def to_ansi Isna::String.new(self) end end
There are no overrides, no intrusion, and no posible collisions withing the Data Type Object. Isna tries to do all its hard work separately and play nice with other libraries :)
“isna” is “ansi” spelled backwards, but the gem name was already taken :S