Class: String

Inherits:
Object show all
Defined in:
lib/mysh/pre_processor.rb,
lib/mysh/handlebars/string.rb,
lib/mysh/internal/decorate.rb,
lib/mysh/internal/format/string.rb,
lib/mysh/shell_variables/evaluate.rb

Overview

Monkey patches for Mysh variables

Instance Method Summary collapse

Instance Method Details

#decorateObject

Make the file name fit the local system.



7
8
9
10
# File 'lib/mysh/internal/decorate.rb', line 7

def decorate
  self.dress_up_slashes
      .dress_up_quotes
end

#do_format_description(input, max_width) ⇒ Object

Do the formatting legwork.
Returns

  • An array of strings.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mysh/internal/format/string.rb', line 16

def do_format_description(input, max_width)
  buffer, build, empty = [], "", false

  loop do  #Grab "words" of input, splitting off lines as needed.
    empty = (build = build.split_if_over(input.next, max_width, buffer)
                          .split_if_huge(max_width, buffer)).empty?
  end

  unless empty
    buffer << build
  end

  buffer
end

#dress_up_quotesObject

Dress up in quotes if needed.



18
19
20
# File 'lib/mysh/internal/decorate.rb', line 18

def dress_up_quotes
  self[' '] ? "\"#{self}\"" : self
end

#dress_up_slashesObject

Dress up slashes and backslashes.



13
14
15
# File 'lib/mysh/internal/decorate.rb', line 13

def dress_up_slashes
  MiniReadline::PLATFORM == :windows ? self.gsub("/", "\\") : self
end

#eval_handlebars(evaluator = $mysh_exec_host) ⇒ Object

Evaluate any variable substitutions in the input.



7
8
9
10
11
12
13
14
15
# File 'lib/mysh/handlebars/string.rb', line 7

def eval_handlebars(evaluator=$mysh_exec_host)
  gsub(/{{.*?}}/m) do |match|
    code   = match[2...-2]
    silent = code.end_with?("#")
    result = evaluator.mysh_eval(code)

    (result unless silent).to_s
  end
end

#eval_quoted_bracesObject

Process quoted brace characters



18
19
20
# File 'lib/mysh/handlebars/string.rb', line 18

def eval_quoted_braces
  gsub(/\\[\{\}]/) {|found| found[1]}
end

#eval_variablesObject

Evaluate any variable substitutions in the input.



16
17
18
19
20
21
# File 'lib/mysh/shell_variables/evaluate.rb', line 16

def eval_variables
  self.gsub(/(\$\$)|(\$[a-z][a-z0-9_]*)/) do |str|
    sym = str[1..-1].to_sym
    MNV.key?(sym) ? MNV[sym].to_s : str
  end
end

#extract_mysh_typesObject

Extract common mysh data from this string.



7
8
9
10
11
12
13
# File 'lib/mysh/shell_variables/evaluate.rb', line 7

def extract_mysh_types
  if self =~ /false|no|off/i
    false
  else
    self
  end
end

#format_description(max_width) ⇒ Object

Create a bullet point description from this string.
Returns

  • An array of strings.



9
10
11
# File 'lib/mysh/internal/format/string.rb', line 9

def format_description(max_width)
  do_format_description(split(' ').each, max_width)
end

#preprocess(evaluator = $mysh_exec_host) ⇒ Object

The mysh string pre-processor stack.



7
8
9
# File 'lib/mysh/pre_processor.rb', line 7

def preprocess(evaluator=$mysh_exec_host)
  self.eval_variables.eval_handlebars(evaluator).eval_quoted_braces
end

#split_if_huge(max_width, buffer) ⇒ Object

Split up a overlong blob of text.
Returns

  • A string.



50
51
52
53
54
55
56
57
58
# File 'lib/mysh/internal/format/string.rb', line 50

def split_if_huge(max_width, buffer)

  #Slice away any excess text into lines in the buffer.
  while length >= max_width
    buffer << slice!(0, max_width)
  end

  self
end

#split_if_over(word, max_width, buffer) ⇒ Object

Split if adding a word goes over a little.
Returns

  • A string.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mysh/internal/format/string.rb', line 34

def split_if_over(word, max_width, buffer)
  word.prepend(" ") unless empty? #Add a space except for the first word.

  word_len = word.length

  if (length + word_len) >= max_width && word_len < max_width
    buffer << self  #Line done, add to buffer.
    word.lstrip     #Start of a new line, remove the leading space.
  else
    self + word
  end
end