Add makefile, cli options

This commit is contained in:
Chris 2024-10-05 16:34:49 +02:00
parent 62c78fa14c
commit eb09b4698f
3 changed files with 76 additions and 17 deletions

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
.PHONY: jsonedit.phar
jsonedit.phar:
echo "<?php return [ 'version' => \"v$(shell git describe --tags)\", 'builddate' => \"$(shell LC_ALL=C date)\" ];" > src/build.php \
&& box compile \
&& rm src/build.php \
&& mv bin/jsonedit.phar .

View File

@ -261,21 +261,7 @@ class Editor
break; break;
case "-": case "-":
foreach ($this->list as $path => $entry) { $this->doCollapseAll();
$node = $entry->node;
if ($path == "/") {
if ($node instanceof CollapsibleNode) {
$node->collapse(false);
}
} else {
if ($node instanceof CollapsibleNode) {
$node->collapse(true);
}
}
}
$this->currentRow = 0;
$this->list->parseTree();
$this->redrawEditor();
break; break;
case "k{LEFT}": case "k{LEFT}":
@ -732,6 +718,25 @@ class Editor
} }
} }
public function doCollapseAll(): void
{
foreach ($this->list as $path => $entry) {
$node = $entry->node;
if ($path == "/") {
if ($node instanceof CollapsibleNode) {
$node->collapse(false);
}
} else {
if ($node instanceof CollapsibleNode) {
$node->collapse(true);
}
}
}
$this->currentRow = 0;
$this->list->parseTree();
$this->redrawEditor();
}
/** /**
* Ask for input * Ask for input
* *

View File

@ -4,9 +4,54 @@ use NoccyLabs\JsonEdit\Settings;
require_once __DIR__."/../vendor/autoload.php"; require_once __DIR__."/../vendor/autoload.php";
if (file_exists(__DIR__."/build.php")) {
$buildInfo = include __DIR__."/build.php";
} else {
$buildInfo = [];
}
define("APP_VERSION", $buildInfo['version']??"DEV");
define("APP_BUILDDATE", $buildInfo['builddate']??"now");
define("SETTINGS_FILE", getenv("HOME")."/.config/jsonedit/config.json"); define("SETTINGS_FILE", getenv("HOME")."/.config/jsonedit/config.json");
$filename = $argv[1]??null; $opts = getopt("hVc", [ "help", "version" ], $optind);
$args = array_slice($argv,$optind);
function showVersion() {
echo "JSONEdit ".APP_VERSION." (built: ".APP_BUILDDATE.")\n";
echo "(c) 2024, NoccyLabs. Licensed under GNU GPL version 3 or later\n";
}
function showHelp() {
global $argv;
$cmd = basename($argv[0]);
showVersion();
echo <<<ENDHELP
Usage:
$cmd [options] [filename]
Options:
-h,--help Show this help
-V,--version Print version and exit
-c When used with a filename, load the file collapsed.
ENDHELP;
}
if (isset($opts['h']) || isset($opts['help'])) {
showHelp();
exit(0);
}
if (isset($opts['V']) || isset($opts['version'])) {
showVersion();
exit(0);
}
$loadFileCollapsed = isset($opts['c']);
$filename = $args[0]??null;
$terminal = new NoccyLabs\JsonEdit\Terminal\Terminal(); $terminal = new NoccyLabs\JsonEdit\Terminal\Terminal();
@ -18,7 +63,9 @@ if ($filename) {
} else { } else {
$editor->loadDocument((object)[]); $editor->loadDocument((object)[]);
} }
if ($loadFileCollapsed) {
$editor->doCollapseAll();
}
set_exception_handler(function (\Throwable $t) use ($terminal) { set_exception_handler(function (\Throwable $t) use ($terminal) {
register_shutdown_function(function () use ($t, $terminal) { register_shutdown_function(function () use ($t, $terminal) {