<commit_before><?php

namespace Webgraphe\Phlip\Operation\Repl;

use Closure;
use Webgraphe\Phlip\Contracts\ContextContract;
use Webgraphe\Phlip\FormCollection\ProperList;
use Webgraphe\Phlip\Operation\PrimaryOperation;

class ReadOperation extends PrimaryOperation
{
    /** @var string */
    const IDENTIFIER = 'read';

    /** @var Closure */
    private $prompt;
    /** @var bool */
    private $multiLine = false;

    public function __construct(Closure $prompt = null)
    {
        $this->prompt = $prompt ?? static::readPrompt();
    }

    protected static function readPrompt(): Closure
    {
        return function (ContextContract $context) {
            static $lastTicks;
            $ticks = null === $lastTicks
                ? 0
                : max(0, $context->getTicks() - $lastTicks - 6);
            $lastTicks = $context->getTicks();

            return sprintf('[%d] >>> ', $ticks);
        };
    }

    /**
     * @param Closure|null $prompt
     * @return static
     */
    public static function multiLine(Closure $prompt = null): self
    {
        $self = new static($prompt);
        $self->multiLine = true;

        return $self;
    }

    /**
     * @return string[]
     */
    public function getIdentifiers(): array
    {
        return [self::IDENTIFIER];
    }

    /**
     * @return bool
     */
    public function isMultiLine(): bool
    {
        return $this->multiLine;
    }

    /**
     * @return Closure
     */
    public function getPrompt(): Closure
    {
        return $this->prompt;
    }

    /**
     * @param ContextContract $context
     * @param ProperList $forms
     * @return string
     */
    protected function invoke(ContextContract $context, ProperList $forms): string
    {
        $lines = [];
        while (true) {
            $prompt = $lines ? '' : $this->prompt;
            $line = rtrim(
                readline(
                    is_callable($prompt)
                        ? call_user_func($prompt, $context)
                        : $prompt
                )
            );
            $break = $this->multiLine && !$line
                || !$this->multiLine && '\\' !== $line[strlen($line) - 1];
            if ($line) {
                $lines[] = rtrim($line, '\\');<commit_msg>Fixed case where line is empty<commit_after><?php

namespace Webgraphe\Phlip\Operation\Repl;

use Closure;
use Webgraphe\Phlip\Contracts\ContextContract;
use Webgraphe\Phlip\FormCollection\ProperList;
use Webgraphe\Phlip\Operation\PrimaryOperation;

class ReadOperation extends PrimaryOperation
{
    /** @var string */
    const IDENTIFIER = 'read';

    /** @var Closure */
    private $prompt;
    /** @var bool */
    private $multiLine = false;

    public function __construct(Closure $prompt = null)
    {
        $this->prompt = $prompt ?? static::readPrompt();
    }

    protected static function readPrompt(): Closure
    {
        return function (ContextContract $context) {
            static $lastTicks;
            $ticks = null === $lastTicks
                ? 0
                : max(0, $context->getTicks() - $lastTicks - 6);
            $lastTicks = $context->getTicks();

            return sprintf('[%d] >>> ', $ticks);
        };
    }

    /**
     * @param Closure|null $prompt
     * @return static
     */
    public static function multiLine(Closure $prompt = null): self
    {
        $self = new static($prompt);
        $self->multiLine = true;

        return $self;
    }

    /**
     * @return string[]
     */
    public function getIdentifiers(): array
    {
        return [self::IDENTIFIER];
    }

    /**
     * @return bool
     */
    public function isMultiLine(): bool
    {
        return $this->multiLine;
    }

    /**
     * @return Closure
     */
    public function getPrompt(): Closure
    {
        return $this->prompt;
    }

    /**
     * @param ContextContract $context
     * @param ProperList $forms
     * @return string
     */
    protected function invoke(ContextContract $context, ProperList $forms): string
    {
        $lines = [];
        while (true) {
            $prompt = $lines ? '' : $this->prompt;
            $line = rtrim(
                readline(
                    is_callable($prompt)
                        ? call_user_func($prompt, $context)
                        : $prompt
                )
            );
            $break = $this->multiLine && !$line
                || !$this->multiLine && $line && '\\' !== $line[strlen($line) - 1];
            if ($line) {
                $lines[] = rtrim($line, '\\');
{'fraction_non_alphanumeric': 0.13620463489287277, 'fraction_numerical': 0.0017490161783996502, 'mean_word_length': 1.7560240963855422, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '5155224', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1308, 'n_words': 290}