Module: NA::Pager
- Defined in:
- lib/na/pager.rb
Overview
Pagination
Class Attribute Summary collapse
-
.paginate ⇒ Boolean
Boolean determines whether output is paginated.
Class Method Summary collapse
-
.page(text) ⇒ Boolean?
Page output.
Class Attribute Details
.paginate ⇒ Boolean
Boolean determines whether output is paginated
12 13 14 |
# File 'lib/na/pager.rb', line 12 def paginate @paginate ||= false end |
Class Method Details
.page(text) ⇒ Boolean?
Page output. If @paginate is false, just dump to STDOUT
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/na/pager.rb', line 25 def page(text) unless @paginate puts text return end # Skip pagination for small outputs (faster than starting a pager) if text.length < 2000 && text.lines.count < 50 puts text return end pager = which_pager return false unless pager # Optimized pager execution - use spawn instead of fork+exec read_io, write_io = IO.pipe # Use spawn for better performance than fork+exec pid = spawn(pager, in: read_io, out: $stdout, err: $stderr) read_io.close begin # Write data to pager write_io.write(text) write_io.close # Wait for pager to complete _, status = Process.waitpid2(pid) status.success? rescue SystemCallError # Clean up on error begin write_io.close rescue StandardError nil end begin Process.kill('TERM', pid) rescue StandardError nil end begin Process.waitpid(pid) rescue StandardError nil end false end end |