Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

l43_color

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

l43_color

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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

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(IndexError, "key not found: :no_color")

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

Author

Copyright © 2024 Robert Dober robert.dober@gmail.com

LICENSE

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

FAQs

Package last updated on 27 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc