Class: TTY::Command::Truncator Private
- Inherits:
-
Object
- Object
- TTY::Command::Truncator
- Defined in:
- lib/tty/command/truncator.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.
Retains the first N bytes and the last N bytes from written content
Constant Summary collapse
- DEFAULT_SIZE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default maximum byte size for prefix & suffix
32 << 10
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Truncator
constructor
Create a Truncator.
-
#read ⇒ String
(also: #to_s)
Truncated representation of the content.
-
#write(content) ⇒ nil
(also: #<<)
Write content.
Constructor Details
#initialize(options = {}) ⇒ Truncator
Create a Truncator
18 19 20 21 22 23 |
# File 'lib/tty/command/truncator.rb', line 18 def initialize( = {}) @max_size = .fetch(:max_size) { DEFAULT_SIZE } @prefix = "" @suffix = "" @skipped = 0 end |
Instance Method Details
#read ⇒ String Also known as: to_s
Truncated representation of the content
57 58 59 60 61 62 63 64 65 |
# File 'lib/tty/command/truncator.rb', line 57 def read return @prefix if @suffix.empty? if @skipped.zero? return @prefix << @suffix end @prefix + "\n... omitting #{@skipped} bytes ...\n" + @suffix end |
#write(content) ⇒ nil Also known as: <<
Write content
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tty/command/truncator.rb', line 33 def write(content) content = content.to_s.dup content, @prefix = append(content, @prefix) if (over = (content.bytesize - @max_size)) > 0 content = content.byteslice(over..-1) @skipped += over end content, @suffix = append(content, @suffix) # suffix is full but we still have content to write while content.bytesize > 0 content = copy(content, @suffix) end end |