Class: Hirb::Pager

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, options = {}) ⇒ Pager

Returns a new instance of Pager.



58
59
60
61
# File 'lib/hirb/pager.rb', line 58

def initialize(width, height, options={})
  resize(width, height)
  @pager_command = options[:pager_command] if options[:pager_command]
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



56
57
58
# File 'lib/hirb/pager.rb', line 56

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



56
57
58
# File 'lib/hirb/pager.rb', line 56

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, options={})
  basic_pager(output) if valid_pager_command?(options[:pager_command])
end

.default_pager(output, options = {}) ⇒ Object

Pages with a ruby-only pager which either pages or quits.



19
20
21
22
23
24
25
26
27
# File 'lib/hirb/pager.rb', line 19

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

.pager_command(*commands) ⇒ Object

:nodoc:



10
11
12
13
14
15
16
# File 'lib/hirb/pager.rb', line 10

def pager_command(*commands) #:nodoc:
  @pager_command = (!@pager_command.nil? && commands.empty?) ? @pager_command : 
    begin
      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:

Returns:

  • (Boolean)


30
31
32
# File 'lib/hirb/pager.rb', line 30

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.

Returns:

  • (Boolean)


87
88
89
# File 'lib/hirb/pager.rb', line 87

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:



92
93
94
# File 'lib/hirb/pager.rb', line 92

def char_count(string) #:nodoc:
  string.chars.count
end

#page(string, inspect_mode) ⇒ Object

Pages given string using configured pager.



64
65
66
67
68
69
70
# File 'lib/hirb/pager.rb', line 64

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:



101
102
103
# File 'lib/hirb/pager.rb', line 101

def resize(width, height) #:nodoc:
  @width, @height = View.determine_terminal_size(width, height)
end

#slice!(output, inspect_mode = false) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hirb/pager.rb', line 72

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