<commit_before><?php

namespace DalaiLomo\ACE\Command;

use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;

class SetupCommand extends ACECommand
{
    const OPTION_LIST_CHUNKS = 'l';
    const OPTION_EDIT_CONFIG = 'e';
    const OPTION_BACK = 'b';
    const OPTION_QUIT = 'q';

    private $choices = [
        'main_menu' => [
            self::OPTION_LIST_CHUNKS => 'List all command chunks',
            self::OPTION_EDIT_CONFIG => 'Edit configuration',
            self::OPTION_QUIT => 'Quit',
        ],
        'edit_config' => [
            self::OPTION_BACK => 'Back',
        ]
    ];

    private $config = [];

    protected function configure()
    {
        $this
            ->setName('ace:setup')
            ->setDescription('ACE interactive configuration');
    }

    protected function doExecute()
    {
        $this->config = Yaml::parse(file_get_contents('config.yml'));
        $this->clearOutputAndShowHeader();

        $menuChoice = $this->mainMenu();

        $this->clearOutputAndShowHeader();

        switch ($menuChoice) {
            case self::OPTION_LIST_CHUNKS:
                $this->sectionListCommandChunks();
                $this->doExecute();
                break;
            case self::OPTION_EDIT_CONFIG:
                $this->sectionEditConfig();
                $this->doExecute();
                break;
            case self::OPTION_QUIT:
                $this->clearOutput();
                $this->output->writeln('Bye!');
                return 0;
        }

        return 0;
    }

    private function mainMenu()
    {
        $this->output->writeln("Main menu");
        $this->ninjaSeparator();

        $question = $this->getHelper('question');

        return $question->ask($this->input, $this->output, new ChoiceQuestion('<info>Select an option: </info>', $this->choices['main_menu']));
    }

    private function sectionListCommandChunks()
    {
        $this->output->writeln("Listing command chunks");

        foreach ($this->config['ace']['command-chunks'] as $chunkName => $chunk) {
            $this->ninjaSeparator();
            $this->output->writeln(sprintf('<fg=green>%s</>', $chunkName));
            $this->oldSchoolSeparator();
            $this->output->writeln(implode(PHP_EOL, $chunk));
            $this->ninjaSeparator();
        }

        $question = $this->getHelper('question');

        $question->ask($this->input, $this->output, new ChoiceQuestion('', $this->choices['edit_config']));
    }

    private function sectionEditConfig()
    {
        $process = new Process('vim config.yml');

        try {
            $process->setTty(true);
            $process->mustRun(function ($type, $buffer) {
                echo $buffer;
            });
        } catch (ProcessFailedException $e) {
            echo $e->getMessage();
        }
    }
}<commit_msg>use a while loop rather than recursive calls<commit_after><?php

namespace DalaiLomo\ACE\Command;

use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;

class SetupCommand extends ACECommand
{
    const OPTION_LIST_CHUNKS = 'l';
    const OPTION_EDIT_CONFIG = 'e';
    const OPTION_BACK = 'b';
    const OPTION_QUIT = 'q';

    private $choices = [
        'main_menu' => [
            self::OPTION_LIST_CHUNKS => 'List all command chunks',
            self::OPTION_EDIT_CONFIG => 'Edit configuration',
            self::OPTION_QUIT => 'Quit',
        ],
        'edit_config' => [
            self::OPTION_BACK => 'Back',
        ]
    ];

    private $config = [];

    protected function configure()
    {
        $this
            ->setName('ace:setup')
            ->setDescription('ACE interactive configuration');
    }

    protected function doExecute()
    {
        $this->config = Yaml::parse(file_get_contents('config.yml'));
        $this->clearOutputAndShowHeader();

        $menuChoice = $this->mainMenu();

        while ($menuChoice !== self::OPTION_QUIT) {
            $this->clearOutputAndShowHeader();

            switch ($menuChoice) {
                case self::OPTION_LIST_CHUNKS:
                    $this->sectionListCommandChunks();
                    break;
                case self::OPTION_EDIT_CONFIG:
                    $this->sectionEditConfig();
                    break;
            }

            $this->clearOutputAndShowHeader();
            $menuChoice = $this->mainMenu();
        }

        $this->clearOutput();
        $this->output->writeln('Bye!');
        return 0;
    }

    private function mainMenu()
    {
        $this->output->writeln("Main menu");
        $this->ninjaSeparator();

        $question = $this->getHelper('question');

        return $question->ask($this->input, $this->output, new ChoiceQuestion('<info>Select an option: </info>', $this->choices['main_menu']));
    }

    private function sectionListCommandChunks()
    {
        $this->output->writeln("Listing command chunks");

        foreach ($this->config['ace']['command-chunks'] as $chunkName => $chunk) {
            $this->ninjaSeparator();
            $this->output->writeln(sprintf('<fg=green>%s</>', $chunkName));
            $this->oldSchoolSeparator();
            $this->output->writeln(implode(PHP_EOL, $chunk));
            $this->ninjaSeparator();
        }

        $question = $this->getHelper('question');

        $question->ask($this->input, $this->output, new ChoiceQuestion('', $this->choices['edit_config']));
    }

    private function sectionEditConfig()
    {
        $process = new Process('vim config.yml');

        try {
            $process->setTty(true);
            $process->mustRun(function ($type, $buffer) {
                echo $buffer;
            });
        } catch (ProcessFailedException $e) {
            echo $e->getMessage();
        }

        $this->config = Yaml::parse(file_get_contents('config.yml'));
    }
}
{'fraction_non_alphanumeric': 0.1475543922787502, 'fraction_numerical': 0.000490757402257484, 'mean_word_length': 2.2297939778129954, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 104, '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': '5368416', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1698, 'n_words': 295}