示例#1
0
def _PrintDirStack(dir_stack, style, home_dir):
  """Helper for 'dirs'."""

  if style == WITH_LINE_NUMBERS:
    for i, entry in enumerate(dir_stack.Iter()):
      print('%2d  %s' % (i, ui.PrettyDir(entry, home_dir)))

  elif style == WITHOUT_LINE_NUMBERS:
    for entry in dir_stack.Iter():
      print(ui.PrettyDir(entry, home_dir))

  elif style == SINGLE_LINE:
    s = ' '.join(ui.PrettyDir(entry, home_dir) for entry in dir_stack.Iter())
    print(s)
示例#2
0
def _PrintDirStack(dir_stack, style, home_dir):
    # type: (DirStack, int, Optional[str]) -> None
    """Helper for 'dirs'."""

    if style == WITH_LINE_NUMBERS:
        for i, entry in enumerate(dir_stack.Iter()):
            print('%2d  %s' % (i, ui.PrettyDir(entry, home_dir)))

    elif style == WITHOUT_LINE_NUMBERS:
        for entry in dir_stack.Iter():
            print(ui.PrettyDir(entry, home_dir))

    elif style == SINGLE_LINE:
        parts = [ui.PrettyDir(entry, home_dir) for entry in dir_stack.Iter()]
        s = ' '.join(parts)
        print(s)
示例#3
0
    def _ReplaceBackslashCodes(self, tokens):
        # type: (List[Tuple[Id_t, str]]) -> str
        ret = []  # type: List[str]
        non_printing = 0
        for id_, value in tokens:
            # BadBacklash means they should have escaped with \\.  TODO: Make it an error.
            # 'echo -e' has a similar issue.
            if id_ in (Id.PS_Literals, Id.PS_BadBackslash):
                ret.append(value)

            elif id_ == Id.PS_Octal3:
                i = int(value[1:], 8)
                ret.append(chr(i % 256))

            elif id_ == Id.PS_LBrace:
                non_printing += 1
                ret.append('\x01')

            elif id_ == Id.PS_RBrace:
                non_printing -= 1
                if non_printing < 0:  # e.g. \]\[
                    return PROMPT_ERROR

                ret.append('\x02')

            elif id_ == Id.PS_Subst:  # \u \h \w etc.
                ch = value[1:]
                if ch == '$':  # So the user can tell if they're root or not.
                    r = self.cache.Get('$')

                elif ch == 'u':
                    r = self.cache.Get('user')

                elif ch == 'h':
                    hostname = self.cache.Get('hostname')
                    # foo.com -> foo
                    r, _ = mylib.split_once(hostname, '.')

                elif ch == 'H':
                    r = self.cache.Get('hostname')

                elif ch == 'w':
                    try:
                        pwd = state.GetString(self.mem, 'PWD')
                        home = state.MaybeString(
                            self.mem, 'HOME')  # doesn't have to exist
                        # Shorten to ~/mydir
                        r = ui.PrettyDir(pwd, home)
                    except error.Runtime as e:
                        r = '<Error: %s>' % e.UserErrorString()

                elif ch == 'W':
                    val = self.mem.GetValue('PWD')
                    if val.tag_() == value_e.Str:
                        str_val = cast(value__Str, val)
                        r = os_path.basename(str_val.s)
                    else:
                        r = '<Error: PWD is not a string> '

                else:
                    r = consts.LookupCharPrompt(ch)

                    # TODO: Handle more codes
                    # R(r'\\[adehHjlnrstT@AuvVwW!#$\\]', Id.PS_Subst),
                    if r is None:
                        r = r'<Error: \%s not implemented in $PS1> ' % ch

                # See comment above on bash hack for $.
                ret.append(r.replace('$', '\\$'))

            else:
                raise AssertionError('Invalid token %r' % id_)

        # mismatched brackets, see https://github.com/oilshell/oil/pull/256
        if non_printing != 0:
            return PROMPT_ERROR

        return ''.join(ret)
示例#4
0
    def _ReplaceBackslashCodes(self, tokens):
        # type: (List[Tuple[Id, str]]) -> str
        ret = []
        non_printing = 0
        for id_, value in tokens:
            # BadBacklash means they should have escaped with \\.  TODO: Make it an error.
            # 'echo -e' has a similar issue.
            if id_ in (Id.PS_Literals, Id.PS_BadBackslash):
                ret.append(value)

            elif id_ == Id.PS_Octal3:
                i = int(value[1:], 8)
                ret.append(chr(i % 256))

            elif id_ == Id.PS_LBrace:
                non_printing += 1
                ret.append('\x01')

            elif id_ == Id.PS_RBrace:
                non_printing -= 1
                if non_printing < 0:  # e.g. \]\[
                    return PROMPT_ERROR

                ret.append('\x02')

            elif id_ == Id.PS_Subst:  # \u \h \w etc.
                char = value[1:]
                if char == '$':  # So the user can tell if they're root or not.
                    r = '#' if self.cache.Get('euid') == 0 else '$'

                elif char == 'u':
                    r = self.cache.Get('user')

                elif char == 'h':
                    r = self.cache.Get('hostname').split('.')[0]

                elif char == 'H':
                    r = self.cache.Get('hostname')

                elif char == 'w':
                    val = self.mem.GetVar('PWD')
                    if val.tag == value_e.Str:
                        # Shorten to ~/mydir
                        r = ui.PrettyDir(val.s, self.mem.GetVar('HOME'))
                    else:
                        r = '<Error: PWD is not a string> '

                elif char == 'W':
                    val = self.mem.GetVar('PWD')
                    if val.tag == value_e.Str:
                        r = os_path.basename(val.s)
                    else:
                        r = '<Error: PWD is not a string> '

                elif char in _ONE_CHAR:
                    r = _ONE_CHAR[char]

                else:
                    r = r'<Error: \%s not implemented in $PS1> ' % char

                # See comment above on bash hack for $.
                ret.append(r.replace('$', '\\$'))

            else:
                raise AssertionError('Invalid token %r' % id_)

        # mismatched brackets, see https://github.com/oilshell/oil/pull/256
        if non_printing != 0:
            return PROMPT_ERROR

        return ''.join(ret)