Method: CLIMarkdown::Converter#page

Defined in:
lib/mdless/converter.rb

#page(text, &callback) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/mdless/converter.rb', line 552

def page(text, &callback)
  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
    MDLess.log.info("Using `#{pager.join(" ")}` as pager")
    begin
      exec(pager.join(" "))
    rescue SystemCallError => e
      MDLess.log.error(e)
      exit 1
    end
  end

  begin
    read_io.close
    write_io.write(text)
    write_io.close
  rescue SystemCallError
    exit 1
  end

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