Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list option to man #12

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions addons/gdshell/commands/default_commands/man.gd
Original file line number Diff line number Diff line change
@@ -1,45 +1,70 @@
extends GDShellCommand


const LIST_FLAGS: Array[String] = ["l", "L", "list", "LIST"]
const SILENT_FLAGS: Array[String] = ["s", "S", "silent", "SILENT"]


func _init():
COMMAND_AUTO_ALIASES = {
"manual": "man",
"help": "man",
}


func _main(argv: Array, data) -> Dictionary:
func _main(argv: Array, _data) -> Dictionary:
if not argv.size() > 1:
output("What manual page do you want?\nFor example, try '%s %s'" % [COMMAND_NAME, COMMAND_NAME])
output("What manual page do you want? For example, try '[b]man man[/b]'\nTo see the list of all commands run '[b]man --list[/b]'")
return DEFAULT_COMMAND_RESULT

var command_db: GDShellCommandDB = _PARENT_PROCESS._PARENT_GDSHELL.command_db
var options: Dictionary = GDShellCommand.argv_parse_options(argv, true, false)

if LIST_FLAGS.any(func(option): return option in options): # If any LIST_FLAG is in options
output("[b][color=AQUAMARINE]Available GDShell commands:[/color][/b]")
for command_name in _PARENT_PROCESS._PARENT_GDSHELL.command_db.get_all_command_names():
output("[color=BISQUE]%s[/color]" % command_name)

if not argv.size() > options.keys().size() + 1:
return DEFAULT_COMMAND_RESULT

var command_name: String = argv[1]
var manual: String = ""
for i in range(1, argv.size()): # first non-option arg
if not (argv[i] as String).begins_with("-"):
manual = get_command_manual(argv[i])
break

if manual.is_empty():
return {"error": 1, "error_string": "No manual", "data": null}

if not SILENT_FLAGS.any(func(option): return option in options): # If NOT any LIST_FLAG is in options
var line: int = get_ui_handler_rich_text_label().get_line_count()
output(manual)
get_ui_handler_rich_text_label().call_deferred(&"scroll_to_line", line)
return {"data": manual}


func get_command_manual(command_name: String) -> String:
var command_db: GDShellCommandDB = _PARENT_PROCESS._PARENT_GDSHELL.command_db
# unalias the name
while true:
if not command_name in command_db._aliases:
break
command_name = command_db._aliases[command_name].split(" ", false)[0]

if not command_name in command_db._commands:
output("Command not found")
return {"error": 2, "error_string": "Command not found"}
output("Command '%s' not found" % command_name)
return ""

# It's checked if the command is in command_db, co it's ok unless someone inserts data manually
@warning_ignore("unsafe_cast")
var command: GDShellCommand = (
var command: GDShellCommand = (
ResourceLoader.load(command_db.get_command_path(command_name), "GDScript").new() as GDShellCommand
)

var manual: String = command._get_manual() if command else ""
command.queue_free()

if not "-s" in argv or "-silent" in argv:
var line: int = get_ui_handler_rich_text_label().get_line_count()
output(manual)
get_ui_handler_rich_text_label().call_deferred(&"scroll_to_line", line)

return {"data": manual}
return manual


func _get_manual() -> String:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func _main(argv: Array, _data) -> Dictionary:
_print_available_options(safe_to_edit_properties)
# Delete all option flags so that _edit_monitor_properties_with_options() does not have to deal with them
for option in OPTIONS_FLAGS:
@warning_ignore("return_value_discarded")
options.erase(option)

_edit_monitor_properties_with_options(monitor, safe_to_edit_properties, options)
Expand All @@ -86,7 +87,7 @@ func _get_monitor_overlay() -> Node:
if not ResourceLoader.exists(MONITOR_FILE_PATH):
return null # MonitorOverlay is not installed

@warning_ignore(unsafe_method_access, unsafe_cast)
@warning_ignore("unsafe_cast", "unsafe_method_access")
var monitor: Node = ResourceLoader.load(MONITOR_FILE_PATH, "GDScript").new() as Node
# Sets the name of the MonitorOverlay Node to make it clear that it belongs to and is managed by GDShell
monitor.name = StringName(MONITOR_NODE_NAME)
Expand All @@ -100,6 +101,7 @@ func _get_monitor_overlay() -> Node:

# returns a list of properties that are used for MonitorOverlay UI control
func _get_monitor_overlay_safe_to_edit_properties(monitor: Object) -> Array[Dictionary]:
@warning_ignore("unsafe_method_access")
return monitor.get_script().get_script_property_list().filter(
func(property):
return property["type"] != TYPE_NIL and property["name"][0] != "_"
Expand All @@ -118,7 +120,7 @@ func _print_available_options(safe_to_edit_properties: Array[Dictionary]) -> voi


func _edit_monitor_properties_with_options(monitor: Node, safe_to_edit_properties: Array[Dictionary], options: Dictionary) -> void:
var safe_to_edit_property_names: Array[String] = safe_to_edit_properties.map(func(property): return property["name"])
var safe_to_edit_property_names: Array = safe_to_edit_properties.map(func(property): return property["name"])

for option in options:
if option in safe_to_edit_property_names:
Expand All @@ -141,6 +143,9 @@ func _get_manual() -> String:
[b]DESCRIPTION[/b]
Uses Monitor Overlay plugin by @HungryProton to show various information about performance

[b]-o, --options[/b]
Displays all available MonitorOverlay options and their types.

[b]-option=value[/b]
Sets the option variable of MonitorOverlay node to the value.
All option values are parsed by str_to_var()
Expand Down