From 2a89521ca22f72fee92e6c8ffe737479bdd9f04b Mon Sep 17 00:00:00 2001 From: MidAutumnMoon Date: Sun, 10 Nov 2024 11:56:03 +0800 Subject: [PATCH] slime down functionality && release 2.0.0 --- Gemfile.lock | 2 +- lib/reinbow.rb | 11 +---------- lib/reinbow/painter.rb | 36 +++++++----------------------------- spec/painter_spec.rb | 29 +++++++---------------------- spec/refinement_spec.rb | 29 ----------------------------- 5 files changed, 16 insertions(+), 91 deletions(-) delete mode 100644 spec/refinement_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index e496312..9458de0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - reinbow (1.2.0) + reinbow (2.0.0) GEM remote: https://rubygems.org/ diff --git a/lib/reinbow.rb b/lib/reinbow.rb index 4e2f0ee..3b64f4a 100644 --- a/lib/reinbow.rb +++ b/lib/reinbow.rb @@ -4,7 +4,7 @@ module Reinbow - VERSION = "1.2.0" + VERSION = "2.0.0" refine String do Painter.instance_methods( false ).each do |name| @@ -15,12 +15,3 @@ module Reinbow end end - - -# rubocop:disable Naming/MethodName - -def Reinbow( content, enable_color: true ) - Reinbow::Painter.new( content ).reinbow!( enable_color ) -end - -# rubocop:enable Naming/MethodName diff --git a/lib/reinbow/painter.rb b/lib/reinbow/painter.rb index 1d1f3d6..1e97c0b 100644 --- a/lib/reinbow/painter.rb +++ b/lib/reinbow/painter.rb @@ -14,7 +14,6 @@ class Painter # Accepts all parameters that String.new accepts. def initialize( input ) @raw = input.to_s - @sgr_stack = [] # Ref: https://bixense.com/clicolors/ @enable = if ENV.key?( "NO_COLOR" ) @@ -26,14 +25,6 @@ def initialize( input ) end end - def reinbow? = @enable - - def reinbow!( status = true ) - @enable = status - self - end - - # # Define method for various color names # @@ -76,41 +67,28 @@ def paint!( data:, layer: :fg ) raise ArgumentError, "layer should be one of :fg, :bg or :effect" \ unless layer in :fg | :bg | :effect + sgr_code = nil + case data in Effect data => { code: } - @sgr_stack.push( "\e[#{code}m" ) + sgr_code = "\e[#{code}m" in Ansi data => { raw_code: } code = raw_code + ( layer == :fg ? 30 : 40 ) - @sgr_stack.push( "\e[#{code}m" ) + sgr_code = "\e[#{code}m" in Rgb data => { red:, green:, blue: } ground = layer == :fg ? 38 : 48 - @sgr_stack.push( "\e[#{ground};2;#{red};#{green};#{blue}m" ) + sgr_code = "\e[#{ground};2;#{red};#{green};#{blue}m" else raise NotImplementedError, "Can't paint #{data.class}" end - self - end - - - # - # String behaviours - # - - def to_s - if @enable - sgr = @sgr_stack.join( nil ) - "#{sgr}#{@raw}\e[0m" - else - @raw - end + "#{sgr_code}#{@raw}" \ + + ( @raw.end_with?( "\e[0m" ) ? "" : "\e[0m" ) end - def +( other ) = to_s + other.to_s - end end diff --git a/spec/painter_spec.rb b/spec/painter_spec.rb index 654776d..7f0e4a7 100644 --- a/spec/painter_spec.rb +++ b/spec/painter_spec.rb @@ -5,25 +5,17 @@ Painter = Reinbow::Painter +using Reinbow + # Expected results should be verified visually. describe Painter do - subject { Painter.new( "lomn" ).reinbow! } + subject { Painter.new( "lomn" ) } it "is its own class" do expect( subject ).to be_a Painter end - context "behaves like a subset of string" do - it "has #to_s" do - expect( subject.to_s ).to eq( "lomn\e[0m" ) - end - - it "has #+" do - expect( "#{subject}!!" ).to eq( "lomn\e[0m!!" ) - end - end - context "#paint!" do x11 = Reinbow::X11_COLORS @@ -40,7 +32,7 @@ ansi = Reinbow::ANSI_COLORS it "#fg for foreground" do - expect( subject.fg( ansi[:blue] ).to_s ) + expect( subject.fg( ansi[:blue] ) ) .to eq( "\e[34mlomn\e[0m" ) end @@ -50,21 +42,14 @@ end end - it "can be turned on and off" do - subject.reinbow!( false ) - expect( subject.italic.to_s ).to eq( "lomn" ) - subject.reinbow!( true ) - expect( subject.to_s ).to eq( "\e[3mlomn\e[0m" ) - end - it "has methods to paint terminal effects" do expect( subject.italic.bold.to_s ) - .to eq( "\e[3m\e[1mlomn\e[0m" ) + .to eq( "\e[1m\e[3mlomn\e[0m" ) end it "has methods to paint ANSI terminal colors" do expect( subject.red.on_blue.to_s ) - .to eq( "\e[31m\e[44mlomn\e[0m" ) + .to eq( "\e[44m\e[31mlomn\e[0m" ) end context "paint RGB colors on terminal" do @@ -90,7 +75,7 @@ x11 = Reinbow::X11_COLORS mess = subject.rgb( x11[:crimson] ).on_blue.italic.strike expect( mess.to_s ) - .to eq( "\e[38;2;220;20;60m\e[44m\e[3m\e[9mlomn\e[0m" ) + .to eq( "\e[9m\e[3m\e[44m\e[38;2;220;20;60mlomn\e[0m" ) end it "still provides access to the raw string" do diff --git a/spec/refinement_spec.rb b/spec/refinement_spec.rb deleted file mode 100644 index c6a8d21..0000000 --- a/spec/refinement_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" -require "reinbow" - - -module WithRefine - using Reinbow - def self.blue_hello = "hello".blue.reinbow!.to_s -end - -module WoRefine - def self.blue_hello = "hello".blue.to_s -end - - -describe "Refinement" do - - it "is using refinement" do - expect( WithRefine.blue_hello ) - .to eq( Reinbow( "hello" ).blue.to_s ) - end - - it "does not pullot objects" do - expect { WoRefine.blue_hello } - .to raise_error( NoMethodError ) - end - -end