Module: Ask::Tools::Shell

Defined in:
lib/ask/tools/shell.rb,
lib/ask/tools/shell/edit.rb,
lib/ask/tools/shell/version.rb,
lib/ask/tools/shell/file_mutation_queue.rb

Defined Under Namespace

Modules: EditOperations Classes: DefaultEditOperations, FileMutationQueue

Constant Summary collapse

TOOLS =
[Bash, Read, Write, Edit, Glob, Grep, Code].freeze
VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.detect_line_ending(content) ⇒ Object

Detect the dominant line ending in content.



44
45
46
47
48
# File 'lib/ask/tools/shell/edit.rb', line 44

def self.detect_line_ending(content)
  crlf = content.count("\r\n")
  lf = content.count("\n") - crlf
  crlf > lf ? "\r\n" : "\n"
end

.normalize_line_endings(content) ⇒ Object

Normalize line endings to LF.



51
52
53
# File 'lib/ask/tools/shell/edit.rb', line 51

def self.normalize_line_endings(content)
  content.gsub("\r\n", "\n").gsub("\r", "\n")
end

.restore_line_endings(content, original_ending) ⇒ Object

Restore original line endings after normalization.



56
57
58
59
# File 'lib/ask/tools/shell/edit.rb', line 56

def self.restore_line_endings(content, original_ending)
  return content if original_ending == "\n"
  content.gsub("\n", original_ending)
end

.strip_bom(content) ⇒ Object

Strip UTF-8 BOM from content. Returns { bom: String, text: String }.



38
39
40
41
# File 'lib/ask/tools/shell/edit.rb', line 38

def self.strip_bom(content)
  bom = content.byteslice(0, 3) == "\xEF\xBB\xBF" ? content.byteslice(0, 3) : ""
  [bom, bom.empty? ? content : content.byteslice(3..)]
end