Sha256: 5e4188a055331bfcff8b05dd3c44f8c6df85139ae63ca0bc27e5d4e8b7cdb0d4

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

module Aocli
  module FileUtils
    module_function

    def wrap_lines(content, wrap_at: 80)
      content
        .split("\n")
        .map { wrap_line(_1, wrap_at: wrap_at) }
        .flatten
        .join("\n")
    end

    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

    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

    def replace_line(lines, line_to_replace, replace_with)
      lines.split("\n").map { _1 == line_to_replace ? replace_with : _1 }.join("\n")
    end

    def touch_file(file_path)
      ::FileUtils.mkdir_p(File.join(file_path.split("/")[0..-2]))
      ::FileUtils.touch(file_path)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
aocli-1.5.0 lib/aocli/file_utils.rb
aocli-1.4.5 lib/aocli/file_utils.rb
aocli-1.4.4 lib/aocli/file_utils.rb
aocli-1.4.3 lib/aocli/file_utils.rb
aocli-1.4.2 lib/aocli/file_utils.rb
aocli-1.4.1 lib/aocli/file_utils.rb
aocli-1.3.0 lib/aocli/file_utils.rb