summaryrefslogtreecommitdiff
path: root/lib/highline
diff options
context:
space:
mode:
authorAbinoam Praxedes Marques Jr <abinoam@gmail.com>2015-03-13 17:15:10 -0300
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2015-04-29 23:43:25 -0300
commit11b9800191e98af0be21c1867e35a773b6354e74 (patch)
tree5ff40cd98eb21f06ed43e6634061cb0088bebe52 /lib/highline
parent7df050b1c2225005170650cf3255e617099a8d14 (diff)
downloadhighline-11b9800191e98af0be21c1867e35a773b6354e74.tar.gz
Move HighLine::Statement wrapping logic into its own module
Diffstat (limited to 'lib/highline')
-rw-r--r--lib/highline/statement.rb33
-rw-r--r--lib/highline/wrapper.rb30
2 files changed, 37 insertions, 26 deletions
diff --git a/lib/highline/statement.rb b/lib/highline/statement.rb
index 529f7ca..205b625 100644
--- a/lib/highline/statement.rb
+++ b/lib/highline/statement.rb
@@ -37,34 +37,15 @@ class HighLine::Statement
end
def render_template
- template = ERB.new(template_string, nil, "%")
- highline.instance_eval { template.result(binding) }
+ # Assigning to a local var so it may be
+ # used inside instance eval block
+
+ template_var = template
+ highline.instance_eval { template_var.result(binding) }
end
- #
- # Wrap a sequence of _lines_ at _wrap_at_ characters per line. Existing
- # newlines will not be affected by this process, but additional newlines
- # may be added.
- #
- def wrap( text )
- wrapped = [ ]
- text.each_line do |line|
- # take into account color escape sequences when wrapping
- wrap_at = highline.wrap_at + (line.length - highline.send(:actual_length, line))
- while line =~ /([^\n]{#{wrap_at + 1},})/
- search = $1.dup
- replace = $1.dup
- if index = replace.rindex(" ", wrap_at)
- replace[index, 1] = "\n"
- replace.sub!(/\n[ \t]+/, "\n")
- line.sub!(search, replace)
- else
- line[$~.begin(1) + wrap_at, 0] = "\n"
- end
- end
- wrapped << line
- end
- return wrapped.join
+ def template
+ @template ||= ERB.new(template_string, nil, "%")
end
#
diff --git a/lib/highline/wrapper.rb b/lib/highline/wrapper.rb
new file mode 100644
index 0000000..5fc5d2f
--- /dev/null
+++ b/lib/highline/wrapper.rb
@@ -0,0 +1,30 @@
+class HighLine
+ module Wrapper
+
+ #
+ # Wrap a sequence of _lines_ at _wrap_at_ characters per line. Existing
+ # newlines will not be affected by this process, but additional newlines
+ # may be added.
+ #
+ def self.wrap(text, wrap_at)
+ wrapped = [ ]
+ text.each_line do |line|
+ # take into account color escape sequences when wrapping
+ wrap_at = highline.wrap_at + (line.length - highline.send(:actual_length, line))
+ while line =~ /([^\n]{#{wrap_at + 1},})/
+ search = $1.dup
+ replace = $1.dup
+ if index = replace.rindex(" ", wrap_at)
+ replace[index, 1] = "\n"
+ replace.sub!(/\n[ \t]+/, "\n")
+ line.sub!(search, replace)
+ else
+ line[$~.begin(1) + wrap_at, 0] = "\n"
+ end
+ end
+ wrapped << line
+ end
+ return wrapped.join
+ end
+ end
+end \ No newline at end of file