Module: Doing::Pager

Defined in:
lib/doing/pager.rb

Overview

Pagination

Class Method Summary collapse

Class Method Details

.page(text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/doing/pager.rb', line 15

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

  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]

    pager = which_pager
    Doing.logger.debug('Pager:', "Using #{pager}")
    begin
      exec(pager.join(' '))
    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



7
8
9
# File 'lib/doing/pager.rb', line 7

def paginate
  @paginate ||= false
end

.paginate=(should_paginate) ⇒ Object



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

def paginate=(should_paginate)
  @paginate = should_paginate
end

.which_pagerObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/doing/pager.rb', line 54

def which_pager
  pagers = [ENV['GIT_PAGER'], ENV['PAGER']]

  if Util.exec_available('git')
    git_pager = `git config --get-all core.pager || true`.split.first
    git_pager && pagers.push(git_pager)
  end

  pagers.concat(%w[bat less more pager])

  pagers.select! do |f|
    if f
      if f.strip =~ /[ |]/
        f
      elsif f == 'most'
        Doing.logger.warn('most not allowed as pager')
        false
      else
        system "which #{f}", out: File::NULL, err: File::NULL
      end
    else
      false
    end
  end

  pg = pagers.first
  args = case pg
         when /^more$/
           ' -r'
         when /^less$/
           ' -Xr'
         when /^bat$/
           ' -p --pager="less -Xr"'
         else
           ''
         end

  [pg, args]
end