lib/sup/util.rb in sup-0.22.1 vs lib/sup/util.rb in sup-0.23
- old
+ new
@@ -6,10 +6,11 @@
require 'pathname'
require 'set'
require 'enumerator'
require 'benchmark'
require 'unicode'
+require 'unicode/display_width'
require 'fileutils'
class Lockfile
def gen_lock_id
Hash[
@@ -237,25 +238,18 @@
end
end
class String
def display_length
- @display_length ||= Unicode.width(self.fix_encoding!, false)
-
- # if Unicode.width fails and returns -1, fall back to
- # regular String#length, see pull-request: #256.
- if @display_length < 0
- @display_length = self.length
- end
-
- @display_length
+ @display_length ||= Unicode::DisplayWidth.of(self)
end
def slice_by_display_length len
each_char.each_with_object "" do |c, buffer|
- len -= c.display_length
- buffer << c if len >= 0
+ len -= Unicode::DisplayWidth.of(c)
+ return buffer if len < 0
+ buffer << c
end
end
def camel_to_hyphy
self.gsub(/([a-z])([A-Z0-9])/, '\1-\2').downcase
@@ -339,17 +333,18 @@
def wrap len
ret = []
s = self
while s.display_length > len
- cut = s.slice_by_display_length(len).rindex(/\s/)
+ slice = s.slice_by_display_length(len)
+ cut = slice.rindex(/\s/)
if cut
ret << s[0 ... cut]
s = s[(cut + 1) .. -1]
else
- ret << s.slice_by_display_length(len)
- s = s[ret.last.length .. -1]
+ ret << slice
+ s = s[slice.length .. -1]
end
end
ret << s
end
@@ -458,21 +453,21 @@
def in? range; range.member? self; end
def to_human_size
if self < 1024
- to_s + "b"
+ to_s + "B"
elsif self < (1024 * 1024)
- (self / 1024).to_s + "k"
+ (self / 1024).to_s + "KiB"
elsif self < (1024 * 1024 * 1024)
- (self / 1024 / 1024).to_s + "m"
+ (self / 1024 / 1024).to_s + "MiB"
else
- (self / 1024 / 1024 / 1024).to_s + "g"
+ (self / 1024 / 1024 / 1024).to_s + "GiB"
end
end
end
-class Fixnum
+class Integer
def to_character
if self < 128 && self >= 0
chr
else
"<#{self}>"