l43_color

ANSI colors and syntactic color highlightening

Gem Version

Synopsis

Provides a module with functions to create ANSI color codes. The module can be included into String for convenience.

It also provides a parser that changes color instruction inside a string to according ANSI codes, and a command line tool that does this for input.

Documentation

The behavior and use of this gem is documented here by means of speculations

New in v0.2: Colorschemes

Documented in `L43::Color::Colorschemes

Context: Backbone Functionality, issuing ANSI codes

Given the L43::Color module

   let(:color) { L43::Color } 

Then we can ask for ANSI codes

    expect(L43::Color.ansi_code("yellow")).to eq("\e[33m")
    expect(L43::Color.ansi_code("bg_black")).to eq("\e[40m")
    expect(L43::Color.ansi_code("reset")).to eq("\e[0m")
    expect(L43::Color.ansi_code("dim")).to eq("\e[2m")

And if we try a nonexisting color we get an error

   expect { L43::Color.ansi_code("no_color") }
   .to raise_error(L43::Color::UndefinedColorCode, "no_color is not a color code")

Context: Injecting color methods into strings

Given the inclusion of the string inject module

   require 'l43/color/string_methods' 

Then we can make colored strings as follows

    expect("red".red).to eq("\e[31mred")

Context: Conveniently named methods

Given the following

   require 'l43/color/methods' 

Then we can get the ANSI codes as functions

   expect(L43::Color::Methods.cyan).to eq("\x1b[36m") 

And to get rid of the long name this module can be included

    c = Class.new do
          include L43::Color::Methods
        end
   expect(c.new.blue).to eq("\e[34m")

Or extended

   m = Module.new do
         extend L43::Color::Methods
       end
   expect(m.magenta).to eq("\e[35m")

Context: Output Helpers

They allow to alternate strings (to be printed verbatim) and symbols that will output ANSI codes

Given they are included

   require 'l43/color/output' 
   include L43::Color::Output

Then we can use them to output to stdout

   expected = "\e[31mhello\e[0mworld\n"
   expect { putcol([:red, "hello", :reset, "world"]) }
    .to output(expected).to_stdout_from_any_process

   expect { putcol([:red, "hello", :reset, "world"], to: :stdout) }
    .to output(expected).to_stdout_from_any_process

Or we can use them to output to stderr

   expected = "\e[41mhello\e[4mworld\n"
   expect { putcol([:bg_red, "hello", :ul, "world"], to: :stderr) }
    .to output(expected).to_stderr_from_any_process

   expect { putcol([:bg_red, "hello", :ul, "world"], to: $stderr) }
    .to output(expected).to_stderr_from_any_process

Context: colorize

Given we include Output again

   require 'l43/color/output' 
   include L43::Color::Output

We can also convert text, symbolic chunks and nil to meaningful output

Example: Useful for help texts

    expected = "  \e[34mhello\e[0m\n"
    expect(colorize(1, :blue, "hello", :reset, nil, reset: false))
      .to eq(expected)

And we can also output that stuff

    expected = " \e[34mhello\n"
    expect { putc(1, :blue, "hello", nil, indent: 1, reset: false) }
      .to output(expected).to_stderr_from_any_process

Author

Copyright © 202[4-6] Robert Dober robert.dober@gmail.com

LICENSE

GNU AFFERO GENERAL PUBLIC LICENSE, Version 3, 19 November 2007. Please refer to LICENSE for details.