Files
jsonedit/src/entry.php
Christopher Vagnetoft ca61374654 Add duplicate (^D), basic search, bugfixes
* Use ^D to duplicate array items
* Use / to search, matching keys will be highlighted
* Don't attempt to load non-existing files when passed on command line
2024-10-08 11:04:31 +02:00

78 lines
1.8 KiB
PHP

<?php
use NoccyLabs\JsonEdit\Settings;
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");
$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();
Settings::load(SETTINGS_FILE);
$editor = new NoccyLabs\JsonEdit\Editor\Editor($terminal);
if ($filename && file_exists($filename)) {
$editor->loadFile($filename);
} else {
$editor->loadDocument((object)[]);
}
if ($loadFileCollapsed) {
$editor->doCollapseAll();
}
set_exception_handler(function (\Throwable $t) use ($terminal) {
register_shutdown_function(function () use ($t, $terminal) {
$terminal->shutdown();
echo $t."\n";
});
});
$editor->run();