diff --git a/CHANGELOG.md b/CHANGELOG.md index 82d5ba3..f24aeb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## HEAD (unreleased) +- Don't print terminal color codes when output is not tty (https://github.com/zombocom/dead_end/pull/91) + ## 2.0.1 - Reintroduce Ruby 2.5 support (https://github.com/zombocom/dead_end/pull/90) diff --git a/exe/dead_end b/exe/dead_end index 053c180..2ea5070 100755 --- a/exe/dead_end +++ b/exe/dead_end @@ -5,7 +5,6 @@ require "optparse" require_relative "../lib/dead_end" options = {} -options[:terminal] = true options[:record_dir] = ENV["DEAD_END_RECORD_DIR"] parser = OptionParser.new do |opts| @@ -46,6 +45,10 @@ parser = OptionParser.new do |opts| options[:record_dir] = v end + opts.on("--terminal", "Enable terminal highlighting") do |v| + options[:terminal] = true + end + opts.on("--no-terminal", "Disable terminal highlighting") do |v| options[:terminal] = false end diff --git a/lib/dead_end/display_invalid_blocks.rb b/lib/dead_end/display_invalid_blocks.rb index 9d22c72..f659473 100644 --- a/lib/dead_end/display_invalid_blocks.rb +++ b/lib/dead_end/display_invalid_blocks.rb @@ -9,8 +9,8 @@ module DeadEnd class DisplayInvalidBlocks attr_reader :filename - def initialize(code_lines:, blocks:, io: $stderr, filename: nil, terminal: false, invalid_obj: WhoDisSyntaxError::Null.new) - @terminal = terminal + def initialize(code_lines:, blocks:, io: $stderr, filename: nil, terminal: nil, invalid_obj: WhoDisSyntaxError::Null.new) + @terminal = terminal.nil? ? io.isatty : terminal @filename = filename @io = io diff --git a/lib/dead_end/internals.rb b/lib/dead_end/internals.rb index bd87c64..26008f9 100644 --- a/lib/dead_end/internals.rb +++ b/lib/dead_end/internals.rb @@ -28,14 +28,13 @@ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT) call( source: Pathname(filename).read, filename: filename, - terminal: true ) end raise e end - def self.call(source:, filename:, terminal: false, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr) + def self.call(source:, filename:, terminal: nil, record_dir: nil, timeout: TIMEOUT_DEFAULT, io: $stderr) search = nil Timeout.timeout(timeout) do record_dir ||= ENV["DEBUG"] ? "tmp" : nil diff --git a/spec/integration/exe_cli_spec.rb b/spec/integration/exe_cli_spec.rb index c1742c0..b3b606b 100644 --- a/spec/integration/exe_cli_spec.rb +++ b/spec/integration/exe_cli_spec.rb @@ -23,7 +23,7 @@ def exe(cmd) it "parses invalid code" do ruby_file = fixtures_dir.join("this_project_extra_def.rb.txt") - out = exe("#{ruby_file} --no-terminal") + out = exe(ruby_file) expect(out.strip).to include("Missing `end` detected") expect(out.strip).to include("❯ 36 def filename") @@ -32,13 +32,13 @@ def exe(cmd) end it "handles heredocs" do + lines = fixtures_dir.join("rexe.rb.txt").read.lines Tempfile.create do |file| - lines = fixtures_dir.join("rexe.rb.txt").read.lines lines.delete_at(85 - 1) Pathname(file.path).write(lines.join) - out = exe("#{file.path} --no-terminal") + out = exe(file.path) expect(out).to include(<<~EOM.indent(4)) 16 class Rexe @@ -50,6 +50,19 @@ def exe(cmd) end end + describe "terminal coloring" do + # When ruby sub shells it is not a interactive shell and dead_end will + # default to no coloring. + + it "passing --terminal will force color codes" do + ruby_file = fixtures_dir.join("this_project_extra_def.rb.txt") + out = exe("#{ruby_file} --terminal") + + expect(out.strip).to include("Missing `end` detected") + expect(out.strip).to include("\e[0m❯ 36 \e[1;3m def filename") + end + end + it "records search" do Dir.mktmpdir do |dir| dir = Pathname(dir)