summaryrefslogtreecommitdiff
path: root/lib/highline/wrapper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/highline/wrapper.rb')
-rw-r--r--lib/highline/wrapper.rb23
1 files changed, 12 insertions, 11 deletions
diff --git a/lib/highline/wrapper.rb b/lib/highline/wrapper.rb
index ae93db6..a2ab4ac 100644
--- a/lib/highline/wrapper.rb
+++ b/lib/highline/wrapper.rb
@@ -1,12 +1,12 @@
# coding: utf-8
-class HighLine
+require "English"
+class HighLine
# A simple Wrapper module that is aware of ANSI escape codes.
# It compensates for the ANSI escape codes so it works on the
# actual (visual) line length.
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
@@ -18,24 +18,25 @@ class HighLine
return text unless wrap_at
wrap_at = Integer(wrap_at)
- wrapped = [ ]
+ wrapped = []
text.each_line do |line|
# take into account color escape sequences when wrapping
- wrap_at = wrap_at + (line.length - actual_length(line))
+ wrap_at += (line.length - actual_length(line))
while line =~ /([^\n]{#{wrap_at + 1},})/
- search = $1.dup
- replace = $1.dup
- if index = replace.rindex(" ", wrap_at)
+ search = Regexp.last_match(1).dup
+ replace = Regexp.last_match(1).dup
+ index = replace.rindex(" ", wrap_at)
+ if index
replace[index, 1] = "\n"
replace.sub!(/\n[ \t]+/, "\n")
line.sub!(search, replace)
else
- line[$~.begin(1) + wrap_at, 0] = "\n"
+ line[$LAST_MATCH_INFO.begin(1) + wrap_at, 0] = "\n"
end
end
wrapped << line
end
- return wrapped.join
+ wrapped.join
end
#
@@ -45,8 +46,8 @@ class HighLine
# @param string_with_escapes [String] any ANSI colored String
# @return [Integer] length based on the visual size of the String
# (without the escape codes)
- def self.actual_length( string_with_escapes )
+ def self.actual_length(string_with_escapes)
string_with_escapes.to_s.gsub(/\e\[\d{1,2}m/, "").length
end
end
-end \ No newline at end of file
+end