From d209e42982a70af754b90a353bc462e5e643e386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Wed, 15 Feb 2017 13:24:43 +0000 Subject: [PATCH] Fix colorize stream target Includes updated spec helper capture_lines wich calls uncolorize by default. Updates Webmock to latest version c673f393 --- shard.lock | 2 +- spec/integration/basic_spec.cr | 12 ++++++++++++ spec/spec_helper.cr | 6 ++++-- src/command.cr | 8 ++++---- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/shard.lock b/shard.lock index 2bc34dd..6cc2590 100644 --- a/shard.lock +++ b/shard.lock @@ -6,5 +6,5 @@ shards: webmock: github: manastech/webmock.cr - commit: d7e2de5aa15b3f1940a873e6efc68d2203b801e1 + commit: c673f393a31eac2734c7599897357ef1f3f24011 diff --git a/spec/integration/basic_spec.cr b/spec/integration/basic_spec.cr index 3e37fa9..197ff4e 100644 --- a/spec/integration/basic_spec.cr +++ b/spec/integration/basic_spec.cr @@ -30,6 +30,18 @@ describe "Basic examples" do lines.last.should eq("Hello") end + it "colorizes output" do + WebMock.stub(:get, "http://example.org/").to_return(body: "Hello", headers: {"Hello" => "World"}) + + lines = capture_lines(uncolorize?: false) do |output| + Crul::CLI.run!(["http://example.org"], output).should be_true + end + + lines.first.should eq("\e[94mHTTP/1.1\e[0m\e[36m 200 \e[0m\e[33mOK") + lines[2].should eq("\e[0mHello: \e[36mWorld") + lines.last.should eq("Hello") + end + it "most basic GET with https" do WebMock.stub(:get, "https://example.org/").to_return(body: "Hello") diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 996d89b..d73f4c6 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -18,10 +18,12 @@ abstract class Crul::Formatters::Base end end -def capture_lines(&block) +def capture_lines(uncolorize? = true, &block) output = IO::Memory.new yield(output) - output.to_s.strip.split("\n") + string = output.to_s + string = uncolorize(string) if uncolorize? + string.strip.split("\n") end Spec.before_each do diff --git a/src/command.cr b/src/command.cr index babef19..f1926d8 100644 --- a/src/command.cr +++ b/src/command.cr @@ -42,13 +42,13 @@ module Crul end private def print_response(response) - with_color.light_blue.surround { @output << response.version } - with_color.cyan.surround { @output << " #{response.status_code} " } - with_color.yellow.surround { @output.puts response.status_message } + with_color.light_blue.surround(@output) { |io| io << response.version } + with_color.cyan.surround(@output) { |io| io << " #{response.status_code} " } + with_color.yellow.surround(@output) { |io| io.puts response.status_message } response.headers.each do |name, values| values.each do |value| @output << "#{name}: " - with_color.cyan.surround { @output.puts value } + with_color.cyan.surround(@output) { |io| io.puts value } end end @output.puts