module Act::Helper

Public Class Methods

add_line_numbers(string, start_line, highlight_line = nil) click to toggle source

@return [String]

# File lib/act/helper.rb, line 53
def self.add_line_numbers(string, start_line, highlight_line = nil)
  start_line ||= 1
  line_count = start_line
  numbered_lines = string.lines.map do |line|
    number = line_count.to_s.ljust(3)
    if highlight_line && highlight_line == line_count
      number = number.yellow
    end
    line_count += 1
    "#{number}  #{line}"
  end
  numbered_lines.join
end
end_line(string, line, context_lines) click to toggle source

@return [Fixnum]

# File lib/act/helper.rb, line 30
def self.end_line(string, line, context_lines)
  end_line = line + context_lines - 1
end
lexer(file_name, string = nil) click to toggle source
# File lib/act/helper.rb, line 67
def self.lexer(file_name, string = nil)
  if string
    Rouge::Lexer.guess(:filename => file_name, :source => string).tag
  else
    Rouge::Lexer.guess_by_filename(file_name).tag
  end
end
open_in_editor_command(path, line) click to toggle source

@return [String]

# File lib/act/helper.rb, line 10
def self.open_in_editor_command(path, line)
  editor = ENV['EDITOR']
  result = "#{editor} #{path}"
  if line
    case editor
    when 'vim', 'mvim'
      result = "#{editor} #{path} +#{line}"
    end
  end
  result
end
prettify(string, lexer) click to toggle source
# File lib/act/helper.rb, line 75
def self.prettify(string, lexer)
  raise ArgumentError unless string
  raise ArgumentError unless lexer
  case lexer
  when 'json'
    require 'json'
    JSON.pretty_generate(JSON.parse(string))
  else
    string
  end
end
select_lines(string, start_line, end_line) click to toggle source

@return [String, Nil]

# File lib/act/helper.rb, line 36
def self.select_lines(string, start_line, end_line)
  start_line = start_line - 1
  end_line = end_line - 1
  start_line = 0 if start_line < 0
  end_line = 0 if end_line < 0
  components = string.lines[start_line..end_line]
  components.join if components && !components.empty?
end
start_line(string, line, context_lines) click to toggle source

@return [Fixnum]

# File lib/act/helper.rb, line 24
def self.start_line(string, line, context_lines)
  start_line = line - context_lines - 1
end
strip_indentation(string) click to toggle source

@return [String]

# File lib/act/helper.rb, line 47
def self.strip_indentation(string)
  string.strip_heredoc
end
syntax_highlight(string, lexer) click to toggle source

@return [String]

# File lib/act/helper.rb, line 89
def self.syntax_highlight(string, lexer)
  formatter = Rouge::Formatters::Terminal256.new(:theme => 'monokai')
  Rouge.highlight(string, lexer, formatter)
end