示例#1
0
文件: format.py 项目: nicolashahn/oil
def PrintTree(node, f, indent=0, max_col=100):
    """Second step of printing: turn homogeneous tree into a colored string.

  Args:
    node: homogeneous tree node
    f: ColorOutput instance.
    max_col: don't print past this column number on ANY line
  """
    ind = ' ' * indent

    # Try printing on a single line
    single_f = f.NewTempBuffer()
    single_f.write(ind)
    if _TrySingleLine(node, single_f, max_col - indent):
        f.WriteRaw(single_f.GetRaw())
        return

    if isinstance(node, str):
        f.write(ind + pretty.Str(node))

    elif isinstance(node, _ColoredString):
        f.PushColor(node.str_type)
        f.write(pretty.Str(node.s))
        f.PopColor()

    elif isinstance(node, _Obj):
        _PrintTreeObj(node, f, indent, max_col)

    else:
        raise AssertionError(node)
示例#2
0
def _GetOpts(spec, argv, optind, errfmt):
  optarg = ''  # not set by default

  try:
    current = argv[optind-1]  # 1-based indexing
  except IndexError:
    return 1, '?', optarg, optind

  if not current.startswith('-'):  # The next arg doesn't look like a flag.
    return 1, '?', optarg, optind

  # It looks like an argument.  Stop iteration by returning 1.
  if current not in spec:  # Invalid flag
    optind += 1
    return 0, '?', optarg, optind

  optind += 1
  opt_char = current[-1]

  needs_arg = spec[current]
  if needs_arg:
    try:
      optarg = argv[optind-1]  # 1-based indexing
    except IndexError:
      errfmt.Print('getopts: option %r requires an argument.', current)
      ui.Stderr('(getopts argv: %s)', ' '.join(pretty.Str(a) for a in argv))
      # Hm doesn't cause status 1?
      return 0, '?', optarg, optind

    optind += 1

  return 0, opt_char, optarg, optind
示例#3
0
def PrintTree(node, f, indent=0, max_col=100):
    # type: (runtime._PrettyBase, ColorOutput, int, int) -> None
    """Second step of printing: turn homogeneous tree into a colored string.

  Args:
    node: homogeneous tree node
    f: ColorOutput instance.
    max_col: don't print past this column number on ANY line
      NOTE: See asdl/run.sh line-length-hist for a test of this.  It's
      approximate.
      TODO: Use the terminal width.
  """
    ind = ' ' * indent

    # Try printing on a single line
    single_f = f.NewTempBuffer()
    single_f.write(ind)
    if _TrySingleLine(node, single_f, max_col - indent):
        f.WriteRaw(single_f.GetRaw())
        return

    if isinstance(node, runtime.PrettyLeaf):
        f.PushColor(node.e_color)
        f.write(pretty.Str(node.s))
        f.PopColor()

    elif isinstance(node, runtime.PrettyNode):
        _PrintTreeObj(node, f, indent, max_col)

    else:
        raise AssertionError(node)
示例#4
0
  def DisplayLine(self):
    # NOTE: This is the format the Tracer uses.

    # bash displays        sleep $n & (code)
    # but OSH displays     sleep 1 &  (argv array)
    # We could switch the former but I'm not sure it's necessary.
    return '[process] %s' % ' '.join(pretty.Str(a) for a in self.arg_vec.strs)
示例#5
0
  def OnSimpleCommand(self, argv):
    # NOTE: I think tracing should be on by default?  For post-mortem viewing.
    if not self.exec_opts.xtrace:
      return

    first_char, prefix = self._EvalPS4()
    cmd = ' '.join(pretty.Str(a) for a in argv)
    self.f.log('%s%s%s', first_char, prefix, cmd)
示例#6
0
文件: format.py 项目: nicolashahn/oil
def _TrySingleLine(node, f, max_chars):
    """Try printing on a single line.

  Args:
    node: homogeneous tree node
    f: ColorOutput instance
    max_chars: maximum number of characters to print on THIS line
    indent: current indent level

  Returns:
    ok: whether it fit on the line of the given size.
      If False, you can't use the value of f.
  """
    if isinstance(node, str):
        f.write(pretty.Str(node))

    elif isinstance(node, _ColoredString):
        f.PushColor(node.str_type)
        f.write(pretty.Str(node.s))
        f.PopColor()

    elif isinstance(node, list):  # Can we fit the WHOLE list on the line?
        f.write('[')
        for i, item in enumerate(node):
            if i != 0:
                f.write(' ')
            if not _TrySingleLine(item, f, max_chars):
                return False
        f.write(']')

    elif isinstance(node, _Obj):
        return _TrySingleLineObj(node, f, max_chars)

    else:
        raise AssertionError("Unexpected node: %r (%r)" %
                             (node, node.__class__))

    # Take into account the last char.
    num_chars_so_far = f.NumChars()
    if num_chars_so_far > max_chars:
        return False

    return True
示例#7
0
文件: format.py 项目: waldyrious/oil
def _TrySingleLine(
        node,  # type: runtime._PrettyBase
        f,  # type: ColorOutput
        max_chars,  # type: int
):
    # type: (...) -> bool
    """Try printing on a single line.

  Args:
    node: homogeneous tree node
    f: ColorOutput instance
    max_chars: maximum number of characters to print on THIS line
    indent: current indent level

  Returns:
    ok: whether it fit on the line of the given size.
      If False, you can't use the value of f.
  """
    if isinstance(node, runtime.PrettyLeaf):
        f.PushColor(node.e_color)
        f.write(pretty.Str(node.s))
        f.PopColor()

    elif isinstance(
            node,
            runtime.PrettyArray):  # Can we fit the WHOLE list on the line?
        f.write('[')
        for i, item in enumerate(node.children):
            if i != 0:
                f.write(' ')
            if not _TrySingleLine(item, f, max_chars):
                return False
        f.write(']')

    elif isinstance(node, runtime.PrettyNode):
        return _TrySingleLineObj(node, f, max_chars)

    else:
        raise AssertionError("Unexpected node: %r" % node)
        # mycpp doesn't like __class__
        #raise AssertionError("Unexpected node: %r (%r)" % (node, node.__class__))

    # Take into account the last char.
    num_chars_so_far = f.NumChars()
    if num_chars_so_far > max_chars:
        return False

    return True