lib/dev/ui/ansi.rb in dev-ui-0.0.2 vs lib/dev/ui/ansi.rb in dev-ui-0.1.0

- old
+ new

@@ -5,12 +5,28 @@ module ANSI ESC = "\x1b" # ANSI escape sequences (like \x1b[31m) have zero width. # when calculating the padding width, we must exclude them. + # This also implements a *reaaaallly* shitty version of utf8 character + # width calculation like we could get for real from something like + # utf8proc. def self.printing_width(str) - strip_codes(str).size + zwj = false + strip_codes(str).codepoints.reduce(0) do |acc, cp| + if zwj + zwj = false + next acc + end + case cp + when 0x200d # zero-width joiner + zwj = true + acc + else + acc + 1 + end + end end def self.strip_codes(str) str.gsub(/\x1b\[[\d;]+[A-z]|\r/, '') end @@ -42,9 +58,13 @@ end def self.cursor_back(n = 1) return '' if n.zero? control(n.to_s, 'D') + end + + def self.cursor_horizontal_absolute(n = 1) + control(n.to_s, 'G') end # Cursor Visibility def self.show_cursor