<commit_before>#!/usr/bin/env python
"""
Utilities for system scripts
"""
import subprocess
import sys

COLOR_GRAY = '30'
COLOR_RED = '31'
COLOR_GREEN = '32'
COLOR_YELLOW = '33'
COLOR_BLUE = '34'
COLOR_MAGENTA = '35'
COLOR_CYAN = '36'
COLOR_WHITE = '37'
COLOR_CRIMSON = '38'


def colorize(msg, color):
    """
    Wrap a message in shell color codes.
    """
    return '\033[1;{0}m{1}\033[1;m'.format(color, msg)


def uncolorize(msg):
    """
    Strip shell color codes from a string
    """
    code = '\033[1;'
    if msg.find(code) >= 0:
        msg = msg.replace(code, '')[3:-1]
    return msg


def ensure_commands_exist(commands, exit_code=1):
    """
    Ensure that all commands given by an iterable exist, otherwise exit.
    """
    commands_exist = map(command_available, commands)
    if not all(commands_exist):
        sys.exit(exit_code)
    return None


def command_available(command, print_error=True):
    """
    See if a command exists by trying to call it with --help.
    """
    try:
        subprocess.call(
            [command, '--help'],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        return True
    except OSError as e:
        if print_error:
            print('You must install {0}.'.format(command))
        return False


class PPTable(object):
    """
    Pretty-print a table.
    """
    def __init__(self, cols, data=None):
        self.cols = cols
        self.data = data or []

    def add_data(self, data):
        self.data.append(data)

    def _col_width(self, col_index):
        max_data_width = max([len(uncolorize(d[col_index])) for d in self.data])
        return max(len(self.cols[col_index]), max_data_width)

    @property
    def header(self):
        h = []
        for i, col in enumerate(self.cols):
            width = self._col_width(i)
            h.append('{0:<{width}}'.format(col, width=width))
        return ' | '.join([''] + h + ['']).strip()

    @property
    def rows(self):
        lines = []
        for row in self.data:
            r = []
            for i, value in enumerate(row):
                width = self._col_width(i)
                if len(value) > width:
                    width = len(value) + (6 - len(uncolorize(value)))
                val = '{0:<{width}}'.format(value, width=width)
                r.append(val)
            lines.append(' | '.join([''] + r + ['']).strip())
        return '\n'.join(lines)

    def __str__(self):
        horizontal_border = '+' + '-'*(len(self.header)-2) + '+'
        return '\n'.join([
            horizontal_border,<commit_msg>Fixing width correction for color-coded table values<commit_after>#!/usr/bin/env python
"""
Utilities for system scripts
"""
import subprocess
import sys

COLOR_GRAY = '30'
COLOR_RED = '31'
COLOR_GREEN = '32'
COLOR_YELLOW = '33'
COLOR_BLUE = '34'
COLOR_MAGENTA = '35'
COLOR_CYAN = '36'
COLOR_WHITE = '37'
COLOR_CRIMSON = '38'


def colorize(msg, color):
    """
    Wrap a message in shell color codes.
    """
    return '\033[1;{0}m{1}\033[1;m'.format(color, msg)


def uncolorize(msg):
    """
    Strip shell color codes from a string
    """
    code = '\033[1;'
    if msg.find(code) >= 0:
        msg = msg.replace(code, '')[3:-1]
    return msg


def ensure_commands_exist(commands, exit_code=1):
    """
    Ensure that all commands given by an iterable exist, otherwise exit.
    """
    commands_exist = map(command_available, commands)
    if not all(commands_exist):
        sys.exit(exit_code)
    return None


def command_available(command, print_error=True):
    """
    See if a command exists by trying to call it with --help.
    """
    try:
        subprocess.call(
            [command, '--help'],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
        )
        return True
    except OSError as e:
        if print_error:
            print('You must install {0}.'.format(command))
        return False


class PPTable(object):
    """
    Pretty-print a table.
    """
    def __init__(self, cols, data=None):
        self.cols = cols
        self.data = data or []

    def add_data(self, data):
        self.data.append(data)

    def _col_width(self, col_index):
        max_data_width = max([len(uncolorize(d[col_index])) for d in self.data])
        return max(len(self.cols[col_index]), max_data_width)

    @property
    def header(self):
        h = []
        for i, col in enumerate(self.cols):
            width = self._col_width(i)
            h.append('{0:<{width}}'.format(col, width=width))
        return ' | '.join([''] + h + ['']).strip()

    @property
    def rows(self):
        lines = []
        for row in self.data:
            r = []
            for i, value in enumerate(row):
                width = self._col_width(i)
                if len(value) > width:
                    width = len(value) + (width - len(uncolorize(value)))
                val = '{0:<{width}}'.format(value, width=width)
                r.append(val)
            lines.append(' | '.join([''] + r + ['']).strip())
        return '\n'.join(lines)

    def __str__(self):
        horizontal_border = '+' + '-'*(len(self.header)-2) + '+'
        return '\n'.join([
            horizontal_border,
{'fraction_non_alphanumeric': 0.14552025870268215, 'fraction_numerical': 0.015408027392048697, 'mean_word_length': 2.194410692588092, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5409936', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1687, 'n_words': 415}