Skip to content
Snippets Groups Projects
Commit d16faf7c authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Factor out a method for stripping ANSI escapes.

parent a38cdad5
No related branches found
No related tags found
No related merge requests found
......@@ -27,9 +27,7 @@ from diffoscope.tempfiles import get_named_temporary_file
from diffoscope.difference import Difference, VisualDifference
from .utils.file import File
from .utils.command import Command, our_check_output
re_ansi_escapes = re.compile(r"\x1b[^m]*m")
from .utils.command import Command, our_check_output, strip_ansi_escapes
logger = logging.getLogger(__name__)
......@@ -40,8 +38,7 @@ class Img2Txt(Command):
return ["img2txt", "--width", "60", "--format", "utf8", self.path]
def filter(self, line):
# Strip ANSI escapes
return re_ansi_escapes.sub("", line.decode("utf-8")).encode("utf-8")
return strip_ansi_escapes(line)
class Identify(Command):
......
......@@ -19,11 +19,14 @@
import abc
import signal
import logging
import re
import subprocess
from .operation import Operation
from ...utils import format_cmdline
re_ansi_escapes = re.compile(rb"\x1b[^m]*m")
logger = logging.getLogger(__name__)
......@@ -115,3 +118,7 @@ def our_check_output(cmd, *args, **kwargs):
logger.debug("Calling external command: %s", " ".join(cmd))
return subprocess.check_output(cmd, *args, **kwargs)
def strip_ansi_escapes(val):
return re_ansi_escapes.sub(b"", val)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment