Add basic line numbers
timw4mail/php-kilo/pipeline/head There was a failure building this commit Details

This commit is contained in:
Timothy Warren 2023-10-17 16:15:00 -04:00
parent 7554c69d63
commit 573f8c0cde
2 changed files with 23 additions and 5 deletions

View File

@ -13,6 +13,14 @@ use Aviat\Kilo\Type\TerminalSize;
* // Don't highlight this! * // Don't highlight this!
*/ */
class Editor { 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 * @var string The screen buffer
*/ */
@ -411,6 +419,11 @@ class Editor {
$len = $this->terminalSize->cols; $len = $this->terminalSize->cols;
} }
if ($this->showLineNumbers)
{
$this->outputBuffer .= sprintf("%-{$this->numberGutter}s", ($rowIdx+1));
}
$chars = substr($row->render, $this->offset->x, (int)$len); $chars = substr($row->render, $this->offset->x, (int)$len);
$hl = array_slice($row->hl, $this->offset->x, (int)$len); $hl = array_slice($row->hl, $this->offset->x, (int)$len);
@ -544,10 +557,12 @@ class Editor {
$this->drawStatusBar(); $this->drawStatusBar();
$this->drawMessageBar(); $this->drawMessageBar();
$gutter = ($this->showLineNumbers) ? $this->numberGutter : 0;
// Specify the current cursor position // Specify the current cursor position
$this->outputBuffer .= ANSI::moveCursor( $this->outputBuffer .= ANSI::moveCursor(
$this->cursor->y - $this->offset->y, $this->cursor->y - $this->offset->y,
$this->renderX - $this->offset->x ($this->renderX - $this->offset->x) + $gutter,
); );
$this->outputBuffer .= ANSI::SHOW_CURSOR; $this->outputBuffer .= ANSI::SHOW_CURSOR;

View File

@ -4,6 +4,9 @@ namespace Aviat\Kilo;
use Aviat\Kilo\Enum\SyntaxFamily; use Aviat\Kilo\Enum\SyntaxFamily;
/**
* A representation of the syntax of a programming language
*/
class Syntax { class Syntax {
// Tokens for PHP files // Tokens for PHP files
public array $tokens = []; public array $tokens = [];
@ -46,10 +49,10 @@ class Syntax {
public static function default(): self public static function default(): self
{ {
return self::new( return self::new(
'No filetype', slcs: 'No filetype',
'', mcs: slcs: '',
'', mce: mcs: '',
'', mce: '',
highlightNumbers: false, highlightNumbers: false,
highlightStrings: false, highlightStrings: false,
hasCommonOperators: false, hasCommonOperators: false,