From 29d070c30550fd28063458ae6b4d0037862cd206 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sat, 5 Oct 2024 23:24:55 +0200 Subject: [PATCH] Fix scrolling in MessageBox, file overwrite confirmation --- src/Editor/Editor.php | 12 ++++++++++- src/Editor/MessageBox.php | 42 +++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index a31ecf6..eb8072b 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -402,6 +402,16 @@ class Editor if (!$saveTo) { return false; } + if (file_exists($saveTo)) { + $overwrite = (new Menu($this->term, [ + 'overwrite' => "Overwrite the file", + 'abort' => "Abort" + ], "The file exists"))->display(0, 0, 30, 0, ''); + if ($overwrite == 'abort') { + $this->redrawEditor(); + return false; + } + } $doc = $this->document->save(); @@ -958,7 +968,7 @@ Welcome to JSONEdit! The editor you never knew you were missing! # QuickStart -To get started, press I (shift-i) and add something to the document. Use the arrow keys to get around. To cancel a prompt, close a menu or close a dialog, press ctrl-C. When you are happy with your work, press ctrl-W and enter a filename to write. You can also press ctrl-R and read in a new file, overwriting your masterpiece. +To get started, press I (shift-i) and add something to the document. Use the arrow keys to get around. To cancel a prompt, close a menu or close a dialog, press ctrl-C. When you are happy with your work, press ctrl-W and enter a filename to write. You can also press ctrl-O to browse for a file to open. # Useful keys diff --git a/src/Editor/MessageBox.php b/src/Editor/MessageBox.php index 50f26cf..b4b1b77 100644 --- a/src/Editor/MessageBox.php +++ b/src/Editor/MessageBox.php @@ -20,7 +20,7 @@ class MessageBox const U_EDGE_HORIZONTAL = "\u{2500}"; const U_EDGE_VERTICAL = "\u{2502}"; - const U_EDGE_VERTICAL_SCROLL = "\u{2591}"; + const U_EDGE_VERTICAL_SCROLL = "\u{2593}"; const U_EDGE_VERTICAL_THUMB = "\u{2593}"; const U_EDGE_VERTICAL_LEFT = "\u{2524}"; const U_EDGE_VERTICAL_RIGHT = "\u{251c}"; @@ -76,14 +76,36 @@ class MessageBox $showing = false; } } else { - if ($ch == "k{UP}") { - $this->scroll--; - if ($this->scroll < 0) $this->scroll = 0; - $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); - } elseif ($ch == "k{DOWN}") { - $this->scroll++; - if ($this->scroll > $maxScroll) $this->scroll = $maxScroll; - $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + switch ($ch) { + case "k{UP}": + $this->scroll--; + if ($this->scroll < 0) $this->scroll = 0; + $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + break; + case "k{DOWN}": + $this->scroll++; + if ($this->scroll > $maxScroll) $this->scroll = $maxScroll; + $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + break; + case "k{PGUP}": + $this->scroll -= $height - 2; + if ($this->scroll < 0) $this->scroll = 0; + $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + break; + case "k{PGDN}": + $this->scroll += $height - 2; + if ($this->scroll >= $maxScroll) $this->scroll = $maxScroll; + $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + break; + case "k{HOME}": + $this->scroll = 0; + if ($this->scroll >= count($wrapped)) $this->scroll = 0; + $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + break; + case "k{END}": + $this->scroll = $maxScroll; + $this->redraw($left, $top, $width, $height, $wrapped, $this->title, $maxScroll); + break; } } } @@ -154,7 +176,7 @@ class MessageBox if ($n >= $thumbTop && $n <= $thumbBottom) { $scrollbar = "\e[97;1m".self::U_EDGE_VERTICAL_THUMB."\e[40;37;22m"; } else { - $scrollbar = "\e[37;2m".self::U_EDGE_VERTICAL_SCROLL."\e[40;37;22m"; + $scrollbar = "\e[30;2m".self::U_EDGE_VERTICAL_SCROLL."\e[40;37;22m"; } $this->terminal ->writeAt($left, $top + 3 + $n, self::U_EDGE_VERTICAL.$item.$scrollbar);