#!/usr/bin/env python3

import argparse
import subprocess
import re
from Beautifier.Beautifier import Beautifier

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


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():
    parser = argparse.ArgumentParser()
    parser.add_argument('exec', help='Executable file passed to Valgrind', type=str)
    args = parser.parse_args()
    output = call_valgrind(args.exec)
    beautifier = Beautifier()
    colored_output = beautifier.process(output)
    print(colored_output)

if __name__ == "__main__":
    valgreen()