diff --git a/src/Editor.php b/src/Editor.php index 90b5529..e8fdbba 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -71,6 +71,11 @@ class Row { } } + public function __toString(): string + { + return $this->chars . "\n"; + } + public function update(): void { $idx = 0; @@ -393,33 +398,35 @@ class Editor { protected function rowsToString(): string { - $str = ''; + $lines = []; foreach ($this->rows as $row) { - $str .= $row->chars . "\n"; + $lines[] = (string)$row; } - return $str; + return implode('', $lines); } public function open(string $filename): void { + // Copy filename for display + $this->filename = $filename; + // Determine the full path to the file - $baseFile = basename($filename); + /* $baseFile = basename($filename); $basePath = str_replace($baseFile, '', $filename); $path = (is_dir($basePath)) ? $basePath : getcwd(); - $fullname = $path . '/' . $baseFile; - - // Copy filename for display - $this->filename = $fullname; + $fullname = $path . '/' . $baseFile; */ // #TODO gracefully handle issues with loading a file - $handle = fopen($fullname, 'rb'); + $handle = fopen($filename, 'rb'); if ($handle === FALSE) { + write_stdout("\x1b[2J"); // Clear the screen + write_stdout("\x1b[H"); // Reposition cursor to top-left disableRawMode(); - print_r(error_clear_last()); + print_r(error_get_last()); die(); } @@ -438,12 +445,14 @@ class Editor { { if ($this->filename === '') { - $this->filename = $this->prompt('Save as: %s'); - if ($this->filename === '') + $newFilename = $this->prompt('Save as: %s'); + if ($newFilename === '') { $this->setStatusMessage('Save aborted'); return; } + + $this->filename = $newFilename; } $contents = $this->rowsToString();