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
- .lexer(file_name) ⇒ Object
- .open_in_editor_command(path, line) ⇒ String
- .prettify(string, lexer) ⇒ Object
- .select_lines(string, start_line, end_line) ⇒ String, Nil
- .start_line(string, line, context_lines) ⇒ Fixnum
- .strip_indentation(string) ⇒ String
- .syntax_highlith(string, lexer) ⇒ 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 ||= 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
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 |
.lexer(file_name) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/act/helper.rb', line 66 def self.lexer(file_name) case file_name when 'Gemfile', 'Rakefile', 'Podfile' 'rb' when 'Podfile.lock', 'Gemfile.lock' 'yaml' else if file_name `pygmentize -N #{file_name}`.chomp else 'text' end end 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 |
.prettify(string, lexer) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/act/helper.rb', line 81 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
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, lexer) ⇒ String
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/act/helper.rb', line 95 def self.syntax_highlith(string, lexer) raise ArgumentError unless string raise ArgumentError unless lexer return string if `which pygmentize`.strip.empty? result = nil Open3.popen3("pygmentize -l #{lexer}") do |stdin, stdout, stderr| stdin.write(string) stdin.close_write result = stdout.read end result end |