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)


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/act/helper.rb', line 48

def self.add_line_numbers(string, start_line, highlight_line = nil)
  line_count = start_line
  numbered_lines = string.lines.map do |line|
    line_count += 1
    number = line_count.to_s.ljust(3)
    if highlight_line && highlight_line == line_count
      number = number.yellow
    end
    "#{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

.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

.select_lines(string, start_line, end_line) ⇒ String

Returns:

  • (String)


36
37
38
# File 'lib/act/helper.rb', line 36

def self.select_lines(string, start_line, end_line)
  string.lines[start_line..end_line].join
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)


42
43
44
# File 'lib/act/helper.rb', line 42

def self.strip_indentation(string)
  string.strip_heredoc
end

.syntax_highlith(string, file_name) ⇒ String

Returns:

  • (String)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/act/helper.rb', line 63

def self.syntax_highlith(string, file_name)
  return string if `which gen_bridge_metadata`.strip.empty?
  result = nil
  lexer = `pygmentize -N #{file_name}`.chomp
  Open3.popen3("pygmentize -l #{lexer}") do |stdin, stdout, stderr|
    stdin.write(string)
    stdin.close_write
    result = stdout.read
  end
  result
end