Class: Hirb::Pager
- Inherits:
-
Object
- Object
- Hirb::Pager
- Defined in:
- lib/spirit_hands/hirb/fixes/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
class methods.
-
#options ⇒ Object
readonly
class methods.
-
#width ⇒ Object
readonly
class methods.
Class Method Summary collapse
-
.basic_pager(output, override_pager_command = nil) ⇒ Object
Exposed to allow user-custom, external-driven formatting.
-
.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.
- .page(string, inspect_mode, pgr_cmd, width, height) ⇒ Object
-
.pager_command ⇒ Object
:nodoc:.
-
.pager_command=(*commands) ⇒ Object
:nodoc:.
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.
- #pager_command ⇒ Object
-
#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.
97 98 99 100 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 97 def initialize(width, height, ={}) resize(width, height) = end |
Instance Attribute Details
#height ⇒ Object (readonly)
class methods
95 96 97 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 95 def height @height end |
#options ⇒ Object (readonly)
class methods
95 96 97 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 95 def end |
#width ⇒ Object (readonly)
class methods
95 96 97 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 95 def width @width end |
Class Method Details
.basic_pager(output, override_pager_command = nil) ⇒ Object
Exposed to allow user-custom, external-driven formatting
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 17 def basic_pager(output, override_pager_command=nil) pc = basic_pager_command(override_pager_command) pager = IO.popen(pc, "w") begin save_stdout = STDOUT.clone STDOUT.reopen(pager) STDOUT.puts output rescue Errno::EPIPE ensure STDOUT.reopen(save_stdout) save_stdout.close pager.close end end |
.command_pager(output, options = {}) ⇒ Object
Pages using a configured or detected shell command.
10 11 12 13 14 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 10 def command_pager(output, ={}) if valid_pager_command?(pc = [:pager_command]) basic_pager(output, pc) end end |
.default_pager(output, options = {}) ⇒ Object
Pages with a ruby-only pager which either pages or quits.
41 42 43 44 45 46 47 48 49 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 41 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 |
.page(string, inspect_mode, pgr_cmd, width, height) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 51 def page(string, inspect_mode, pgr_cmd, width, height) if valid_pager_command?(pgr_cmd) command_pager(string, :pager_command=>pgr_cmd) else default_pager(string, :width=>width, :height=>height, :inspect=>inspect_mode) end end |
.pager_command ⇒ Object
:nodoc:
36 37 38 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 36 def pager_command #:nodoc: @pager_command || pager_command_select end |
.pager_command=(*commands) ⇒ Object
:nodoc:
32 33 34 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 32 def pager_command=(*commands) #:nodoc: @pager_command = pager_command_select(*commands) 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.
126 127 128 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 126 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:
131 132 133 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 131 def char_count(string) #:nodoc: string.chars.count end |
#page(string, inspect_mode) ⇒ Object
Pages given string using configured pager.
107 108 109 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 107 def page(string, inspect_mode) self.class.page(string, inspect_mode, pager_command, @width, @height) end |
#pager_command ⇒ Object
102 103 104 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 102 def pager_command [:pager_command] || self.class.pager_command end |
#resize(width, height) ⇒ Object
:nodoc:
140 141 142 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 140 def resize(width, height) #:nodoc: @width, @height = View.determine_terminal_size(width, height) end |
#slice!(output, inspect_mode = false) ⇒ Object
:nodoc:
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 111 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 |