Class: String
- Defined in:
- lib/mysh/pre_processor.rb,
lib/mysh/handlebars/string.rb,
lib/mysh/internal/to_file_spec.rb,
lib/mysh/internal/format/string.rb,
lib/mysh/shell_variables/evaluate.rb
Overview
Monkey patches for Mysh variables
Instance Method Summary collapse
-
#do_format_description(input, max_width) ⇒ Object
Do the formatting legwork.
-
#dress_up_quotes ⇒ Object
Dress up in quotes if needed.
-
#dress_up_slashes ⇒ Object
Dress up slashes and backslashes.
-
#eval_handlebars(evaluator = $mysh_exec_host) ⇒ Object
Evaluate any variable substitutions in the input.
-
#eval_quoted_braces ⇒ Object
Process quoted brace characters.
-
#eval_variables ⇒ Object
Evaluate any variable substitutions in the input.
-
#extract_mysh_types ⇒ Object
Extract common mysh data from this string.
-
#format_description(max_width) ⇒ Object
Create a bullet point description from this string.
-
#preprocess(evaluator = $mysh_exec_host) ⇒ Object
The mysh string pre-processor stack.
-
#split_if_huge(max_width, buffer) ⇒ Object
Split up a overlong blob of text.
-
#split_if_over(word, max_width, buffer) ⇒ Object
Split if adding a word goes over a little.
-
#to_host_spec ⇒ Object
Make the file name fit the local system.
-
#to_std_spec ⇒ Object
Make the file name fit the standard notation.
Instance Method Details
#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_quotes ⇒ Object
Dress up in quotes if needed.
18 19 20 |
# File 'lib/mysh/internal/to_file_spec.rb', line 18 def dress_up_quotes self[' '] ? "\"#{self}\"" : self end |
#dress_up_slashes ⇒ Object
Dress up slashes and backslashes.
13 14 15 |
# File 'lib/mysh/internal/to_file_spec.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 (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_braces ⇒ Object
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_variables ⇒ Object
Evaluate any variable substitutions in the input.
16 17 18 19 20 21 22 |
# 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.gsub(/\\\$/, "$") end |
#extract_mysh_types ⇒ Object
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.(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 |
#to_host_spec ⇒ Object
Make the file name fit the local system.
7 8 9 10 |
# File 'lib/mysh/internal/to_file_spec.rb', line 7 def to_host_spec self.dress_up_slashes .dress_up_quotes end |
#to_std_spec ⇒ Object
Make the file name fit the standard notation.
23 24 25 |
# File 'lib/mysh/internal/to_file_spec.rb', line 23 def to_std_spec self.gsub("\\", "/") end |