From 573f8c0cde2beb3d095d69eeab1608164e1d9401 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 17 Oct 2023 16:15:00 -0400 Subject: [PATCH] Add basic line numbers --- src/Editor.php | 17 ++++++++++++++++- src/Syntax.php | 11 +++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Editor.php b/src/Editor.php index c5b066e..da07928 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -13,6 +13,14 @@ use Aviat\Kilo\Type\TerminalSize; * // Don't highlight this! */ class Editor { + /** + * @var bool Whether to render line numbers + */ + public bool $showLineNumbers = true; + /** + * @var int The size of the line number Gutter in characters + */ + public int $numberGutter = 5; /** * @var string The screen buffer */ @@ -411,6 +419,11 @@ class Editor { $len = $this->terminalSize->cols; } + if ($this->showLineNumbers) + { + $this->outputBuffer .= sprintf("%-{$this->numberGutter}s", ($rowIdx+1)); + } + $chars = substr($row->render, $this->offset->x, (int)$len); $hl = array_slice($row->hl, $this->offset->x, (int)$len); @@ -544,10 +557,12 @@ class Editor { $this->drawStatusBar(); $this->drawMessageBar(); + $gutter = ($this->showLineNumbers) ? $this->numberGutter : 0; + // Specify the current cursor position $this->outputBuffer .= ANSI::moveCursor( $this->cursor->y - $this->offset->y, - $this->renderX - $this->offset->x + ($this->renderX - $this->offset->x) + $gutter, ); $this->outputBuffer .= ANSI::SHOW_CURSOR; diff --git a/src/Syntax.php b/src/Syntax.php index 48b4a62..4f40971 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -4,6 +4,9 @@ namespace Aviat\Kilo; use Aviat\Kilo\Enum\SyntaxFamily; +/** + * A representation of the syntax of a programming language + */ class Syntax { // Tokens for PHP files public array $tokens = []; @@ -46,10 +49,10 @@ class Syntax { public static function default(): self { return self::new( - 'No filetype', slcs: - '', mcs: - '', mce: - '', + 'No filetype', + slcs: '', + mcs: '', + mce: '', highlightNumbers: false, highlightStrings: false, hasCommonOperators: false,