"""
This module specializes Processor to print every provided message to the
terminal.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys

from .processor import Processor


class Echoer(Processor):
    def __init__(self, queue, clear=False, show_tracing_tags=False):
        super(Echoer, self).__init__(queue)
        self._clear = clear
        self._remove_tracing_tags = not show_tracing_tags

    def process(self, msg):
        if self._clear:
            # ANSI excape sequence
            # \x1b[2J: Clear the screen
            # \x1b[H:  Reset cursor to 0,0
            print('\x1b[2J\x1b[H')
        if self._remove_tracing_tags:
            try:
                del msg.DESERIALIZED.tracing_tags[:]
            except Exception:
                pass
        print(msg)
        sys.stdout.flush()
