<commit_before><?php

namespace Qissues\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Process\Process;

class QueryCommand extends Command
{
    protected $priorities = array(
        5 => '▲',
        4 => '▴',
        3 => '-',
        2 => '▾',
        1 => '▼'
    );
    protected $types = array(
        'bug' => '<p5>B</p5>'
    );

    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        $this
            ->setName('query')
            ->setDescription('List all issues, optionally filtering them.')
            ->addOption('view', 'z', InputOption::VALUE_OPTIONAL, 'View mode (tiny, basic or detailed) defaults based on width)', null)
            ->addOption('status', 's', InputOption::VALUE_OPTIONAL, 'Filter by status', "new,open")
            ->addOption('sort', 'o', InputOption::VALUE_OPTIONAL, 'Sort results by [priority]', null)
            ->addOption('assignee', 'a', InputOption::VALUE_OPTIONAL, 'Filter by assignee', null)
            ->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Filter by priority', null)
            ->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Filter by type', null)
            ->addOption('mine', null, InputOption::VALUE_NONE, 'Only show things assigned to me', null)
            ->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Limit the results', 50)
            ->addOption('web', 'w', InputOption::VALUE_NONE, 'Open in web browser.', null)
        ;
    }
    
    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $connector = $this->getApplication()->getConnector();

        if ($input->getOption('web')) {
            return exec(sprintf(
                'xdg-open %s',
                escapeshellarg($connector->getBrowseUrl())
            ));
        }


        list($width, $height) = $this->getApplication()->getTerminalDimensions();
        $issues = $connector->findAll($this->buildOptions($input));

        if (!$view = $input->getOption('view')) {
            if ($width > 150) {
                $view = 'detailed';
            } elseif ($width > 100) {
                $view = 'basic';
            } else {
                $view = 'tiny';
            }
        }

        if ($view == 'detailed') {
            return $this->renderDetailedView($issues, $output);
        }

        if ($view == 'basic') {
            return $this->renderBasicView($issues, $output);
        }

        return $this->renderTinyView($issues, $output);
    }

    /**
     * Renders a detailed table view of the issues
     *
     * @param array issues from connector
     * @param OutputInterface
     */
    protected function renderDetailedView(array $issues, OutputInterface $output)
    {
        list($width, $height) = $this->getApplication()->getTerminalDimensions();

        $renderIssues = array();
        foreach ($issues as $issue) {
            $renderIssues[] = array(
                'Id'           => $issue['id'],
                'Title'        => strlen($issue['title']) > $width * 0.4 
                    ? (substr($issue['title'], 0, $width * 0.4) . '...')
                    : $issue['title'],
                'Status'       => $issue['status'],
                'Type'         => $issue['type'],
                'Priority'     => $issue['priority_text'],
                'Assignee'     => $issue['assignee'],
                'Date Created' => $issue['created']->format('Y-m-d g:ia'),
                'Date updated' => $issue['updated']->format('Y-m-d g:ia'),
                'Comments'     => $issue['comments']
            );
        }

        $renderer = new \Qissues\Renderer\TableRenderer();
        $output->writeln($renderer->render($renderIssues, $width));
    }

    /**
     * Renders a basic view of the issues
     * XXX NOT DONE
     *
     * @param array issues from connector
     * @param OutputInterface
     */
    protected function renderBasicView(array $issues, OutputInterface $output)
    {
        list($width, $height) = $this->getApplication()->getTerminalDimensions();

        $maxLength = 0;
        foreach ($issues as $issue) {
            if (strlen($issue['local_id']) > $maxLength) {
                $maxLength = strlen($issue['local_id']);
            }
        }

        $allowedSize = $width
            - 4          // icons
            - $maxLength // number area
            - 1          // space
        ;

        foreach ($issues as $issue) {
            $output->writeln(sprintf(
                '%s %s <comment>%s%d</comment> <message>%s</message>',
                $this->priorities[$issue['priority']],
                $issue['type'] == 'bug' ? $this->types['bug'] : ' ',
                str_repeat(' ', $maxLength - strlen($issue['id'])),
                $issue['id'],
                strlen($issue['title']) > $allowedSize
                    ? (substr($issue['title'], 0, $allowedSize - 3) . '...')
                    : $issue['title']
            ));
        }
    }

    /**
     * Renders a tiny view for sidebars (ex in tmux)
     *
     * @param array issues from connector
     * @param OutputInterface
     */
    protected function renderTinyView(array $issues, OutputInterface $output)
    {
        list($width, $height) = $this->getApplication()->getTerminalDimensions();

        $maxLength = 0;
        foreach ($issues as $issue) {
            if (strlen($issue['local_id']) > $maxLength) {
                $maxLength = strlen($issue['local_id']);
            }
        }

        $allowedSize = $width
            - 4          // icons
            - $maxLength // number area
            - 1          // space
        ;

        foreach ($issues as $issue) {
            $output->writeln(sprintf(
                '%s %s <comment>%s%d</comment> <message>%s</message>',<commit_msg>basic view  uses table renderer with less columns<commit_after><?php

namespace Qissues\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Process\Process;

class QueryCommand extends Command
{
    protected $priorities = array(
        5 => '▲',
        4 => '▴',
        3 => '-',
        2 => '▾',
        1 => '▼'
    );
    protected $types = array(
        'bug' => '<p5>B</p5>'
    );

    /**
     * {@inheritDoc}
     */
    protected function configure()
    {
        $this
            ->setName('query')
            ->setDescription('List all issues, optionally filtering them.')
            ->addOption('view', 'z', InputOption::VALUE_OPTIONAL, 'View mode (tiny, basic or detailed) defaults based on width)', null)
            ->addOption('status', 's', InputOption::VALUE_OPTIONAL, 'Filter by status', "new,open")
            ->addOption('sort', 'o', InputOption::VALUE_OPTIONAL, 'Sort results by [priority]', null)
            ->addOption('assignee', 'a', InputOption::VALUE_OPTIONAL, 'Filter by assignee', null)
            ->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Filter by priority', null)
            ->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Filter by type', null)
            ->addOption('mine', null, InputOption::VALUE_NONE, 'Only show things assigned to me', null)
            ->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Limit the results', 50)
            ->addOption('web', 'w', InputOption::VALUE_NONE, 'Open in web browser.', null)
        ;
    }
    
    /**
     * {@inheritDoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $connector = $this->getApplication()->getConnector();

        if ($input->getOption('web')) {
            return exec(sprintf(
                'xdg-open %s',
                escapeshellarg($connector->getBrowseUrl())
            ));
        }


        list($width, $height) = $this->getApplication()->getTerminalDimensions();
        $issues = $connector->findAll($this->buildOptions($input));

        if (!$view = $input->getOption('view')) {
            if ($width > 150) {
                $view = 'detailed';
            } elseif ($width > 100) {
                $view = 'basic';
            } else {
                $view = 'tiny';
            }
        }

        if ($view == 'detailed') {
            return $this->renderDetailedView($issues, $output);
        }

        if ($view == 'basic') {
            return $this->renderBasicView($issues, $output);
        }

        return $this->renderTinyView($issues, $output);
    }

    /**
     * Renders a detailed table view of the issues
     *
     * @param array issues from connector
     * @param OutputInterface
     */
    protected function renderDetailedView(array $issues, OutputInterface $output)
    {
        list($width, $height) = $this->getApplication()->getTerminalDimensions();

        $renderIssues = array();
        foreach ($issues as $issue) {
            $renderIssues[] = array(
                'Id'           => $issue['id'],
                'Title'        => strlen($issue['title']) > $width * 0.4 
                    ? (substr($issue['title'], 0, $width * 0.4) . '...')
                    : $issue['title'],
                'Status'       => $issue['status'],
                'Type'         => $issue['type'],
                'Priority'     => $issue['priority_text'],
                'Assignee'     => $issue['assignee'],
                'Date Created' => $issue['created']->format('Y-m-d g:ia'),
                'Date updated' => $issue['updated']->format('Y-m-d g:ia'),
                'Comments'     => $issue['comments']
            );
        }

        $renderer = new \Qissues\Renderer\TableRenderer();
        $output->writeln($renderer->render($renderIssues, $width));
    }

    /**
     * Renders a basic view of the issues
     *
     * @param array issues from connector
     * @param OutputInterface
     */
    protected function renderBasicView(array $issues, OutputInterface $output)
    {
        list($width, $height) = $this->getApplication()->getTerminalDimensions();

        $renderIssues = array();
        foreach ($issues as $issue) {
            $renderIssues[] = array(
                'Id'           => $issue['id'],
                'Title'        => strlen($issue['title']) > $width * 0.4 
                    ? (substr($issue['title'], 0, $width * 0.4) . '...')
                    : $issue['title'],
                'Status'       => $issue['status'],
                'Type'         => $issue['type'],
                'P'     => $issue['priority'],
                'Date updated' => $issue['updated']->format('Y-m-d g:ia')
            );
        }

        $renderer = new \Qissues\Renderer\TableRenderer();
        $output->writeln($renderer->render($renderIssues, $width));
    }

    /**
     * Renders a tiny view for sidebars (ex in tmux)
     *
     * @param array issues from connector
     * @param OutputInterface
     */
    protected function renderTinyView(array $issues, OutputInterface $output)
    {
        list($width, $height) = $this->getApplication()->getTerminalDimensions();

        $maxLength = 0;
        foreach ($issues as $issue) {
            if (strlen($issue['local_id']) > $maxLength) {
                $maxLength = strlen($issue['local_id']);
            }
        }

        $allowedSize = $width
            - 4          // icons
            - $maxLength // number area
            - 1          // space
        ;

        foreach ($issues as $issue) {
            $output->writeln(sprintf(
                '%s %s <comment>%s%d</comment> <message>%s</message>',
{'fraction_non_alphanumeric': 0.15882009832513957, 'fraction_numerical': 0.004666277810182484, 'mean_word_length': 1.9166464155528553, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 140, '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': '3382071', 'n_tokens_mistral': 3406, 'n_tokens_neox': 3299, 'n_words': 723}