class Rundoc::CodeCommand::Bash

Public Class Methods

new(line) click to toggle source

line = “cd ..”“ line = ”pwd“ line = ”ls“

# File lib/rundoc/code_command/bash.rb, line 6
def initialize(line)
  @line     = line
  @contents = ""
  @delegate = case @line.split(' ').first.downcase
  when 'cd'
    Cd.new(@line)
  else
    false
  end
end

Public Instance Methods

call(env = {}) click to toggle source
# File lib/rundoc/code_command/bash.rb, line 23
def call(env = {})
  return @delegate.call(env) if @delegate

  shell(@line, @contents)
end
sanitize_escape_chars(input) click to toggle source

markdown doesn't understand bash color codes

# File lib/rundoc/code_command/bash.rb, line 30
def sanitize_escape_chars(input)
  input.gsub(/\e\[(\d+)m/, '')
end
shell(cmd, stdin = nil) click to toggle source
# File lib/rundoc/code_command/bash.rb, line 34
def shell(cmd, stdin = nil)
  cmd = "(#{cmd}) 2>&1"
  msg  = "Running: $ '#{cmd}'"
  msg  << " with stdin: '#{stdin.inspect}'" if stdin && !stdin.empty?
  puts msg

  result = ""
  IO.popen(cmd, "w+") do |io|
    io << stdin if stdin
    io.close_write
    result = sanitize_escape_chars io.read
  end
  unless $?.success?
    raise "Command `#{@line}` exited with non zero status: #{result}" unless keyword.to_s.include?("fail")
  end
  return result
end
to_md(env = {}) click to toggle source
# File lib/rundoc/code_command/bash.rb, line 17
def to_md(env = {})
  return @delegate.to_md(env) if @delegate

  "$ #{@line}"
end