Giter Club home page Giter Club logo

ansi256's Introduction

ansi256

ansi256 is a rubygem for colorizing text with 256-color ANSI codes.

Features:

  • Supports both named color codes and numeric 256-color codes
  • Allows nesting of colored text
  • Generates optimal(shortest) code sequence

Installation

$ gem install ansi256

Basic usage

Numeric 256-color codes

require 'ansi256'

# Foreground color
puts "Colorize me".fg(111)

# With background color
puts "Colorize me".fg(111).bg(226)

# Also with underline
puts "Colorize me".fg(111).bg(226).underline

# Strip ANSI codes
puts "Colorize me".fg(111).bg(226).underline.plain

colorize-me

16 named colors

s = "Colorize me"
puts [ s.black,   s.black.bold,   s.white.bold.on_black   ].join ' '
puts [ s.red,     s.red.bold,     s.white.bold.on_red     ].join ' '
puts [ s.green,   s.green.bold,   s.white.bold.on_green   ].join ' '
puts [ s.yellow,  s.yellow.bold,  s.white.bold.on_yellow  ].join ' '
puts [ s.blue,    s.blue.bold,    s.white.bold.on_blue    ].join ' '
puts [ s.magenta, s.magenta.bold, s.white.bold.on_magenta ].join ' '
puts [ s.cyan,    s.cyan.bold,    s.white.bold.on_cyan    ].join ' '
puts [ s.white,   s.white.bold,   s.white.bold.on_white   ].join ' '

colorize-me-16

RGB color approximated to 256-color ANSI code

puts "RGB Color (RRGGBB)".fg('ff9930').bg('203366')

puts "RGB Color (R-G-B-)".fg('f90').bg('036')

puts "RGB Color (Monochrome)".fg('ef').bg('3f')

Nesting

Unlike the other similar gems, Ansi256 allows you to nest colored text.

require 'ansi256'

puts           world = "World".bg(226).fg(232).underline
puts     hello_world = "Hello #{world} !".fg(230).bg(75)
puts say_hello_world = "Say '#{hello_world}'".fg(30)
puts say_hello_world.plain.fg(27)

say-hello-world

Ansi256 methods

Ansi256.fg(232)
Ansi256.bg(226)
Ansi256.green
Ansi256.on_green
Ansi256.bold
Ansi256.dim
Ansi256.italic
Ansi256.underline
Ansi256.reset

Ansi256.fg(232, 'Hello')
Ansi256.bg(226, 'World')
Ansi256.green('Hello World')

Disabling extended String methods

Ansi256.enabled?
  # true

# Print colored output only when STDOUT is tty
Ansi256.enabled = $stdout.tty?

"Hello".fg(232)
  # Hello

ansi256 executable

Ansi256 comes with ansi256 script which can be used as follows

usage: ansi256 [-b] [-d] [-i] [-u] <[fg][/bg]> [message]

# Numeric color codes
ansi256 232 "Hello world"
ansi256 /226 "Hello world"
ansi256 232/226 "Hello world"

# Named color codes
ansi256 yellow "Hello world"
ansi256 /blue "Hello world"
ansi256 yellow/blue "Hello world"

# RGB colors (only support 6-letter hex codes)
ansi256 ff9900/000033 "Hello world"

# Mixed color codes
ansi256 yellow/232 "Hello world"

# Bold yellow
ansi256 yellow/232 -b "Hello world"

# With underline
ansi256 yellow/232 -b -u "Hello world"

# Colorizing STDIN
find / | ansi256 -u /226

# Nesting
ansi256 30 "Say '$(ansi256 230/75 "Hello $(ansi256 -u 232/226 World)")'"

Color chart

256-color table

require 'ansi256'

def cfmt col
  col.to_s.rjust(5).fg(232).bg(col)
end

puts (0..7).map  { |col| cfmt col }.join
puts (8..15).map { |col| cfmt col }.join
(16..255).each_slice(6) do |slice|
  puts slice.map { |col| cfmt col }.join
end

Reference chart

xterm-color-chart.png

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

ansi256's People

Contributors

junegunn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.