#!/usr/bin/env python3

import subprocess
import re
import sys
from Beautifier.Beautifier import Beautifier

VFLAGS = ["--leak-check=full", "--track-origins=yes", "--show-reachable=yes"]


def call_valgrind(exec, flags=VFLAGS):
    valgrind = subprocess.Popen(["valgrind", *flags, *exec],
                                stdin=subprocess.DEVNULL, 
                                stdout=subprocess.PIPE, 
                                stderr=subprocess.STDOUT)
    out, _ = valgrind.communicate()
    return out.decode("utf-8")

def valgreen():
    output = call_valgrind(sys.argv[1:])
    beautifier = Beautifier()
    colored_output = beautifier.process(output)
    print(colored_output)

if __name__ == "__main__":
    valgreen()
