Module: Aocli::FileUtils
- Defined in:
- lib/aocli/file_utils.rb
Class Method Summary collapse
- .insert_lines(lines, into:, after:) ⇒ Object
- .replace_line(lines, line_to_replace, replace_with) ⇒ Object
- .touch_file(file_path) ⇒ Object
- .wrap_line(line, wrap_at:) ⇒ Object
- .wrap_lines(content, wrap_at: 80) ⇒ Object
Class Method Details
.insert_lines(lines, into:, after:) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/aocli/file_utils.rb', line 34 def insert_lines(lines, into:, after:) into_array = into.split("\n") insert_index = into_array.index(after) raise(StandardError, "Cannot find after #{after.inspect}") if insert_index.nil? lines.reverse.each do |line| into_array.insert(insert_index + 1, line) end into_array.join("\n") end |
.replace_line(lines, line_to_replace, replace_with) ⇒ Object
45 46 47 |
# File 'lib/aocli/file_utils.rb', line 45 def replace_line(lines, line_to_replace, replace_with) lines.split("\n").map { _1 == line_to_replace ? replace_with : _1 }.join("\n") end |
.touch_file(file_path) ⇒ Object
49 50 51 52 |
# File 'lib/aocli/file_utils.rb', line 49 def touch_file(file_path) ::FileUtils.mkdir_p(File.join(file_path.split("/")[0..-2])) ::FileUtils.touch(file_path) end |
.wrap_line(line, wrap_at:) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aocli/file_utils.rb', line 13 def wrap_line(line, wrap_at:) return line unless line.length > wrap_at char_count = 0 results = [] current_line = "" line.split(" ").each do |word| if (current_line.length + word.length + 1) > wrap_at results << current_line.strip current_line = word char_count = word.length else current_line += " #{word}" char_count += word.length + 1 end end results << current_line.strip unless current_line == "" results end |
.wrap_lines(content, wrap_at: 80) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/aocli/file_utils.rb', line 5 def wrap_lines(content, wrap_at: 80) content .split("\n") .map { wrap_line(_1, wrap_at: wrap_at) } .flatten .join("\n") end |