class String
Public Class Methods
color_output?()
click to toggle source
# File lib/string_mixins.rb, line 4 def self.color_output? $stdout.isatty unless ENV['SLIGHTISH_NO_COLOR'] end
Public Instance Methods
expand(chdir: nil, source: nil)
click to toggle source
# File lib/string_mixins.rb, line 38 def expand(chdir: nil, source: nil) # Non-existent environmental variables are not replaced. # A little unexpected, but it's the behavior of tush. # TODO: print a warning when this happens? variable_replacer = ->(match) { ENV.fetch(Regexp.last_match(:var_name), match) } res = gsub(/\$(?<var_name>[[:alnum:]_]+)/, &variable_replacer) # $VARIABLE res.gsub!(/\$\{(?<var_name>[[:alnum:]_]+)\}/, &variable_replacer) # ${VARIABLE} command_replacer = ->(_) { capture_stdout_with_logging(Regexp.last_match(:cmd), chdir, source) } res.gsub!(/\$\((?<cmd>[^\)]+)\)/, &command_replacer) # $(COMMAND) res.gsub!(/`(?<cmd>[^`]+)`/, &command_replacer) # `COMMAND` res end
Private Instance Methods
capture_stdout_with_logging(cmd, chdir, source)
click to toggle source
# File lib/string_mixins.rb, line 62 def capture_stdout_with_logging(cmd, chdir, source) if chdir.nil? stdout, stderr, status = Open3.capture3(cmd) else stdout, stderr, status = Open3.capture3(cmd, { chdir: chdir }) end puts_warning('stderr from command substitution will be ignored', cmd, source) unless stderr.empty? puts_warning("nonzero exit code (#{status.exitstatus}) from command substitution", cmd, source) unless status.exitstatus.zero? stdout.chomp end
puts_warning(message, cmd, source)
click to toggle source
# File lib/string_mixins.rb, line 55 def puts_warning(message, cmd, source) s = "warning: #{message} (" s += source + '; ' unless source.nil? || source.empty? s += "'#{cmd}')" $stderr.puts(s.yellow) unless ENV['SLIGHTISH_NO_WARNINGS'] end