Class: Hirb::Pager

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  resize(width, height)
  @options = options
end

Instance Attribute Details

#heightObject (readonly)

class methods



95
96
97
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 95

def height
  @height
end

#optionsObject (readonly)

class methods



95
96
97
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 95

def options
  @options
end

#widthObject (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, options={})
  if valid_pager_command?(pc = options[: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, options={})
  pager = new(options[:width], options[:height])
  while pager.activated_by?(output, options[:inspect])
    puts pager.slice!(output, options[: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_commandObject

: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.

Returns:

  • (Boolean)


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_commandObject



102
103
104
# File 'lib/spirit_hands/hirb/fixes/pager.rb', line 102

def pager_command
  options[: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