Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_mixins.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color_output?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/string_mixins.rb', line 4

def self.color_output?
  $stdout.isatty unless ENV['SLIGHTISH_NO_COLOR']
end

Instance Method Details

#expand(chdir: nil, source: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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