Skip to content
Browse files

better color support detection | if color is not supported, remove ta…

…gs from string
  • Loading branch information...
1 parent e67bdb9 commit d12ce369ac57dabd1d4c2637e407c7240b1ce23b cloudhead committed
Showing with 67 additions and 32 deletions.
  1. +29 −7 lib/mutter/mutterer.rb
  2. +38 −25 spec/mutter_spec.rb
View
36 lib/mutter/mutterer.rb
@@ -1,6 +1,7 @@
module Mutter
class Mutterer
- @stream = STDOUT
+ @stream = STDOUT # output stream
+ @color = false # force color
# Initialize the styles, and load the defaults from +styles.yml+
#
@@ -37,7 +38,7 @@ def initialize obj = {}
end if style.is_a? Symbol
end
end
-
+
def styles
@defaults.merge @styles
end
@@ -48,7 +49,7 @@ def clear opt = :all
when :styles then @styles, @defaults = {}, {}
when :active then @active = []
when :all then @active, @styles, @defaults = [], {}, {}
- when :default,
+ when :default,
:defaults then @defaults = {}
else raise ArgumentError, "[:user, :default, :active, :all] only"
end
@@ -69,16 +70,29 @@ def load styles
end
end
+ def color?
+ (ENV['TERM'].include?('color') && self.class.stream.tty?) || self.class.color
+ end
+
#
# Output to @stream
#
def say msg, *styles
- self.write((ENV['TERM'].include?('color') ? process(msg, *styles) : msg) + "\n")
- return nil
+ self.write((color?? process(msg, *styles) : unstyle(msg)) + "\n") ; nil
end
-
alias :print say
-
+
+ #
+ # Remove all tags from string
+ #
+ def unstyle msg
+ styles.map do |_,v|
+ v[:match]
+ end.flatten.inject(msg) do |m, tag|
+ m.gsub(tag, '')
+ end
+ end
+
#
# Parse the message, but also apply a style on the whole thing
#
@@ -184,5 +198,13 @@ def self.stream
def self.stream= io
@stream = io
end
+
+ def self.color
+ @color
+ end
+
+ def self.color= bool
+ @color = bool
+ end
end
end
View
63 spec/mutter_spec.rb
@@ -4,11 +4,12 @@
def out
File.read("spec/out.txt")
end
-
+
before(:each) do
Mutter::Mutterer.stream = File.new("spec/out.txt", 'w')
+ Mutter::Mutterer.color = true
end
-
+
after(:all) do
File.delete("spec/out.txt")
end
@@ -17,107 +18,107 @@ def out
Mutter.say "hello mutter!"
out.should == "hello mutter!\n"
end
-
+
it "should underline" do
Mutter.say "_hello mutter!_"
out.should == "\e[4mhello mutter!\e[24m\n"
end
-
+
it "should bold" do
Mutter.say "*hello mutter!*"
out.should == "\e[1mhello mutter!\e[22m\n"
end
-
+
it "should inverse" do
Mutter.say "[hello mutter!]"
out.should == "\e[7mhello mutter!\e[27m\n"
end
-
+
it "should blink" do
Mutter.say "hello mutter!", :blink
out.should == "\e[5mhello mutter!\e[25m\n"
end
-
+
it "should escape glyphs" do
Mutter.say "**hello * world**"
out.should == "\e[1mhello * world\e[22m\n"
end
-
+
it "should set actives at the instance level" do
Mutter.new([:bold, :underline]).say "hello mutter!"
out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
end
-
+
it "should set actives at the method level" do
Mutter.say "hello mutter!", :bold, :underline
out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
end
-
+
it "should set actives at both levels" do
Mutter.new(:bold).say "hello mutter!", :underline, :yellow
out.should == "\e[33m\e[4m\e[1mhello mutter!\e[22m\e[24m\e[39m\n"
end
-
+
describe "with custom styles" do
it "should work with custom styles" do
style = {
:alert => {
- :match => '!!',
+ :match => '!!',
:style => ['bold', 'red']
}
}
Mutter.new(style).say "alert!", :alert
out.should == "\e[31m\e[1malert!\e[22m\e[39m\n"
end
-
+
describe "in shorthand form" do
it "should work with simple shorthand styles" do
Mutter.new({:bold => '~'}).say "~hello mutter!~"
out.should == "\e[1mhello mutter!\e[22m\n"
end
-
+
it "should work with complex shorthand styles" do
Mutter.new({:cyan => ['<', '>']}).say "<hello mutter!>"
out.should == "\e[36mhello mutter!\e[39m\n"
end
-
+
it "should work with multiple shorthand styles" do
Mutter.new({[:cyan, :underline] => '~'}).say "~hello mutter!~"
out.should == "\e[4m\e[36mhello mutter!\e[39m\e[24m\n"
end
end
-
+
it "should mix styles" do
Mutter.new({:cyan => '~'}).say "_*hello* ~world~_"
out.should == "\e[4m\e[1mhello\e[22m \e[36mworld\e[39m\e[24m\n"
end
-
+
it "should color backgrounds" do
Mutter.new({:cyan => '~'}).say "~[hello mutter!]~"
out.should == "\e[36m\e[7mhello mutter!\e[27m\e[39m\n"
end
-
+
it "should load styles from a yaml" do
Mutter.new("spec/style").say "{important message!}"
out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[39m\n"
end
-
+
it "should be able to call styles as methods" do
Mutter.new("spec/style").important "important message!"
out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[39m\n"
end
-
+
it "should clear all styles" do
Mutter.new.clear(:all).say "[hello] *world*"
out.should == "[hello] *world*\n"
end
-
+
it "should clear active styles" do
mut = Mutter.new(:bold)
mut.clear(:active).say "hello"
out.should == "hello\n"
end
-
+
it "should add and remove active styles" do
mut = Mutter.new
mut << :bold << :underline << :inverse
@@ -125,7 +126,7 @@ def out
mut.say "hello mutter!"
out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
end
-
+
it "should return instances of itself, +- active styles" do
mut = Mutter.new
mut << :bold << :underline
@@ -133,10 +134,22 @@ def out
out.should == "\e[4mhello mutter!\e[24m\n"
end
end
-
+
it "should parse a complex string" do
m = Mutter.new({:cyan => ['<','>']})
m.say "hello *mutter*, would you _<please be quiet>_ for this <[test]>?"
out.should == "hello \e[1mmutter\e[22m, would you \e[4m\e[36mplease be quiet\e[39m\e[24m for this \e[36m\e[7mtest\e[27m\e[39m?\n"
end
-end
+
+ describe "when color isn't supported" do
+ before(:each) do
+ Mutter::Mutterer.color = false
+ end
+
+ it "should remove style tags" do
+ m = Mutter.new
+ m.say "hello *mutter*, how are _you_?"
+ out.should == "hello mutter, how are you?\n"
+ end
+ end
+end

0 comments on commit d12ce36

Please sign in to comment.
Something went wrong with that request. Please try again.