Class: Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/renamr/utils.rb

Overview

Provides utility methods.

Class Method Summary collapse

Class Method Details

.trim(src, lim) ⇒ Object

Trims src to fit lim terminal columns. Wide characters such as CJK occupy two columns, so the string keeps shrinking until its display width fits the limit.



16
17
18
19
20
21
22
# File 'lib/renamr/utils.rb', line 16

def trim(src, lim)
  out = src.ellipsized(lim, '~', :center)
  while out.length > 1 && Unicode::DisplayWidth.of(out) > lim
    out = out.ellipsized(out.length - 1, '~', :center)
  end
  out
end