Module: Doing::Pager

Defined in:
lib/doing/pager.rb

Overview

Pagination

Class Method Summary collapse

Class Method Details

.page(text) ⇒ Object

Page output. If @paginate is false, just dump to STDOUT

Parameters:

  • text (String)

    text to paginate



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/doing/pager.rb', line 26

def page(text)
  unless @paginate
    puts text
    return
  end

  pager = which_pager
  Doing.logger.debug('Pager:', "Using #{pager}")

  read_io, write_io = IO.pipe

  input = $stdin

  pid = Kernel.fork do
    write_io.close
    input.reopen(read_io)
    read_io.close

    # Wait until we have input before we start the pager
    IO.select [input]

    begin
      exec(pager)
    rescue SystemCallError => e
      raise Errors::DoingStandardError, "Pager error, #{e}"
    end
  end

  begin
    read_io.close
    write_io.write(text)
    write_io.close
  rescue SystemCallError # => e
    # raise Errors::DoingStandardError, "Pager error, #{e}"
  end

  _, status = Process.waitpid2(pid)
  status.success?
end

.paginateObject

Boolean determines whether output is paginated



10
11
12
# File 'lib/doing/pager.rb', line 10

def paginate
  @paginate ||= false
end

.paginate=(should_paginate) ⇒ Object

Enable/disable pagination

Parameters:

  • should_paginate (Boolean)

    true to paginate



17
18
19
# File 'lib/doing/pager.rb', line 17

def paginate=(should_paginate)
  @paginate = should_paginate
end