lib/rouge/cli.rb in rouge-3.15.0 vs lib/rouge/cli.rb in rouge-3.16.0

- old
+ new

@@ -2,10 +2,12 @@ # frozen_string_literal: true # not required by the main lib. # to use this module, require 'rouge/cli'. +require 'rbconfig' + module Rouge class FileReader attr_reader :input def initialize(input) @input = input @@ -198,13 +200,26 @@ yield %[] yield %[--escape-with <l> <r> allow the use of escapes between custom] yield %[ delimiters. implies --escape] end + # There is no consistent way to do this, but this is used elsewhere, + # and we provide explicit opt-in and opt-out with $COLORTERM + def self.supports_truecolor? + return true if %w(24bit truecolor).include?(ENV['COLORTERM']) + return false if ENV['COLORTERM'] && ENV['COLORTERM'] =~ /256/ + + if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ + ENV['ConEmuANSI'] == 'ON' && !ENV['ANSICON'] + else + ENV['TERM'] !~ /(^rxvt)|(-color$)/ + end + end + def self.parse(argv) opts = { - :formatter => 'terminal256', + :formatter => supports_truecolor? ? 'terminal-truecolor' : 'terminal256', :theme => 'thankful_eyes', :css_class => 'codehilite', :input_file => '-', :lexer_opts => {}, :formatter_opts => {}, @@ -297,11 +312,13 @@ @lexer_opts = opts[:lexer_opts] theme = Theme.find(opts[:theme]).new or error! "unknown theme #{opts[:theme]}" + # TODO: document this in --help @formatter = case opts[:formatter] when 'terminal256' then Formatters::Terminal256.new(theme) + when 'terminal-truecolor' then Formatters::TerminalTruecolor.new(theme) when 'html' then Formatters::HTML.new when 'html-pygments' then Formatters::HTMLPygments.new(Formatters::HTML.new, opts[:css_class]) when 'html-inline' then Formatters::HTMLInline.new(theme) when 'html-line-table' then Formatters::HTMLLineTable.new(Formatters::HTML.new) when 'html-table' then Formatters::HTMLTable.new(Formatters::HTML.new)