code: def detect_mode(term_hint="xterm-256color"):
      
      if "ANSICON" in os.environ:
          return 16
      elif os.environ.get("ConEmuANSI", "OFF") == "ON":
          return 256
      else:
          term = os.environ.get("TERM", term_hint)
          if term.endswith("-256color") or term in ("xterm", "screen"):
              return 256
          elif term.endswith("-color") or term in ("rxvt",):
              return 16
          else:
              return 256
docs: Poor-mans color mode detection.