Clean up Main.gd, improve CLI and rename 'cli' command to 'apply' - implements #48

This commit is contained in:
ChaoticByte 2025-01-31 19:50:53 +01:00
parent fee1c64775
commit 168cb036be
No known key found for this signature in database

View file

@ -11,14 +11,19 @@ const BATCH_MODE_SUPPORTED_EXTS = [
func show_help(): func show_help():
print( print(
"Usage:\n\n", "Usage:\n\n",
"./Fragmented cmd --shader PATH [--load-image PATH]\n\n", "./Fragmented <command> <args...>\n\n",
" --shader PATH The path to the shader\n", "Commands:\n\n",
" --output PATH Where to write the resulting image to.\n", " help\n\n",
" In batch mode, this must be a folder.\n", " | Shows this help text.\n\n",
" --load-image PATH The path to the image. This will overwrite the\n", " apply --shader PATH [--load-image PATH]\n\n",
" load directive of the shader file.\n", " | Applies a shader file.\n\n",
" Passing a folder activates batch mode.\n", " --shader PATH The path to the shader\n",
" (optional)\n") " --output PATH Where to write the resulting image to.\n",
" In batch mode, this must be a folder.\n",
" --load-image PATH The path to the image. This will overwrite the\n",
" load directive of the shader file.\n",
" Passing a folder activates batch mode.\n",
" (optional)\n")
func parse_custom_cmdline(args: PackedStringArray): func parse_custom_cmdline(args: PackedStringArray):
var kwargs: Dictionary = {"--shader": null, "--output": null, "--load-image": null} var kwargs: Dictionary = {"--shader": null, "--output": null, "--load-image": null}
@ -41,78 +46,88 @@ func cli_handle_errors(errors: Array) -> int:
printerr(e) printerr(e)
return n_errors return n_errors
func _ready(): func cli(args: PackedStringArray):
var args = OS.get_cmdline_args() print(
if "cmd" in args: # commandline interface "~ Fragmented CLI ~\n",
if "help" in args: "-================-\n")
show_help() if "help" in args:
get_tree().quit(1) show_help()
return get_tree().quit(1)
var kwargs: Dictionary = parse_custom_cmdline(args) return
if kwargs["--shader"] == null or kwargs["--output"] == null: var kwargs: Dictionary = parse_custom_cmdline(args)
show_help() if kwargs["--shader"] == null or kwargs["--output"] == null:
get_tree().quit(1) show_help()
return get_tree().quit(1)
var batch_mode = false return
var load_image_dir: DirAccess var batch_mode = false
if kwargs["--load-image"] != null: var load_image_dir: DirAccess
load_image_dir = DirAccess.open(kwargs["--load-image"]) if kwargs["--load-image"] != null:
if load_image_dir != null: load_image_dir = DirAccess.open(kwargs["--load-image"])
# batch mode if load_image_dir != null:
if DirAccess.open(kwargs["--output"]) == null: # batch mode
printerr("If --load-image is a directory, --output has to be one too.\n") if DirAccess.open(kwargs["--output"]) == null:
show_help() printerr("If --load-image is a directory, --output has to be one too.\n")
show_help()
get_tree().quit(1)
return
else:
batch_mode = true
#
Filesystem.load_shader(kwargs["--shader"])
#
if batch_mode:
var in_dir_path = load_image_dir.get_current_dir()
var out_dir_path: String = kwargs["--output"].rstrip("/")
for f in load_image_dir.get_files():
var supported = false
for e in BATCH_MODE_SUPPORTED_EXTS:
if f.ends_with(e):
supported = true
break
if supported:
f = in_dir_path + "/" + f
print(f)
var errors = await $Compositor.update(f)
if cli_handle_errors(errors) == 0:
var filename = out_dir_path + "/" + f.substr(f.rfind("/"), -1)
Filesystem.save_result(filename)
else:
get_tree().quit(1) get_tree().quit(1)
return return
else: get_tree().quit(0)
batch_mode = true else:
# var errors = []
Filesystem.load_shader(kwargs["--shader"]) if kwargs["--load-image"] == null:
# errors = await $Compositor.update()
if batch_mode: else:
var in_dir_path = load_image_dir.get_current_dir() errors = await $Compositor.update(kwargs["--load-image"])
var out_dir_path: String = kwargs["--output"].rstrip("/") if cli_handle_errors(errors) == 0:
for f in load_image_dir.get_files(): Filesystem.save_result(kwargs["--output"])
var supported = false
for e in BATCH_MODE_SUPPORTED_EXTS:
if f.ends_with(e):
supported = true
break
if supported:
f = in_dir_path + "/" + f
print(f)
var errors = await $Compositor.update(f)
if cli_handle_errors(errors) == 0:
var filename = out_dir_path + "/" + f.substr(f.rfind("/"), -1)
Filesystem.save_result(filename)
else:
get_tree().quit(1)
return
get_tree().quit(0) get_tree().quit(0)
else: else:
var errors = [] get_tree().quit(1)
if kwargs["--load-image"] == null:
errors = await $Compositor.update() func prepare_gui():
else: update_title()
errors = await $Compositor.update(kwargs["--load-image"]) # position windows
if cli_handle_errors(errors) == 0: get_window().position = Vector2i(
Filesystem.save_result(kwargs["--output"]) editor_window.position.x + editor_window.size.x + 50,
get_tree().quit(0) editor_window.position.y)
else: get_window().min_size = Vector2i(400, 400)
get_tree().quit(1) editor_window.min_size = Vector2i(560, 400)
editor_window.show()
# Load last opened file
Filesystem.remember_last_opened_file()
if Filesystem.last_shader_savepath != "":
ui_container.get_node("Editor")._on_open_shader_dialog_file_selected(Filesystem.last_shader_savepath)
func _ready():
var args = OS.get_cmdline_args()
if len(args) > 0 and args[0] in ["apply", "help"]:
# use the commandline interface
cli(args)
else: else:
update_title() prepare_gui()
# position windows
get_window().position = Vector2i(
editor_window.position.x + editor_window.size.x + 50,
editor_window.position.y)
get_window().min_size = Vector2i(400, 400)
editor_window.min_size = Vector2i(560, 400)
editor_window.show()
# Load last opened file
Filesystem.remember_last_opened_file()
if Filesystem.last_shader_savepath != "":
ui_container.get_node("Editor")._on_open_shader_dialog_file_selected(Filesystem.last_shader_savepath)
func update_title(current_file: String = ""): func update_title(current_file: String = ""):
if current_file == "": if current_file == "":