Module: PageMe

Includes:
POSIX::Spawn
Defined in:
lib/pageme.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pagerObject



40
41
42
# File 'lib/pageme.rb', line 40

def pager
  @pager || ENV["PAGER"] || "less"
end

#paging_disabled=(value) ⇒ Object (writeonly)

Sets the attribute paging_disabled

Parameters:

  • value

    the value to set the attribute paging_disabled to.



24
25
26
# File 'lib/pageme.rb', line 24

def paging_disabled=(value)
  @paging_disabled = value
end

Class Method Details

.use_domain_socket_for_paging=(f) ⇒ Object



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

def self.use_domain_socket_for_paging=(f)
  if f
    require "tempfile"
    require "socket"
  end
  @use_domain_socket_for_paging = f
end

.use_domain_socket_for_paging?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/pageme.rb', line 18

def self.use_domain_socket_for_paging?
  @use_domain_socket_for_paging
end

Instance Method Details

#disable_pagingObject



32
33
34
# File 'lib/pageme.rb', line 32

def disable_paging
  self.paging_disabled = true
end

#enable_pagingObject



36
37
38
# File 'lib/pageme.rb', line 36

def enable_paging
  self.paging_disabled = false
end

#page(*args, &p) ⇒ Object



44
45
46
47
48
49
50
51
52
53
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pageme.rb', line 44

def page *args, &p
  args << p
  args.compact!

  if paging_disabled?
    page_to STDOUT, args
    return
  end

  t, w, server = nil

  if defined? JRUBY_VERSION
    loop do
      begin
        t = File.join(Dir::tmpdir, "socket-for-jruby-pager-#{rand(2**16)}")
        server = UNIXServer.new(t)
        break
      rescue Errno::EADDRINUSE
      end
    end


    child_pid = spawn "netcat -U #{t} | #{pager}"

    w = server.accept
  else
    r, w = IO.pipe
    child_pid = spawn pager, :in => r, w => :close
    r.close
  end

  previous_int_handler = Kernel.trap("SIGINT"){ Process.kill "INT", child_pid }
  previous_term_handler = Kernel.trap("SIGTERM") do 
    Process.kill "TERM", child_pid 
    Process.wait child_pid, Process::WUNTRACED 
    child_pid = nil
    if previous_term_handler == "DEFAULT"
      raise SignalException.new("SIGTERM")
    else
      previous_term_handler.call
    end
  end

  begin
    page_to w, args
  rescue Errno::EPIPE
  rescue Exception => e
    begin
      unless w.closed?
        w.puts "#{e.class}: #{e.message}"
        e.backtrace.each{|l| w.puts "\t#{l}" }
      end 
    rescue Errno::EPIPE
    end

    raise 
  ensure
    begin
      w.close unless w.closed?
    ensure
      Process.wait child_pid, Process::WUNTRACED  if child_pid
      Kernel.trap("SIGINT", previous_int_handler)
      Kernel.trap("SIGTERM", previous_term_handler)
      File.delete(t) if t
      server.close if server
    end
  end
  nil
end

#page_file(file) ⇒ Object

Raises:

  • (Errno::ENOENT)


114
115
116
117
118
119
# File 'lib/pageme.rb', line 114

def page_file file
  raise Errno::ENOENT, file unless File.exist? file

  system pager, file
  nil
end

#paging_disabled?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/pageme.rb', line 26

def paging_disabled?
  return @paging_disabled if defined? paging_disabled

  !(STDOUT.tty?)
end

#with_paged_outputObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pageme.rb', line 121

def with_paged_output
  page do |o|
    original_stdout = STDOUT.dup
    original_stderr = STDERR.dup
    begin
      STDERR.reopen(o)
      STDOUT.reopen(o)
      yield
    ensure
      STDERR.reopen(original_stderr)
      STDOUT.reopen(original_stdout)
    end 
  end
end