diff options
author | Abinoam Praxedes Marques Jr <abinoam@gmail.com> | 2015-03-13 17:15:10 -0300 |
---|---|---|
committer | Abinoam Praxedes Marques Jr <abinoam@gmail.com> | 2015-04-29 23:43:25 -0300 |
commit | 11b9800191e98af0be21c1867e35a773b6354e74 (patch) | |
tree | 5ff40cd98eb21f06ed43e6634061cb0088bebe52 /lib/highline/wrapper.rb | |
parent | 7df050b1c2225005170650cf3255e617099a8d14 (diff) | |
download | highline-11b9800191e98af0be21c1867e35a773b6354e74.tar.gz |
Move HighLine::Statement wrapping logic into its own module
Diffstat (limited to 'lib/highline/wrapper.rb')
-rw-r--r-- | lib/highline/wrapper.rb | 30 |
1 files changed, 30 insertions, 0 deletions
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 |