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.



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

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.



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

def height
  @height
end

#widthObject (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, 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.



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

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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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