Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some style and change some code #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Colorize your text for the terminal
# etc...
```

## Supported Colors
## Supported Text Colors

- `red`
- `black`
Expand All @@ -24,7 +24,23 @@ Colorize your text for the terminal
- `white`
- `blue`
- `cyan`

## Supported Background Colors

- `red`
- `black`
- `green`
- `yellow`
- `magenta`
- `white`
- `blue`
- `cyan`

## Supported Text Style

- `bold`
- `dim`
- `underlined`

## Other supported Ansi methods

Expand Down
29 changes: 24 additions & 5 deletions lib/colorator.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
$:.unshift File.dirname(__FILE__)
# $:.unshift File.dirname(__FILE__)
$LOAD_PATH.unshift File.dirname(__FILE__)

module Colorator
module_function
Expand All @@ -7,18 +8,36 @@ module Colorator
# --------------------------------------------------------------------------

ANSI_MATCHR = /\x1b.*?[jkmsuABGKH]/
ANSI_COLORS = {
ANSI_BACKGROUND_COLORS = {
:bg_black => 40,
:bg_red => 41,
:bg_green => 42,
:bg_yellow => 43,
:bg_blue => 44,
:bg_magenta => 45,
:bg_cyan => 46,
:bg_white => 47
}

ANSI_TEXT_COLORS = {
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37,
:bold => 1
:white => 37
}

ANSI_TEXT_STYLE = {
:bold => 1,
:dim => 2,
:underlined => 4,
}

ANSI_HASH = [ANSI_TEXT_COLORS, ANSI_BACKGROUND_COLORS, ANSI_TEXT_STYLE].inject(&:merge)

# --------------------------------------------------------------------------
# Allows you to check if a string currently has ansi.
# --------------------------------------------------------------------------
Expand Down Expand Up @@ -85,7 +104,7 @@ def colorize(str = "", color)

# --------------------------------------------------------------------------

Colorator::ANSI_COLORS.each do |color, code|
Colorator::ANSI_HASH.each do |color, code|
define_singleton_method color do |str|
colorize(
str, code
Expand Down
2 changes: 1 addition & 1 deletion spec/tests/lib/colorator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec/helper"
describe Colorator do
Colorator::ANSI_COLORS.each do |color, code|
Colorator::ANSI_HASH.each do |color, code|
it "colors the text #{color} properly" do
expect(Colorator.send(color, "string")).to include code.to_s
expect(Colorator.send(color, "string")).to eq(
Expand Down