/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $info = $this->fileInfo(); $num = $input->getOption('num'); $colors = new ConsoleColor(); $colors->addTheme('line_number', array('blue')); $highlighter = new Highlighter($colors); $contents = file_get_contents($info['file']); $output->page($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW); }
public function parse() { $c = new ConsoleColor(); $o = ''; $b = []; foreach ($this->attributes as $a => $v) { $a = Str::underscored($a); $b[] = $a; if (is_string($v)) { $o .= $c->apply($b, $v); $b = []; } } return $o; }
private function getDisabledConsoleColor() { $color = new ConsoleColor(); $color->addTheme(Highlighter::TOKEN_STRING, array('none')); $color->addTheme(Highlighter::TOKEN_COMMENT, array('none')); $color->addTheme(Highlighter::TOKEN_KEYWORD, array('none')); $color->addTheme(Highlighter::TOKEN_DEFAULT, array('none')); $color->addTheme(Highlighter::TOKEN_HTML, array('none')); $color->addTheme(Highlighter::ACTUAL_LINE_MARK, array('none')); $color->addTheme(Highlighter::LINE_NUMBER, array('none')); return $color; }
/** * Format the code represented by $reflector. * * @param \Reflector $reflector * * @return string formatted code */ public static function format(\Reflector $reflector) { if ($fileName = $reflector->getFileName()) { if (!is_file($fileName)) { throw new RuntimeException('Source code unavailable.'); } $file = file_get_contents($fileName); $start = $reflector->getStartLine(); $end = $reflector->getEndLine() - $start; $colors = new ConsoleColor(); $colors->addTheme('line_number', array('blue')); $highlighter = new Highlighter($colors); return $highlighter->getCodeSnippet($file, $start, 0, $end); // no need to escape this bad boy, since (for now) it's being output raw. // return OutputFormatter::escape(implode(PHP_EOL, $code)); return implode(PHP_EOL, $code); } else { throw new RuntimeException('Source code unavailable.'); } }
/** * @param array $lines * @param null|int $markLine * @return string * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException */ private function lineNumbers(array $lines, $markLine = null) { end($lines); $lineStrlen = strlen(key($lines) + 1); $snippet = ''; foreach ($lines as $i => $line) { if ($markLine !== null) { $snippet .= $markLine === $i + 1 ? $this->color->apply(self::ACTUAL_LINE_MARK, ' > ') : ' '; } $snippet .= $this->color->apply(self::LINE_NUMBER, str_pad($i + 1, $lineStrlen, ' ', STR_PAD_LEFT) . '| '); $snippet .= $line . PHP_EOL; } return $snippet; }
/** * @param string $string * @param string $type * @throws \JakubOnderka\PhpConsoleColor\InvalidStyleException */ public function write($string, $type = self::TYPE_DEFAULT) { if (!$this->colors instanceof \JakubOnderka\PhpConsoleColor\ConsoleColor) { parent::write($string, $type); } else { switch ($type) { case self::TYPE_OK: parent::write($this->colors->apply('bg_green', $string)); break; case self::TYPE_SKIP: parent::write($this->colors->apply('bg_yellow', $string)); break; case self::TYPE_ERROR: parent::write($this->colors->apply('bg_red', $string)); break; default: parent::write($string); } } }
/** * @param string $filePath * @param int $lineNumber * @param int $linesBefore * @param int $linesAfter * @return string */ protected function getColoredCodeSnippet($filePath, $lineNumber, $linesBefore = 2, $linesAfter = 2) { if (!class_exists('\\JakubOnderka\\PhpConsoleHighlighter\\Highlighter') || !class_exists('\\JakubOnderka\\PhpConsoleColor\\ConsoleColor')) { return $this->getCodeSnippet($filePath, $lineNumber, $linesBefore, $linesAfter); } $colors = new ConsoleColor(); $colors->setForceStyle(true); $highlighter = new Highlighter($colors); $fileContent = file_get_contents($filePath); return $highlighter->getCodeSnippet($fileContent, $lineNumber, $linesBefore, $linesAfter); }
/** * @param ProgrammingCollection $collection * @param string $channelName * @param bool $showFullProgramming show all programs for the day * @param int $maxLength * @return string */ public static function printChannel(ProgrammingCollection $collection, $channelName, $showFullProgramming = false, $maxLength = 100) { $consoleColor = new ConsoleColor(); $res = ''; $programming = $collection->getProgrammingsByChannel($channelName); $res .= $consoleColor->apply('blue', '==> ') . $consoleColor->apply('white', $programming->getChannelName() . $consoleColor->apply('blue', ' <==') . "\n"); $now = Carbon::now(); $nextTwoHours = Carbon::now()->addHours(2); foreach ($programming->getEvents() as $event) { $color = null; if ($event->starts_at->lte($now) && $event->ends_at->gte($now)) { $color = 'green'; } else { if (!$showFullProgramming && $event->starts_at->gte($now) && $event->starts_at->lte($nextTwoHours)) { $color = 'default'; } else { if ($showFullProgramming) { $color = 'default'; } } } if (!$color) { continue; } $title = $event->render(); if (mb_strlen($title) > $maxLength) { $title = mb_substr($title, 0, $maxLength - 2) . '..'; } $res .= $consoleColor->apply($color, $title); $res .= "\n"; } return $res; }