Class: TTY::Pager::Abstract
- Inherits:
-
Object
- Object
- TTY::Pager::Abstract
- Defined in:
- lib/tty/pager/abstract.rb
Direct Known Subclasses
Constant Summary collapse
- UndefinedMethodError =
Class.new(StandardError)
Class Method Summary collapse
-
.page(text = nil, path: nil, **options, &block) ⇒ Object
Paginate content through null, basic or system pager.
Instance Method Summary collapse
- #close ⇒ Object
-
#enabled? ⇒ Boolean
Check if pager is enabled.
-
#initialize(input: $stdin, output: $stdout, enabled: true, **_options) ⇒ Abstract
constructor
Create a pager.
-
#page(text = nil, path: nil) ⇒ Object
Page text.
- #puts ⇒ Object
-
#try_write(*args) ⇒ Boolean
Try writing to the pager.
- #write ⇒ Object
Constructor Details
#initialize(input: $stdin, output: $stdout, enabled: true, **_options) ⇒ Abstract
Create a pager
61 62 63 64 65 |
# File 'lib/tty/pager/abstract.rb', line 61 def initialize(input: $stdin, output: $stdout, enabled: true, **) @input = input @output = output @enabled = enabled end |
Class Method Details
.page(text = nil, path: nil, **options, &block) ⇒ Object
Paginate content through null, basic or system pager.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tty/pager/abstract.rb', line 16 def self.page(text = nil, path: nil, **, &block) validate_arguments(text, path, block) instance = new(**) begin if block_given? block.call(instance) else instance.page(text, path: path) end rescue PagerClosed # do nothing ensure instance.close end end |
Instance Method Details
#close ⇒ Object
127 128 129 |
# File 'lib/tty/pager/abstract.rb', line 127 def close(*) raise UndefinedMethodError end |
#enabled? ⇒ Boolean
Check if pager is enabled
72 73 74 |
# File 'lib/tty/pager/abstract.rb', line 72 def enabled? !!@enabled end |
#page(text = nil, path: nil) ⇒ Object
Page text
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tty/pager/abstract.rb', line 85 def page(text = nil, path: nil) if path IO.foreach(path) do |line| write(line) end else write(text) end rescue PagerClosed # do nothing ensure close end |
#puts ⇒ Object
123 124 125 |
# File 'lib/tty/pager/abstract.rb', line 123 def puts(*) raise UndefinedMethodError end |
#try_write(*args) ⇒ Boolean
Try writing to the pager. Return false if the pager was closed.
In case of system pager, send text to the pager process. Start a new process if it hasn’t been started yet.
112 113 114 115 116 117 |
# File 'lib/tty/pager/abstract.rb', line 112 def try_write(*args) write(*args) true rescue PagerClosed false end |
#write ⇒ Object
119 120 121 |
# File 'lib/tty/pager/abstract.rb', line 119 def write(*) raise UndefinedMethodError end |