lib/cli/ui/truncater.rb in cli-ui-1.5.1 vs lib/cli/ui/truncater.rb in cli-ui-2.0.0
- old
+ new
@@ -1,5 +1,6 @@
+# typed: true
# frozen_string_literal: true
require 'cli/ui'
module CLI
@@ -25,16 +26,19 @@
UC_ALPHA_RANGE = 0x60..0x71
TRUNCATED = "\x1b[0m…"
class << self
+ extend T::Sig
+
+ sig { params(text: String, printing_width: Integer).returns(String) }
def call(text, printing_width)
return text if text.size <= printing_width
width = 0
mode = PARSE_ROOT
- truncation_index = nil
+ truncation_index = T.let(nil, T.nilable(Integer))
codepoints = text.codepoints
codepoints.each.with_index do |cp, index|
case mode
when PARSE_ROOT
@@ -81,14 +85,15 @@
# It's specifically for the case where we decided "Yes, this is the
# point at which we'd have to add a truncation!" but it's actually
# the end of the string.
return text if !truncation_index || width <= printing_width
- codepoints[0...truncation_index].pack('U*') + TRUNCATED
+ T.must(codepoints[0...truncation_index]).pack('U*') + TRUNCATED
end
private
+ sig { params(printable_codepoint: Integer).returns(Integer) }
def width(printable_codepoint)
case printable_codepoint
when EMOJI_RANGE
2
else