Files
jsonedit/src/entry.php

78 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2024-10-01 18:46:03 +02:00
<?php
use NoccyLabs\JsonEdit\Settings;
2024-10-02 00:53:11 +02:00
2024-10-01 18:46:03 +02:00
require_once __DIR__."/../vendor/autoload.php";
2024-10-05 16:34:49 +02:00
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");
2024-10-02 00:53:11 +02:00
define("SETTINGS_FILE", getenv("HOME")."/.config/jsonedit/config.json");
2024-10-05 16:34:49 +02:00
$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;
2024-10-01 18:46:03 +02:00
$terminal = new NoccyLabs\JsonEdit\Terminal\Terminal();
2024-10-01 18:46:03 +02:00
2024-10-02 00:53:11 +02:00
Settings::load(SETTINGS_FILE);
$editor = new NoccyLabs\JsonEdit\Editor\Editor($terminal);
if ($filename && file_exists($filename)) {
2024-10-01 18:46:03 +02:00
$editor->loadFile($filename);
} else {
$editor->loadDocument((object)[]);
}
2024-10-05 16:34:49 +02:00
if ($loadFileCollapsed) {
$editor->doCollapseAll();
}
2024-10-01 18:46:03 +02:00
set_exception_handler(function (\Throwable $t) use ($terminal) {
register_shutdown_function(function () use ($t, $terminal) {
$terminal->shutdown();
echo $t."\n";
});
});
$editor->run();