コード例 #1
0
    def __init__(self, contents, out=None, prompt=None):
        """Constructor.

        Args:
          contents: The entire contents of the text lines to page.
          out: The output stream, log.out (effectively) if None.
          prompt: The page break prompt, a defalt prompt is used if None..
        """
        self._contents = contents
        self._out = out or sys.stdout
        self._search_pattern = None
        self._search_direction = None

        # prev_pos, prev_next values to force reprint
        self.prev_pos, self.prev_nxt = self.PREV_POS_NXT_REPRINT
        # Initialize the console attributes.
        self._attr = console_attr.GetConsoleAttr()
        self._width, self._height = self._attr.GetTermSize()

        # Initialize the prompt and the prompt clear string.
        if not prompt:
            prompt = "{bold}--({{percent}}%)--{normal}".format(
                bold=self._attr.GetFontCode(bold=True), normal=self._attr.GetFontCode()
            )
        self._clear = "\r{0}\r".format(" " * (self._attr.DisplayWidth(prompt) - 6))
        self._prompt = prompt

        # Initialize a list of lines with long lines split into separate display
        # lines.
        self._lines = []
        for line in contents.splitlines():
            self._lines += self._attr.SplitLine(line, self._width)
コード例 #2
0
def More(contents, out, prompt=None, check_pager=True):
  """Run a user specified pager or fall back to the internal pager.

  Args:
    contents: The entire contents of the text lines to page.
    out: The output stream.
    prompt: The page break prompt.
    check_pager: Checks the PAGER env var and uses it if True.
  """
  if not IsInteractive(output=True):
    out.write(contents)
    return
  if check_pager:
    pager = encoding.GetEncodedValue(os.environ, 'PAGER', None)
    if pager == '-':
      # Use the fallback Pager.
      pager = None
    elif not pager:
      # Search for a pager that handles ANSI escapes.
      for command in ('less', 'pager'):
        if files.FindExecutableOnPath(command):
          pager = command
          break
    if pager:
      # If the pager is less(1) then instruct it to display raw ANSI escape
      # sequences to enable colors and font embellishments.
      less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None)
      less = '-R' + (less_orig or '')
      encoding.SetEncodedValue(os.environ, 'LESS', less)
      # Ignores SIGINT from this point on since the child process has started
      # and we don't want to terminate either one when the child is still alive.
      signal.signal(signal.SIGINT, signal.SIG_IGN)
      # Runs PreexecFunc before starting the child so SIGINT is ignored for the
      # child process as well.
      p = subprocess.Popen(
          pager, stdin=subprocess.PIPE, shell=True, preexec_fn=PreexecFunc)
      enc = console_attr.GetConsoleAttr().GetEncoding()
      p.communicate(input=contents.encode(enc))
      p.wait()
      # Starts using default disposition for SIGINT again after the child has
      # exited.
      signal.signal(signal.SIGINT, signal.SIG_DFL)
      if less_orig is None:
        encoding.SetEncodedValue(os.environ, 'LESS', None)
      return
  # Fall back to the internal pager.
  console_pager.Pager(contents, out, prompt).Run()
コード例 #3
0
def More(contents, out, prompt=None, check_pager=True):
    """Run a user specified pager or fall back to the internal pager.

    Args:
      contents: The entire contents of the text lines to page.
      out: The output stream.
      prompt: The page break prompt.
      check_pager: Checks the PAGER env var and uses it if True.
    """
    if not IsInteractive(output=True):
        out.write(contents)
        return
    if check_pager:
        pager = encoding.GetEncodedValue(os.environ, "PAGER", None)
        if pager == "-":
            # Use the fallback Pager.
            pager = None
        elif not pager:
            # Search for a pager that handles ANSI escapes.
            for command in ("less", "pager"):
                if files.FindExecutableOnPath(command):
                    pager = command
                    break
        if pager:
            # If the pager is less(1) then instruct it to display raw ANSI escape
            # sequences to enable colors and font embellishments.
            less_orig = encoding.GetEncodedValue(os.environ, "LESS", None)
            less = "-R" + (less_orig or "")
            encoding.SetEncodedValue(os.environ, "LESS", less)
            # Ignore SIGINT while the pager is running.
            # We don't want to terminate the parent while the child is still alive.
            signal.signal(signal.SIGINT, signal.SIG_IGN)
            p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True)
            enc = console_attr.GetConsoleAttr().GetEncoding()
            p.communicate(input=contents.encode(enc))
            p.wait()
            # Start using default signal handling for SIGINT again.
            signal.signal(signal.SIGINT, signal.SIG_DFL)
            if less_orig is None:
                encoding.SetEncodedValue(os.environ, "LESS", None)
            return
    # Fall back to the internal pager.
    console_pager.Pager(contents, out, prompt).Run()
コード例 #4
0
ファイル: console_io.py プロジェクト: yunho0130/python-fire
def More(contents, out, prompt=None, check_pager=True):
    """Run a user specified pager or fall back to the internal pager.

  Args:
    contents: The entire contents of the text lines to page.
    out: The output stream.
    prompt: The page break prompt.
    check_pager: Checks the PAGER env var and uses it if True.
  """
    if not IsInteractive(output=True):
        out.write(contents)
        return
    if check_pager:
        pager = encoding.GetEncodedValue(os.environ, 'PAGER', None)
        if pager == '-':
            # Use the fallback Pager.
            pager = None
        elif not pager:
            # Search for a pager that handles ANSI escapes.
            for command in ('less', 'pager'):
                if files.FindExecutableOnPath(command):
                    pager = command
                    break
        if pager:
            # If the pager is less(1) then instruct it to display raw ANSI escape
            # sequences to enable colors and font embellishments.
            less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None)
            less = '-R' + (less_orig or '')
            encoding.SetEncodedValue(os.environ, 'LESS', less)
            p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True)
            enc = console_attr.GetConsoleAttr().GetEncoding()
            p.communicate(input=contents.encode(enc))
            p.wait()
            if less_orig is None:
                encoding.SetEncodedValue(os.environ, 'LESS', None)
            return
    # Fall back to the internal pager.
    console_pager.Pager(contents, out, prompt).Run()