<commit_before><?php
namespace Kafoso\SvnNumber\SvnAction;

use Kafoso\SvnNumber\Bash\Styling as BashStyling;

class Diff extends AbstractSvnAction {
    public function getOutputForFilePaths(array $filePaths) {
        $diff = array();
        foreach ($filePaths as $filePath) {
            $cmd = "svn di " . escapeshellarg($filePath) . " " . $this->svnNumber->getAdditionalArgsStr();
            $output = $this->svnNumber->exec($cmd);
            $diff[] = implode(PHP_EOL, $this->stylize($output));
        }
        return implode(PHP_EOL, $diff);
    }

    public function getOutputAll(){
        $cmd = "svn di " . $this->svnNumber->getAdditionalArgsStr();
        $output = $this->svnNumber->exec($cmd);
        return implode(PHP_EOL, $this->stylize($output));
    }

    protected function stylize(array $svnDiff){
        $lineRegexToColor = array(
            '/^(\=+(\s*))$/' => array(242, null),
            '/^(\+\+\+\s+.*)$/' => array(40, null),
            '/^(---\s+.*)$/' => array(160, null),
            '/^(\+.*)$/' => array(40, null),
            '/^(-.*)$/' => array(160, null),
            '/^(@@ .+ @@).*$/' => array(33, null),
            '/^(Index: .+)$/' => array(226, null)
        );
        $bashStyling = new BashStyling;
        foreach ($svnDiff as &$line) {
            foreach ($lineRegexToColor as $regex => $colors) {
                if (preg_match($regex, ltrim($line), $match)) {
                    list($foreground, $background) = $colors;
                    $count = 1;
                    $line = str_replace(
                        $match[1],
                        $bashStyling->normal($match[1], $foreground, $background, true),
                        $line,
                        $count
                    );
                    break;
                }
            }
        }
        return $svnDiff;
    }
}<commit_msg>Improved logic + used color constants<commit_after><?php
namespace Kafoso\SvnNumber\SvnAction;

use Kafoso\SvnNumber\Bash\Styling as BashStyling;

class Diff extends AbstractSvnAction {
    public function getOutputForFilePaths(array $filePaths) {
        $diff = array();
        $filePaths = array_map(function($filePath){
            return escapeshellarg($filePath);
        }, $filePaths);
        $cmd = sprintf(
            "svn diff %s %s",
            implode(" ", $filePaths),
            $this->svnNumber->getAdditionalArgsStr()
        );
        $diffLinesArray = $this->svnNumber->exec($cmd);
        return implode(PHP_EOL, $this->stylizeDiffLines($diffLinesArray));
    }

    public function getOutputAll(){
        $cmd = "svn diff " . $this->svnNumber->getAdditionalArgsStr();
        $diffLinesArray = $this->svnNumber->exec($cmd);
        return implode(PHP_EOL . PHP_EOL, $this->stylizeDiffLines($diffLinesArray));
    }

    protected function stylizeDiffLines(array $diffLinesArray){
        foreach ($diffLinesArray as &$line) {
            if (preg_match("/^Index: /", $line)) {
                $line = PHP_EOL . $line;
            }
            $line = $this->stylizeDiff($line);
        }
        return $diffLinesArray;
    }

    protected function stylizeDiff($diff){
        $lineRegexToColor = array(
            '/^(\=+(\s*))$/' => array(static::COLOR_CODE_GRAY, null),
            '/^(\+\+\+\s+.*)$/' => array(static::COLOR_CODE_GREEN, null),
            '/^(---\s+.*)$/' => array(static::COLOR_CODE_RED, null),
            '/^(\+.*)$/' => array(static::COLOR_CODE_GREEN, null),
            '/^(-.*)$/' => array(static::COLOR_CODE_RED, null),
            '/^(@@ .+ @@).*$/' => array(static::COLOR_CODE_TEAL, null),
            '/^(Index: .+)$/' => array(static::COLOR_CODE_YELLOW, null)
        );
        $bashStyling = new BashStyling;
        foreach ($lineRegexToColor as $regex => $colors) {
            if (preg_match($regex, ltrim($diff), $match)) {
                list($foreground, $background) = $colors;
                $count = 1;
                $diff = str_replace(
                    $match[1],
                    $bashStyling->normal($match[1], $foreground, $background, true),
                    $diff,
                    $count
                );
                break;
            }
        }
        return $diff;
    }
}
{'fraction_non_alphanumeric': 0.17230046948356809, 'fraction_numerical': 0.005633802816901409, 'mean_word_length': 1.8482620320855614, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '2131211', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1273, 'n_words': 226}