Module: Act::Helper

Defined in:
lib/act/helper.rb

Class Method Summary collapse

Class Method Details

.add_line_numbers(string, start_line, highlight_line = nil) ⇒ String

Returns:

  • (String)


53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Fixnum

Returns:

  • (Fixnum)


30
31
32
# 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) ⇒ Object



67
68
69
70
71
72
73
# 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) ⇒ String

Returns:

  • (String)


10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ String, Nil

Returns:

  • (String, Nil)


36
37
38
39
40
41
42
43
# 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) ⇒ Fixnum

Returns:

  • (Fixnum)


24
25
26
# 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) ⇒ String

Returns:

  • (String)


47
48
49
# File 'lib/act/helper.rb', line 47

def self.strip_indentation(string)
  string.strip_heredoc
end

.syntax_highlight(string, lexer) ⇒ String

Returns:

  • (String)


89
90
91
92
# 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