colored2-4.0.0/0000755000004100000410000000000014531040337013254 5ustar www-datawww-datacolored2-4.0.0/README.md0000640000004100000410000000742514531040337014537 0ustar www-datawww-data [![RSpecs](https://github.com/kigster/colored2/actions/workflows/ruby-ci.yml/badge.svg)](https://github.com/kigster/colored2/actions/workflows/ruby-ci.yml) [![Rubocop](https://github.com/kigster/colored2/actions/workflows/rubocop-ci.yml/badge.svg)](https://github.com/kigster/colored2/actions/workflows/rubocop-ci.yml) [![Gem Version](https://badge.fury.io/rb/colored2.svg)](https://badge.fury.io/rb/colored2) [![Gitter](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/colored2) ## Colored2 This is an actively maintained fork of Chris (defunkt) Wanstrath's gem [colored](https://github.com/defunkt/colored), which appears to be no longer supported. This fork comes with a slightly spruced up syntax, some additional features, and a test suite written in [RSpec](http://rspec.info/). ## Please Consider Donating Donate using Liberapay Donate using Liberapay ## Usage In addition to the simple syntax of the original gem, which affected only the string to the left of the method call, the new "bang" syntax affects a string to the right. If the block or a method argument is provided, the contents is wrapped in the color, and the color is then reset back. If no block or argument is provided, the color is left open-ended, and must be explicitly reset – when using the 'bang' notation. ![](doc/colored2-session1.png) ### Complete set of colors: * black * red * green * yellow * blue * magenta * cyan * white ### Complete Set of Effects > Note: previous versions used method name `clear` instead of `no_color`, which clashed with many 3rd party frameworks that defined similarly named method in the global namespace. > This highlights the dangers of introducing so many words into the `String` namespace. * no_color * bold * dark * italic * underlined * reversed * plain * normal ## Usage in Other Classes With this gem you can add color to not just strings, but to any other class. `String` class is automatically decorated as soon as `require 'colored2'` is parsed by the ruby interpreter. Note that future versions may refrain from auto-requiring `colored2/strings`, and rely on explicitly requiring components they need colorized, eg `require 'colored2/numbers'`. To color numbers, require the following file, which automatically decorates `Integer` and `Float`. You can also add color methods to the `Object`. Finally, you can add the methods to any custom class by including the `Colored2` Module. Below is an `IRB` — session that shows a slightly more advanced usage. ![](doc/colored2-session2.png) ## Additional Helpers There are several additional helpers tucked onto the `String` class. * `#to_bol` (to beginning of the line) will rewind the cursor back to the beginning of the current line. * `#to_eol` (to end of line) ## Installation Add this line to your application's Gemfile: gem 'colored2' And then execute: $ bundle Or install it yourself as: $ gem install colored2 ## Development To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at [https://github.com/kigster/colored2](https://github.com/kigster/colored2). ## License The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). colored2-4.0.0/LICENSE0000640000004100000410000000213114531040337014252 0ustar www-datawww-dataCopyright (c) 2010 Chris Wanstrath Copyright (c) 2016 Konstantin Gredeskoul MIT License 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. colored2-4.0.0/colored2.gemspec0000644000004100000410000000410714531040337016334 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: colored2 4.0.0 ruby lib Gem::Specification.new do |s| s.name = "colored2".freeze s.version = "4.0.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "rubygems_mfa_required" => "true" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] s.authors = ["Chris Wanstrath".freeze, "Konstantin Gredeskoul".freeze] s.date = "2023-08-24" s.description = "This is a heavily modified fork of http://github.com/defunkt/colored gem, with many\nsensible pull requests combined. Since the authors of the original gem no longer support it,\nthis might, perhaps, be considered a good alternative.\n\nSimple gem that adds various color methods to String class, and can be used as follows:\n\n require 'colored2'\n\n puts 'this is red'.red\n puts 'this is red with a yellow background'.red.on.yellow\n puts 'this is red with and italic'.red.italic\n puts 'this is green bold'.green.bold << ' and regular'.green\n puts 'this is really bold blue on white but reversed'.bold.blue.on.white.reversed\n puts 'this is regular, but '.red! << 'this is red '.yellow! << ' and yellow.'.no_color!\n puts ('this is regular, but '.red! do\n 'this is red '.yellow! do\n ' and yellow.'.no_color!\n end\n end)\n\n".freeze s.email = "kigster@gmail.com".freeze s.files = ["LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "lib/colored2.rb".freeze, "lib/colored2/ascii_decorator.rb".freeze, "lib/colored2/codes.rb".freeze, "lib/colored2/numbers.rb".freeze, "lib/colored2/object.rb".freeze, "lib/colored2/strings.rb".freeze, "lib/colored2/version.rb".freeze] s.homepage = "http://github.com/kigster/colored2".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 3.1".freeze) s.rubygems_version = "3.3.15".freeze s.summary = "Add even more color to your life.".freeze end colored2-4.0.0/Rakefile0000640000004100000410000000047014531040337014716 0ustar www-datawww-data# frozen_string_literal: false require 'bundler' require 'bundler/gem_tasks' require 'rake/clean' CLEAN.include %w(pkg coverage *.gem) begin require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) rescue LoadError => e warn "Error loading task: #{e.message}" exit 1 end task default: [:spec] colored2-4.0.0/lib/0000755000004100000410000000000014531040337014022 5ustar www-datawww-datacolored2-4.0.0/lib/colored2.rb0000640000004100000410000000466214531040337016064 0ustar www-datawww-data# frozen_string_literal: false require 'colored2/codes' require 'colored2/ascii_decorator' module Colored2 def self.decorate(a_class) a_class.send(:include, Colored2) end def self.included(from_class) from_class.class_eval do def surround_with_color(color: nil, effect: nil, color_self: nil, string: nil, &block) color_type = if Colored2.background_next? && effect.nil? Colored2.foreground_next! :background else :foreground end opts = {} opts.merge!(color_type => color) if color opts.merge!(effect:) if effect if color_self opts.merge!( beginning: :on, end: :off) colored = Colored2::AsciiDecorator.new(self).decorate(opts) if string || block arg = "#{string}#{block&.call}" colored << Colored2::AsciiDecorator.new(arg).decorate(opts) if !arg.empty? end else opts.merge!( end: :on ) colored = Colored2::AsciiDecorator.new(self).decorate(opts) if string || block arg = "#{string}#{block&.call}" colored << Colored2::AsciiDecorator.new(arg).decorate(opts.merge(end: :off)) if !arg.empty? end end colored end def on Colored2.background_next! self end end from_class.instance_eval do COLORS.each_key do |color| define_method(color) do |string = nil, &block| surround_with_color(color:, color_self: true, string:, &block) end define_method("#{color}!".to_sym) do |string = nil, &block| surround_with_color(color:, color_self: false, string:, &block) end end EFFECTS.each_key do |effect| next if effect == 'no_color' define_method(effect) do |string = nil, &block| surround_with_color(effect:, color_self: true, string:, &block) end define_method("#{effect}!".to_sym) do |string = nil, &block| surround_with_color(effect:, color_self: false, string:, &block) end end define_method(:to_eol) do tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K") if tmp == self return "\e[2K" << self end tmp end define_method(:to_bol) do "#{self}\033[#{length}D\033[0D" end end end end require 'colored2/strings' colored2-4.0.0/lib/colored2/0000755000004100000410000000000014531040337015533 5ustar www-datawww-datacolored2-4.0.0/lib/colored2/version.rb0000640000004100000410000000010714531040337017537 0ustar www-datawww-data# frozen_string_literal: true module Colored2 VERSION = '4.0.0' end colored2-4.0.0/lib/colored2/codes.rb0000640000004100000410000000201414531040337017146 0ustar www-datawww-data# frozen_string_literal: false module Colored2 COLORS = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 }.freeze EFFECTS = { no_color: 0, bold: 1, dark: 2, italic: 3, underlined: 4, reversed: 7, plain: 21, # non-bold normal: 22 }.freeze class Code attr_accessor :name, :escape def initialize(name) @name = name return if name.nil? @escape = codes[name.to_sym] raise ArgumentError, "No color or effect named #{name} exists for #{self.class}." if @escape.nil? end def value(shift = nil) escape_code = escape escape_code += shift if shift && escape_code name && escape ? "\e[#{escape_code}m" : '' end def to_s value end end class Effect < Code def codes EFFECTS end end class TextColor < Code def codes COLORS end end class BackgroundColor < TextColor def value super 10 end end end colored2-4.0.0/lib/colored2/ascii_decorator.rb0000640000004100000410000000416214531040337021211 0ustar www-datawww-data# frozen_string_literal: false require 'colored2/codes' require 'forwardable' module Colored2 def self.enable! Colored2::AsciiDecorator.enable! end def self.disable! Colored2::AsciiDecorator.disable! end def self.background_next! Colored2::AsciiDecorator.background_next! end def self.foreground_next! Colored2::AsciiDecorator.foreground_next! end def self.background_next? Colored2::AsciiDecorator.background_next? end class AsciiDecorator @__background_next = false @__colors_disabled = false class << self attr_accessor :__background_next, :__colors_disabled def enable! self.__colors_disabled = false end def enabled? !__colors_disabled end def disable! self.__colors_disabled = true end def background_next! self.__background_next = true end def foreground_next! self.__background_next = false end def background_next? __background_next end end extend Forwardable def_delegators :@my_class, :enable!, :disable! attr_accessor :string, :my_class def initialize(a_string) self.string = a_string.instance_of?(Object) ? '' : a_string.to_s self.my_class = self.class end # options[:start] = :color # options[:end] = :color | :no_color def decorate(options = {}) return string if !self.class.enabled? || string.empty? escape_sequence = [ Colored2::TextColor.new(options[:foreground]), Colored2::BackgroundColor.new(options[:background]), Colored2::Effect.new(options[:effect]) ].compact.join colored = '' colored << escape_sequence if options[:beginning] == :on colored << string if options[:end] colored << no_color if options[:end] == :off && !colored.end_with?(no_color) colored << escape_sequence if options[:end] == :on end colored end def un_decorate string.gsub(/\e\[\d+(;\d+)*m/, '') end private def no_color @no_color ||= Colored2::Effect.new(:no_color).to_s end end end colored2-4.0.0/lib/colored2/strings.rb0000640000004100000410000000021214531040337017540 0ustar www-datawww-data# frozen_string_literal: false require 'colored2' unless defined?(Colored2) && Colored2.respond_to?(:decorate) Colored2.decorate(String) colored2-4.0.0/lib/colored2/object.rb0000640000004100000410000000021214531040337017315 0ustar www-datawww-data# frozen_string_literal: false require 'colored2' unless defined?(Colored2) && Colored2.respond_to?(:decorate) Colored2.decorate(Object) colored2-4.0.0/lib/colored2/numbers.rb0000640000004100000410000000052014531040337017524 0ustar www-datawww-data# frozen_string_literal: false require 'colored2' unless defined?(Colored2) && Colored2.respond_to?(:decorate) module Colored2 def self.integer_class major, minor = RUBY_VERSION.split(".").map(&:to_i) major >= 2 && minor >= 4 ? Integer : Numeric end end Colored2.decorate(Colored2.integer_class) Colored2.decorate(Float)