Class: LLM::Repl::Node Private

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/repl/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The LLM::Repl::Node class wraps a piece of text and optional curses attributes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, attrs = nil) ⇒ LLM::Repl::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • text (String)
  • attrs (Integer, nil) (defaults to: nil)


45
46
47
48
# File 'lib/llm/repl/node.rb', line 45

def initialize(text, attrs = nil)
  @text = text.to_s
  @attrs = attrs
end

Instance Attribute Details

#attrsInteger? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer, nil)


39
40
41
# File 'lib/llm/repl/node.rb', line 39

def attrs
  @attrs
end

#textString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


35
36
37
# File 'lib/llm/repl/node.rb', line 35

def text
  @text
end

Class Method Details

.slice(text, width) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Slices the given text so the returned piece has a display width of at most width columns.

Parameters:

  • text (#to_s)
  • width (Integer)

Returns:

  • (String)


24
25
26
27
28
29
30
31
# File 'lib/llm/repl/node.rb', line 24

def self.slice(text, width)
  out = +""
  text.to_s.each_char do |char|
    break if self.width(out + char) > width
    out << char
  end
  out
end

.width(text) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the display width of the given text, counting wide characters such as emoji as two columns.

Parameters:

  • text (#to_s)

Returns:

  • (Integer)


14
15
16
# File 'lib/llm/repl/node.rb', line 14

def self.width(text)
  Unicode::DisplayWidth.of(text.to_s)
end

Instance Method Details

#[](key) ⇒ String, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hash-like lookup.

Parameters:

  • key (Symbol)

Returns:

  • (String, Integer, nil)


62
63
64
65
66
67
# File 'lib/llm/repl/node.rb', line 62

def [](key)
  case key
  when :text then @text
  when :attrs then @attrs
  end
end

#sizeInteger Also known as: length

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the display width of the text

Returns:

  • (Integer)


53
54
55
# File 'lib/llm/repl/node.rb', line 53

def size
  self.class.width(@text)
end