예제 #1
0
def render(markup, *, ensure_newline=True, file=None, renderer=Renderer):
    if file is None:
        file = sys.stdout

    try:
        fileno = file.fileno()
    except OSError:
        # This is a hack to try to get nice colorized dump output over
        # a remote-pdb connection. If the output is redirected to
        # something without fileno, use what ought to be stdout's fileno
        # to decide on color, etc.
        fileno = 1
    max_width = term.size(fileno)[1]

    style_table = None
    if term.use_colors(fileno):
        max_colors = term.max_colors()
        if max_colors > 255:
            style_table = styles_module.Dark256
        elif max_colors > 6:
            style_table = styles_module.Dark16

    rendered = renderer.renders(markup,
                                styles=style_table,
                                max_width=max_width)
    if ensure_newline and not rendered.endswith('\n'):
        rendered += '\n'

    print(rendered, file=file, end='')
예제 #2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__styles = None
        self._colorize = term.use_colors()

        if self._colorize:
            self._init_styles()
예제 #3
0
파일: terminal.py 프로젝트: xing0713/edgedb
def render(markup, *, ensure_newline=True, file=None, renderer=Renderer):
    if file is None:
        file = sys.stdout

    fileno = file.fileno()
    max_width = term.size(fileno)[1]

    style_table = None
    if term.use_colors(fileno):
        max_colors = term.max_colors()
        if max_colors > 255:
            style_table = styles_module.Dark256
        elif max_colors > 6:
            style_table = styles_module.Dark16

    rendered = renderer.renders(
        markup, styles=style_table, max_width=max_width)
    if ensure_newline and not rendered.endswith('\n'):
        rendered += '\n'

    print(rendered, file=file, end='')
예제 #4
0
파일: __init__.py 프로젝트: fantix/edgedb
    def run(self) -> None:
        self._prompt = self.build_propmpt()
        self.ensure_connection()
        self.context.use_colors = term.use_colors(sys.stdout.fileno())
        banner_shown = False

        try:
            while True:
                self.ensure_connection()
                if not banner_shown:
                    self.show_banner()
                    banner_shown = True

                try:
                    text = self.prompt.prompt()
                except KeyboardInterrupt:
                    continue

                command = text.strip()
                if not command:
                    continue

                if command in self.exit_commands:
                    raise EOFError

                elif command.startswith('\\'):
                    try:
                        self._parser.run(command)
                        print()
                    except LookupError as e:
                        render.render_error(self.context, str(e))
                    except Exception as ex:
                        render.render_exception(self.context, ex)
                    continue

                qm = self.context.query_mode
                results = []
                last_query = None
                if self.context.implicit_limit:
                    limit = self.context.implicit_limit + 1
                else:
                    limit = 0
                json_mode = qm is context.QueryMode.JSON
                try:
                    for query in utils.split_edgeql(command)[0]:
                        last_query = query
                        results.append(self.fetch(
                            query,
                            json=json_mode,
                            implicit_limit=limit,
                        ))

                except KeyboardInterrupt:
                    self.connection.close()
                    self._connection = None
                    print('\r', end='')
                    render.render_error(
                        self.context,
                        '== aborting query and closing the connection ==')
                    continue
                except Exception as ex:
                    render.render_exception(self.context, ex, query=last_query)
                    continue

                max_width = self.prompt.output.get_size().columns
                try:
                    for result, status in results:
                        if status in STATUSES_WITH_OUTPUT:
                            if qm is context.QueryMode.JSON:
                                render.render_json(
                                    self.context,
                                    result,
                                    max_width=min(max_width, MAX_WIDTH))
                            else:
                                render.render_binary(
                                    self.context,
                                    result,
                                    max_width=min(max_width, MAX_WIDTH))
                        elif status:
                            render.render_status(self.context, status)
                except KeyboardInterrupt:
                    print('\r', end='')
                    render.render_error(
                        self.context,
                        '== aborting rendering of the result ==')
                    continue
                except Exception as ex:
                    render.render_error(
                        self.context,
                        '== an exception while rendering the result ==')
                    render.render_exception(self.context, ex)

        except EOFError:
            return
예제 #5
0
    def run(self):
        self.prompt = self.build_propmpt()
        self.ensure_connection()
        self.context.use_colors = term.use_colors(sys.stdout.fileno())
        banner_shown = False

        try:
            while True:
                self.ensure_connection()
                if not banner_shown:
                    self.show_banner()
                    banner_shown = True

                try:
                    text = self.prompt.prompt()
                except KeyboardInterrupt:
                    continue

                command = text.strip()
                if not command:
                    continue

                if command in self.exit_commands:
                    raise EOFError

                if command == R'\?':
                    for title, desc, _, is_devonly in self.commands.values():
                        if is_devonly:
                            continue
                        print(f'  {title:<20} {desc}')
                    _q = r'\q or "exit"'
                    print(f'  {_q:<20} quit')
                    print()
                    continue

                elif command.startswith('\\'):
                    prefix, _, args = command.partition(' ')
                    prefix = prefix[1:]
                    if prefix in self.commands:
                        self.ensure_connection()
                        self.commands[prefix][2](self, args)
                    else:
                        print(f'No command {command} is found.')
                        print(R'Try \? to see the list of supported commands.')
                    continue

                qm = self.context.query_mode
                results = []
                last_query = None
                try:
                    if qm is context.QueryMode.Normal:
                        for query in lexutils.split_edgeql(command)[0]:
                            last_query = query
                            results.append(self.fetch(query, json=False))
                    else:
                        for query in lexutils.split_edgeql(command)[0]:
                            last_query = query
                            results.append(self.fetch(query, json=True))

                except KeyboardInterrupt:
                    self.connection.close()
                    self.connection = None
                    print('\r', end='')
                    render.render_error(
                        self.context,
                        '== aborting query and closing the connection ==')
                    continue
                except Exception as ex:
                    render.render_exception(self.context, ex, query=last_query)
                    continue

                max_width = self.prompt.output.get_size().columns
                try:
                    for result, status in results:
                        if status in STATUSES_WITH_OUTPUT:
                            if qm is context.QueryMode.JSON:
                                render.render_json(self.context,
                                                   result,
                                                   max_width=min(
                                                       max_width, 120))
                            else:
                                render.render_binary(self.context,
                                                     result,
                                                     max_width=min(
                                                         max_width, 120))
                        else:
                            render.render_status(self.context, status)
                except KeyboardInterrupt:
                    print('\r', end='')
                    render.render_error(
                        self.context, '== aborting rendering of the result ==')
                    continue
                except Exception as ex:
                    render.render_error(
                        self.context,
                        '== an exception while rendering the result ==')
                    render.render_exception(self.context, ex)

        except EOFError:
            return