Module: Kramdown::ANSI::Width

Extended by:
Term::ANSIColor
Includes:
Term::ANSIColor
Included in:
Kramdown::ANSI
Defined in:
lib/kramdown/ansi/width.rb

Class Method Summary collapse

Class Method Details

.truncate(text, percentage: nil, length: nil, ellipsis: ?…) ⇒ String

Truncates a given string to a specified length or percentage. If the text is longer an ellipsis sequence is added at the end of the generated string, to indicate that a truncation has been performed.

Parameters:

  • text (String)

    the input string to truncate

  • percentage (Hash) (defaults to: nil)

    a customizable set of options

  • length (Hash) (defaults to: nil)

    a customizable set of options

  • ellipsis (Hash) (defaults to: ?…)

    a customizable set of options

Options Hash (percentage:):

  • the (Numeric)

    percentage value for the width (defaults to nil)

Options Hash (length:):

  • the (Integer)

    character length for the width (defaults to nil)

Options Hash (ellipsis:):

  • the (Character)

    truncation indicator (defaults to ?…)

Returns:

  • (String)

    the truncated string

Raises:

  • (ArgumentError)

    if neither ‘percentage` nor `length` is provided



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kramdown/ansi/width.rb', line 48

def truncate(text, percentage: nil, length: nil, ellipsis: ?…)
  percentage.nil? ^ length.nil? or
    raise ArgumentError, "either pass percentage or length argument"
  percentage and length ||= width(percentage:)
  ellipsis_length = ellipsis.size
  if length < ellipsis_length
    +''
  elsif text.size >= length + ellipsis_length
    text[0, length - ellipsis_length] + ellipsis
  else
    text
  end
end

.width(percentage: 100.0) ⇒ Integer

Returns the width of the terminal in characters, given a percentage.

Parameters:

  • percentage (Numeric) (defaults to: 100.0)

    the percentage value (defaults to 100.0)

Returns:

  • (Integer)

    the calculated width



14
15
16
# File 'lib/kramdown/ansi/width.rb', line 14

def width(percentage: 100.0)
  ((Float(percentage) * Tins::Terminal.columns) / 100).floor
end

.wrap(text, percentage: nil, length: nil) ⇒ String

Wraps text to a specified width.

Parameters:

  • text (String)

    the text to wrap

  • percentage (Hash) (defaults to: nil)

    a customizable set of options

  • length (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (percentage:):

  • the (Numeric)

    percentage value for the width (defaults to nil)

Options Hash (length:):

  • the (Integer)

    character length for the width (defaults to nil)

Returns:

  • (String)

    the wrapped text

Raises:

  • (ArgumentError)

    if neither ‘percentage` nor `length` is provided



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kramdown/ansi/width.rb', line 25

def wrap(text, percentage: nil, length: nil)
  percentage.nil? ^ length.nil? or
    raise ArgumentError, "either pass percentage or length argument"
  percentage and length ||= width(percentage:)
  text.gsub(/(?<!\n)\n(?!\n)/, ' ').lines.map do |line|
    if length >= 1 && uncolor { line }.length > length
      line.gsub(/(.{1,#{length}})(\s+|$)/, "\\1\n").strip
    else
      line.strip
    end
  end * ?\n
end