code: def _detect_screen_size(screen_lines_def):
      """Attempt to work out the number of lines on the screen.
  
      This is called by page(). It can raise an error (e.g. when run in the
      test suite), so it's separated out so it can easily be called in a try block.
      """
      TERM = os.environ.get("TERM", None)
      if not (
          (TERM == "xterm" or TERM == "xterm-color")
          and sys.platform != "sunos5"
      ):
          # curses causes problems on many terminals other than xterm, and
          # some termios calls lock up on Sun OS5.
          return screen_lines_def
  
      try:
          import termios
          import curses
      except ImportError:
          return screen_lines_def
  
      # There is a bug in curses, where *sometimes* it fails to properly
      # initialize, and then after the endwin() call is made, the
      # terminal is left in an unusable state.  Rather than trying to
      # check every time for this (by requesting and comparing termios
      # flags each time), we just save the initial terminal state and
      # unconditionally reset it every time.  It's cheaper than making
      # the checks.
      try:
          term_flags = termios.tcgetattr(sys.stdout)
      except termios.error as err:
          # can fail on Linux 2.6, pager_page will catch the TypeError
          raise TypeError("termios error: {0}".format(err)) from err
  
      try:
          scr = curses.initscr()
      except AttributeError:
          # Curses on Solaris may not be complete, so we can't use it there
          return screen_lines_def
  
      screen_lines_real, screen_cols = scr.getmaxyx()
      curses.endwin()
  
      # Restore terminal state in case endwin() didn't.
      termios.tcsetattr(sys.stdout, termios.TCSANOW, term_flags)
      # Now we have what we needed: the screen size in rows/columns
      return screen_lines_real
  
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 4 for calling a global function with example usage: _detect_screen_size(screen_lines_def) and returns: screen_lines_real, screen_lines_def, screen_lines_def, screen_lines_def