Skip to content

Remove terminal output and CLI #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Phiki](./art/banner.png)

Phiki is a syntax highlighter written in PHP. It uses TextMate grammar files and Visual Studio Code themes to generate syntax highlighted code for the web and terminal.
Phiki is a syntax highlighter written in PHP. It uses TextMate grammar files and Visual Studio Code themes to generate syntax highlighted code for the web.

The name and public API of Phiki is heavily inspired by [Shiki](https://shiki.style/), a package that does more or less the same thing in the JavaScript ecosystem. The actual implementation of the highlighter is inspired by the [`vscode-textmate`](https://github.com/microsoft/vscode-textmate) package which Shiki uses internally, but isn't a 1-1 translation since the internal APIs of Phiki differ to `vscode-textmate`.

Expand Down Expand Up @@ -113,26 +113,6 @@ $phiki = new Phiki($environment);
$phiki->codeToHtml('...', 'my-language', 'my-theme');
```

### Terminal Output

Phiki has support for generating output designed for use in the terminal. This is available through the `codeToTerminal()` method which accepts the same parameters as the `codeToHtml()` method.

```php
echo $phiki->codeToTerminal('echo "Hello, world"!', Grammar::Php, Theme::GithubDark);
```

![](./art/codeToTerminal.png)

### Binary

If you want to use Phiki to highlight a file in the terminal without writing any PHP code, you can use the `phiki` binary.

```sh
vendor/bin/phiki ./path/to/file --grammar php --theme github-dark
```

This will output the highlighted code from that file using the grammar and theme provided.

### Line numbers

Each line has its own `<span>` element with a `data-line` attribute, so you can use CSS to display line numbers in the generated HTML. The benefit to this approach is that the text isn't selectable so you code snippets can be highlighted the same as before.
Expand Down
Binary file removed art/codeToTerminal.png
Binary file not shown.
107 changes: 0 additions & 107 deletions bin/phiki

This file was deleted.

37 changes: 0 additions & 37 deletions src/Generators/TerminalGenerator.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Phiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Phiki\Environment\Environment;
use Phiki\Generators\HtmlGenerator;
use Phiki\Generators\TerminalGenerator;
use Phiki\Grammar\Grammar;
use Phiki\Support\Arr;
use Phiki\Support\Str;
Expand Down Expand Up @@ -55,14 +54,6 @@ public function codeToHighlightedTokens(string $code, string|Grammar $grammar, s
return $highlighter->highlight($tokens);
}

public function codeToTerminal(string $code, string|Grammar $grammar, string|Theme $theme): string
{
$tokens = $this->codeToHighlightedTokens($code, $grammar, $theme);
$generator = new TerminalGenerator($this->environment->resolveTheme($theme));

return $generator->generate($tokens);
}

/**
* @param bool $withGutter Include a gutter in the generated HTML. The gutter typically contains line numbers and helps provide context for the code.
* @param bool $withWrapper Wrap the generated HTML in an additional `<div>` so that it can be styled with CSS. Useful for avoiding overflow issues.
Expand Down
32 changes: 0 additions & 32 deletions src/Theme/TokenSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,6 @@ public function __construct(
public ?string $fontStyle,
) {}

public function toAnsiEscape(): string
{
$codes = [];

if (isset($this->background)) {
$codes[] = Color::hexToAnsi($this->background) + 10;
}

if (isset($this->foreground)) {
$codes[] = Color::hexToAnsi($this->foreground);
}

$fontStyles = explode(' ', $this->fontStyle ?? '');
$decorations = [];

foreach ($fontStyles as $fontStyle) {
if ($fontStyle === 'underline') {
$decorations[] = Color::ANSI_UNDERLINE;
}

if ($fontStyle === 'italic') {
$decorations[] = Color::ANSI_ITALIC;
}

if ($fontStyle === 'bold') {
$decorations[] = Color::ANSI_BOLD;
}
}

return "\033[".implode(';', $decorations).';38;5;'.implode(';', $codes).'m';
}

public function toCssVarString(string $prefix): string
{
$styles = $this->toStyleArray();
Expand Down