code: def pager_page(strng, start=0, screen_lines=0, pager_cmd=None):
      """Display a string, piping through a pager after a certain length.
  
      strng can be a mime-bundle dict, supplying multiple representations,
      keyed by mime-type.
  
      The screen_lines parameter specifies the number of *usable* lines of your
      terminal screen (total lines minus lines you need to reserve to show other
      information).
  
      If you set screen_lines to a number <=0, page() will try to auto-determine
      your screen size and will only use up to (screen_size+screen_lines) for
      printing, paging after that. That is, if you want auto-detection but need
      to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
      auto-detection without any lines reserved simply use screen_lines = 0.
  
      If a string won't fit in the allowed lines, it is sent through the
      specified pager command. If none given, look for PAGER in the environment,
      and ultimately default to less.
  
      If no system pager works, the string is sent through a 'dumb pager'
      written in python, very simplistic.
      """
  
      # for compatibility with mime-bundle form:
      if isinstance(strng, dict):
          strng = strng["text/plain"]
  
      # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
      TERM = os.environ.get("TERM", "dumb")
      if TERM in ["dumb", "emacs"] and os.name != "nt":
          print(strng)
          return
      # chop off the topmost part of the string we don't want to see
      str_lines = strng.splitlines()[start:]
      str_toprint = os.linesep.join(str_lines)
      num_newlines = len(str_lines)
      len_str = len(str_toprint)
  
      # Dumb heuristics to guesstimate number of on-screen lines the string
      # takes.  Very basic, but good enough for docstrings in reasonable
      # terminals. If someone later feels like refining it, it's not hard.
      numlines = max(num_newlines, int(len_str / 80) + 1)
  
      screen_lines_def = get_terminal_size()[1]
  
      # auto-determine screen size
      if screen_lines <= 0:
          try:
              screen_lines += _detect_screen_size(screen_lines_def)
          except (TypeError, UnsupportedOperation):
              print(str_toprint)
              return
  
      # print 'numlines',numlines,'screenlines',screen_lines  # dbg
      if numlines <= screen_lines:
          # print '*** normal print'  # dbg
          print(str_toprint)
      else:
          # Try to open pager and default to internal one if that fails.
          # All failure modes are tagged as 'retval=1', to match the return
          # value of a failed system command.  If any intermediate attempt
          # sets retval to 1, at the end we resort to our own page_dumb() pager.
          pager_cmd = get_pager_cmd(pager_cmd)
          pager_cmd += " " + get_pager_start(pager_cmd, start)
          if os.name == "nt":
              if pager_cmd.startswith("type"):
                  # The default WinXP 'type' command is failing on complex strings.
                  retval = 1
              else:
                  fd, tmpname = tempfile.mkstemp(".txt")
                  tmppath = Path(tmpname)
                  try:
                      os.close(fd)
                      with tmppath.open(
                          "wt", encoding="utf-8"
                      ) as tmpfile:
                          tmpfile.write(strng)
                          cmd = "%s < %s" % (pager_cmd, tmppath)
                      # tmpfile needs to be closed for windows
                      if os.system(cmd):
                          retval = 1
                      else:
                          retval = None
                  finally:
                      Path.unlink(tmppath)
          else:
              try:
                  retval = None
                  # Emulate os.popen, but redirect stderr
                  proc = subprocess.Popen(
                      pager_cmd,
                      shell=True,
                      stdin=subprocess.PIPE,
                      stderr=subprocess.DEVNULL,
                  )
                  pager = os._wrap_close(
                      io.TextIOWrapper(proc.stdin, encoding="utf-8"),
                      proc,
                  )
                  try:
                      pager_encoding = (
                          pager.encoding or sys.stdout.encoding
                      )
                      pager.write(strng)
                  finally:
                      retval = pager.close()
              except IOError as msg:  # broken pipe when user quits
                  if msg.args == (32, "Broken pipe"):
                      retval = None
                  else:
                      retval = 1
              except OSError:
                  # Other strange problems, sometimes seen in Win2k/cygwin
                  retval = 1
          if retval is not None:
              page_dumb(strng, screen_lines=screen_lines)
  
imports: import os
  import io
  import re
  import sys
  import tempfile
  import subprocess
  from io import UnsupportedOperation
  from pathlib import Path
  from IPython import get_ipython
  from IPython.display import display
  from IPython.core.error import TryNext
  from IPython.utils.data import chop
  from IPython.utils.process import system
  from IPython.utils.terminal import get_terminal_size
  from IPython.utils import py3compat
label_desc: Use image node_id 5 for calling a global function with example usage: pager_page(strng, start, screen_lines, pager_cmd) without return types