Module: Act::Helper
- Defined in:
- lib/act/helper.rb
Class Method Summary collapse
- .add_line_numbers(string, start_line, highlight_line = nil) ⇒ String
- .end_line(string, line, context_lines) ⇒ Fixnum
- .open_in_editor_command(path, line) ⇒ String
- .select_lines(string, start_line, end_line) ⇒ String, Nil
- .start_line(string, line, context_lines) ⇒ Fixnum
- .strip_indentation(string) ⇒ String
- .syntax_highlith(string, file_name) ⇒ String
Class Method Details
.add_line_numbers(string, start_line, highlight_line = nil) ⇒ String
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/act/helper.rb', line 52 def self.add_line_numbers(string, start_line, highlight_line = nil) start_line ||= 0 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
29 30 31 |
# File 'lib/act/helper.rb', line 29 def self.end_line(string, line, context_lines) end_line = line + context_lines - 1 end |
.open_in_editor_command(path, line) ⇒ String
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/act/helper.rb', line 9 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, Nil
35 36 37 38 39 40 41 42 |
# File 'lib/act/helper.rb', line 35 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
23 24 25 |
# File 'lib/act/helper.rb', line 23 def self.start_line(string, line, context_lines) start_line = line - context_lines - 1 end |
.strip_indentation(string) ⇒ String
46 47 48 |
# File 'lib/act/helper.rb', line 46 def self.strip_indentation(string) string.strip_heredoc end |
.syntax_highlith(string, file_name) ⇒ String
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/act/helper.rb', line 68 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 |