Class: Hirb::Pager
- Inherits:
-
Object
- Object
- Hirb::Pager
- Defined in:
- lib/hirb/pager.rb
Overview
This class provides class methods for paging and an object which can conditionally page given a terminal size that is exceeded.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.command_pager(output, options = {}) ⇒ Object
Pages using a configured or detected shell command.
-
.default_pager(output, options = {}) ⇒ Object
Pages with a ruby-only pager which either pages or quits.
-
.pager_command(*commands) ⇒ Object
:nodoc:.
-
.valid_pager_command?(cmd) ⇒ Boolean
:stopdoc:.
Instance Method Summary collapse
-
#activated_by?(string_to_page, inspect_mode = false) ⇒ Boolean
Determines if string should be paged based on configured width and height.
-
#char_count(string) ⇒ Object
:nodoc:.
-
#initialize(width, height, options = {}) ⇒ Pager
constructor
A new instance of Pager.
-
#page(string, inspect_mode) ⇒ Object
Pages given string using configured pager.
-
#resize(width, height) ⇒ Object
:nodoc:.
-
#slice!(output, inspect_mode = false) ⇒ Object
:nodoc:.
Constructor Details
#initialize(width, height, options = {}) ⇒ Pager
Returns a new instance of Pager.
59 60 61 62 |
# File 'lib/hirb/pager.rb', line 59 def initialize(width, height, ={}) resize(width, height) @pager_command = [:pager_command] if [:pager_command] end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
57 58 59 |
# File 'lib/hirb/pager.rb', line 57 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
57 58 59 |
# File 'lib/hirb/pager.rb', line 57 def width @width end |
Class Method Details
.command_pager(output, options = {}) ⇒ Object
Pages using a configured or detected shell command.
6 7 8 |
# File 'lib/hirb/pager.rb', line 6 def command_pager(output, ={}) basic_pager(output) if valid_pager_command?([:pager_command]) end |
.default_pager(output, options = {}) ⇒ Object
Pages with a ruby-only pager which either pages or quits.
20 21 22 23 24 25 26 27 28 |
# File 'lib/hirb/pager.rb', line 20 def default_pager(output, ={}) pager = new([:width], [:height]) while pager.activated_by?(output, [:inspect]) puts pager.slice!(output, [:inspect]) return unless continue_paging? end puts output puts "=== Pager finished. ===" end |
.pager_command(*commands) ⇒ Object
:nodoc:
10 11 12 13 14 15 16 17 |
# File 'lib/hirb/pager.rb', line 10 def pager_command(*commands) #:nodoc: @pager_command = (!@pager_command.nil? && commands.empty?) ? @pager_command : begin env_pager = ENV['PAGER'] ? File.basename(ENV['PAGER']) : nil commands = [env_pager, 'less', 'more', 'pager'] if commands.empty? commands.compact.uniq.find {|e| Util.command_exists?(e[/\w+/]) } end end |
.valid_pager_command?(cmd) ⇒ Boolean
:stopdoc:
31 32 33 |
# File 'lib/hirb/pager.rb', line 31 def valid_pager_command?(cmd) cmd ? pager_command(cmd) : pager_command end |
Instance Method Details
#activated_by?(string_to_page, inspect_mode = false) ⇒ Boolean
Determines if string should be paged based on configured width and height.
88 89 90 |
# File 'lib/hirb/pager.rb', line 88 def activated_by?(string_to_page, inspect_mode=false) inspect_mode ? (String.size(string_to_page) > @height * @width) : (string_to_page.count("\n") > @height) end |
#char_count(string) ⇒ Object
:nodoc:
93 94 95 |
# File 'lib/hirb/pager.rb', line 93 def char_count(string) #:nodoc: string.chars.count end |
#page(string, inspect_mode) ⇒ Object
Pages given string using configured pager.
65 66 67 68 69 70 71 |
# File 'lib/hirb/pager.rb', line 65 def page(string, inspect_mode) if self.class.valid_pager_command?(@pager_command) self.class.command_pager(string, :pager_command=>@pager_command) else self.class.default_pager(string, :width=>@width, :height=>@height, :inspect=>inspect_mode) end end |
#resize(width, height) ⇒ Object
:nodoc:
102 103 104 |
# File 'lib/hirb/pager.rb', line 102 def resize(width, height) #:nodoc: @width, @height = View.determine_terminal_size(width, height) end |
#slice!(output, inspect_mode = false) ⇒ Object
:nodoc:
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hirb/pager.rb', line 73 def slice!(output, inspect_mode=false) #:nodoc: effective_height = @height - 2 # takes into account pager prompt if inspect_mode sliced_output = String.slice(output, 0, @width * effective_height) output.replace String.slice(output, char_count(sliced_output), String.size(output)) sliced_output else # could use output.scan(/[^\n]*\n?/) instead of split sliced_output = output.split("\n").slice(0, effective_height).join("\n") output.replace output.split("\n").slice(effective_height..-1).join("\n") sliced_output end end |