Project

kolorit

0.0
Repository is archived
No release in over a year
Colorize terminal output without touching core classes. Work for Linux and Mac, require `win32console` for Windows. Use on any class, auto #puts or #print, with power of ruby blocks.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.3
~> 13.0
 Project Readme

DEPRECATED

Kolorit

GIF Preview

Still in early development stage, meaning windows support should come in near future. For now, windows 10 users should be able to use it with win32console gem, but it's not tested yet.

Kolorit allow you to easily colorize any string, integer, array, hash, without touching core classes. Use methods #colorize and #kolorize for power of Ruby blocks.

How to install

GitHub repo is always updated before rubygems.

git clone https://www.github.com/alx3dev/kolorit.git
cd kolorit
bundle install

Install from rubygems:

gem install kolorit

How to use:

  • require kolorit gem in your file
# you can call method on string only if you include module outside of main namespace.
# that's what happen when you require kolorit

require 'kolorit'
'this is red string'.red
  • Inlcude kolorit module to be used where you need it
# if you include it inside another class/module, call methods with arguments

require 'kolorit/linux' # or 'kolorit/windows'

include Kolorit::Linux # or ::Windows

blue 'some string'
red 'red bold string'

#colorize accept color as first argument, and string as second
#kolorize accept string as first argument and color as second

Both of this methods also accept block - #colorize block should return :color, while #kolorize block should return 'string' to be colorized.

You can allow methods to automatically #puts or #print colorized string:

# this call without arguments is same as with :puts
Kolorit.output :puts
# or
Kolorit.output :print

# everything else is same as false
Kolorit.output false

Enable or disable colorization globally:

Kolorit.disable
# or
Kolorit.enable

Examples are better then documentation, so until I write docs...

[example 1]

colorize(:green) do
  if RUBY_VERSION.start_with?('3')
    'Happy Coding!'.bold
  else
    'You should try Ruby 3, it is much better!'
  end
end

[example 2]

def color_line(param)

  # check if kolorit is enabled (default: true)
  status = Kolorit.enabled?

  # enable kolorit if disabled
  Kolorit.enable

  # colorize param based on it's class
  colorize(:cyan) do
    case param.class.name
    when 'String'
      :green
    when 'Integer'
      :yellow
    else
      :gray
    end
  end

  # keep module status same as before method action
  Kolorit.disable unless status
end

[example 3]

# enable automated #puts or #print
Kolorit.output :puts

# define message to print
message = 'Thank you Matz, for Amazing Ruby '

# print bold cyan message based on block evaluation
colorize(:bold) { RUBY_VERSION[0] == '3' ? message + '3' : message + 'language'}.cyan

# disable output
Kolorit.output false