This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
source "https://rubygems.org"
|
||||
gemspec
|
||||
|
||||
gem "rake"
|
||||
group :development do
|
||||
gem "rspec-helpers", :require => false
|
||||
gem "luna-rspec-formatters", :require => false
|
||||
gem "pry", :require => false unless ENV[
|
||||
"CI"
|
||||
]
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
## 1.1.0 / 2016-06-28
|
||||
|
||||
### Minor Enhancements
|
||||
|
||||
* Support jruby (#8)
|
||||
|
||||
## 1.0.0 / 2016-04-28
|
||||
|
||||
### Major enhancements
|
||||
|
||||
- Merge Simple::ANSI and Colorator. (#7)
|
||||
|
||||
### Minor Enhancements
|
||||
|
||||
- Delete unnecessary `Symbol#to_sym` (#2)
|
||||
- Change argument name of `Enumerator#each` for better code legibility (#3)
|
||||
|
||||
### Development Fixes
|
||||
|
||||
- Convert to new RSpec expectation syntax (#1)
|
||||
- Fix `String#blue` result in README (#4)
|
||||
|
||||
## 0.1 / 2013-04-13
|
||||
|
||||
Birthday!
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) Parker Moore
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -0,0 +1,47 @@
|
||||
# colorator
|
||||
|
||||
Colorize your text for the terminal
|
||||
|
||||
[](https://travis-ci.org/octopress/colorator)
|
||||
|
||||
## Example
|
||||
|
||||
```ruby
|
||||
"this string".red
|
||||
# => \e[31mthis string\e[0m
|
||||
"my string".blue
|
||||
# => \e[34mmy string\e[0m
|
||||
# etc...
|
||||
```
|
||||
|
||||
## Supported Colors
|
||||
|
||||
- `red`
|
||||
- `black`
|
||||
- `green`
|
||||
- `yellow`
|
||||
- `magenta`
|
||||
- `white`
|
||||
- `blue`
|
||||
- `cyan`
|
||||
- `bold`
|
||||
|
||||
## Other supported Ansi methods
|
||||
|
||||
- `clear_line`
|
||||
- `has_ansi?`, `has_color?`
|
||||
- `strip_ansi`, `strip_color`
|
||||
- `reset_ansi`, `reset_color`
|
||||
- `clear_screen`
|
||||
- `ansi_jump`
|
||||
|
||||
## Why
|
||||
|
||||
There are a bunch of gems that provide functionality like this, but none have
|
||||
as simple an API as this. Just call `"string".color` and your text will be
|
||||
colorized.
|
||||
|
||||
## License
|
||||
|
||||
MIT. Written as a single Ruby file by Brandon Mathis, converted into a gem by
|
||||
Parker Moore.
|
||||
@@ -0,0 +1,6 @@
|
||||
require "bundler/gem_tasks"
|
||||
require "rspec/core/rake_task"
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
task :default => :spec
|
||||
@@ -0,0 +1,23 @@
|
||||
# coding: utf-8
|
||||
|
||||
require File.expand_path('lib/colorator.rb', __dir__)
|
||||
|
||||
Gem::Specification.new do |spec|
|
||||
spec.name = "colorator"
|
||||
spec.summary = "Colorize your text in the terminal."
|
||||
spec.version = Colorator::VERSION
|
||||
spec.authors = ["Parker Moore", "Brandon Mathis"]
|
||||
spec.email = ["parkrmoore@gmail.com", "brandon@imathis.com"]
|
||||
spec.homepage = "https://github.com/octopress/colorator"
|
||||
spec.licenses = ["MIT"]
|
||||
|
||||
all = `git ls-files -z`.split("\x0").reject { |f| f.start_with?(".") }
|
||||
spec.files = all.select { |f| File.basename(f) == f || f =~ %r{^(bin|lib)/} }
|
||||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
||||
spec.require_paths = ["lib"]
|
||||
|
||||
spec.extra_rdoc_files = ["README.markdown", "LICENSE"]
|
||||
spec.rdoc_options = ["--charset=UTF-8"]
|
||||
|
||||
spec.add_development_dependency "rspec", "~> 3.1"
|
||||
end
|
||||
@@ -0,0 +1,111 @@
|
||||
$:.unshift File.dirname(__FILE__)
|
||||
|
||||
module Colorator
|
||||
module_function
|
||||
VERSION = "1.1.0"
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
ANSI_MATCHR = /\x1b.*?[jkmsuABGKH]/
|
||||
ANSI_COLORS = {
|
||||
:black => 30,
|
||||
:red => 31,
|
||||
:green => 32,
|
||||
:yellow => 33,
|
||||
:blue => 34,
|
||||
:magenta => 35,
|
||||
:cyan => 36,
|
||||
:white => 37,
|
||||
:bold => 1
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Allows you to check if a string currently has ansi.
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def has_ansi?(str)
|
||||
str.match(ANSI_MATCHR).is_a?(
|
||||
MatchData
|
||||
)
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Jump the cursor, moving it up and then back down to it's spot, allowing
|
||||
# you to do fancy things like multiple output (downloads) the way that Docker
|
||||
# does them in an async way without breaking term.
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def ansi_jump(str, num)
|
||||
"\x1b[#{num}A#{clear_line(str)}\x1b[#{
|
||||
num
|
||||
}B"
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def reset_ansi(str = "")
|
||||
"\x1b[0m#{
|
||||
str
|
||||
}"
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def clear_line(str = "")
|
||||
"\x1b[2K\r#{
|
||||
str
|
||||
}"
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Strip ANSI from the current string, making it just a normal string.
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def strip_ansi(str)
|
||||
str.gsub(
|
||||
ANSI_MATCHR, ""
|
||||
)
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Clear the screen's current view, so the user gets a clean output.
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def clear_screen(str = "")
|
||||
"\x1b[H\x1b[2J#{
|
||||
str
|
||||
}"
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def colorize(str = "", color)
|
||||
"\x1b[#{color}m#{str}\x1b[0m"
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
Colorator::ANSI_COLORS.each do |color, code|
|
||||
define_singleton_method color do |str|
|
||||
colorize(
|
||||
str, code
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
class << self
|
||||
alias reset_color reset_ansi
|
||||
alias strip_color strip_ansi
|
||||
alias has_color? has_ansi?
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
CORE_METHODS = (
|
||||
public_methods - Object.methods
|
||||
)
|
||||
end
|
||||
|
||||
require "colorator/core_ext"
|
||||
@@ -0,0 +1,9 @@
|
||||
class String
|
||||
Colorator::CORE_METHODS.each do |method|
|
||||
define_method method do |*args|
|
||||
Colorator.public_send(method,
|
||||
self, *args
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user