Add save to json/yaml (ctrl-w)
This commit is contained in:
@ -204,15 +204,78 @@ class Editor
|
||||
break;
|
||||
|
||||
case 'Q':
|
||||
case "\x03":
|
||||
case "\x03": // ctrl-w
|
||||
$this->running = false;
|
||||
break;
|
||||
|
||||
case "\x12": // ctrl-r
|
||||
$readFrom = $this->ask("\e[33mRead from:\e[0m ", "");
|
||||
|
||||
$this->term->setCursor(1, $h);
|
||||
if (!file_exists($readFrom)) {
|
||||
echo "\e[97;41mFile does not exist\e[K\e[0m";
|
||||
break;
|
||||
}
|
||||
if (!is_readable($readFrom)) {
|
||||
echo "\e[97;41mFile not readable\e[K\e[0m";
|
||||
break;
|
||||
}
|
||||
$ext = strtolower(pathinfo($readFrom, PATHINFO_EXTENSION));
|
||||
switch ($ext) {
|
||||
case 'json':
|
||||
$doc = json_decode(file_get_contents($readFrom));
|
||||
break;
|
||||
case 'yml':
|
||||
case 'yaml':
|
||||
$doc = Yaml::parseFile($readFrom);
|
||||
break;
|
||||
default:
|
||||
echo "\e[97;41mUnable to read format: {$ext}\e[K\e[0m";
|
||||
break(2);
|
||||
}
|
||||
|
||||
$this->filename = $readFrom;
|
||||
$this->shortfilename = basename($readFrom);
|
||||
|
||||
$this->document->load($doc);
|
||||
$this->currentRow = 0;
|
||||
$this->list->parseTree();
|
||||
$this->redrawEditor();
|
||||
|
||||
$this->term->setCursor(1, $h);
|
||||
echo "\e[97;42mLoaded {$readFrom}\e[K\e[0m";
|
||||
break;
|
||||
|
||||
|
||||
case "\x17": // ctrl-w
|
||||
$saveTo = $this->ask("\e[33mWrite to:\e[0m ", $this->filename);
|
||||
|
||||
$doc = $this->document->save();
|
||||
|
||||
$this->term->setCursor(1, $h);
|
||||
|
||||
$ext = strtolower(pathinfo($saveTo, PATHINFO_EXTENSION));
|
||||
switch ($ext) {
|
||||
case 'json':
|
||||
file_put_contents($saveTo, json_encode($doc, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)."\n");
|
||||
break;
|
||||
case 'yml':
|
||||
case 'yaml':
|
||||
$doc = json_decode(json_encode($doc), true);
|
||||
file_put_contents($saveTo, Yaml::dump($doc));
|
||||
break;
|
||||
default:
|
||||
echo "\e[97;41mUnable to write format: {$ext}\e[K\e[0m";
|
||||
}
|
||||
|
||||
echo "\e[97;42mWrote to {$saveTo}\e[K\e[0m";
|
||||
break;
|
||||
|
||||
case null:
|
||||
break;
|
||||
default:
|
||||
$this->term->setCursor(1, $h, true);
|
||||
echo $read;
|
||||
echo sprintf("%s %02x %03d", ctype_print($read)?$read:'.', ord($read), ord($read));
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
@ -248,10 +311,11 @@ class Editor
|
||||
return $value;
|
||||
}
|
||||
if (ord($ch) == 3) {
|
||||
// ctrl-c
|
||||
$this->term->setCursor(1, $h);
|
||||
echo "\e[0m\e[K";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} elseif (ord($ch) == 127) {
|
||||
if ($cursorPos > 0) {
|
||||
$value = substr($value, 0, $cursorPos - 1) . substr($value, $cursorPos);
|
||||
|
Reference in New Issue
Block a user