|
| 1 | +# Copyright (C) 2025 Gil Barbosa Reis. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 | +# this software and associated documentation files (the “Software”), to deal in |
| 5 | +# the Software without restriction, including without limitation the rights to |
| 6 | +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 | +# of the Software, and to permit persons to whom the Software is furnished to do |
| 8 | +# so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +@tool |
| 22 | +extends Node |
| 23 | + |
| 24 | +@onready var _output: RichTextLabel = $Output |
| 25 | +@onready var _input: LineEdit = $Footer/Input |
| 26 | +@onready var _history_popup: PopupMenu = $Header/HistoryButton.get_popup() |
| 27 | +var _lua: LuaState |
| 28 | +var _history = PackedStringArray() |
| 29 | +var _current_history = 0 |
| 30 | + |
| 31 | + |
| 32 | +func _ready(): |
| 33 | + _history_popup.about_to_popup.connect(_on_history_popup_about_to_popup) |
| 34 | + _history_popup.id_pressed.connect(_on_history_popup_id_pressed) |
| 35 | + reset() |
| 36 | + |
| 37 | + |
| 38 | +func reset(): |
| 39 | + _lua = LuaState.new() |
| 40 | + _lua.open_libraries() |
| 41 | + _lua.registry.print = _printn |
| 42 | + _lua.do_string(r"print = function(...) debug.getregistry().print(table.concat({...}, '\t')) end") |
| 43 | + |
| 44 | + _history.clear() |
| 45 | + _current_history = 0 |
| 46 | + clear() |
| 47 | + |
| 48 | + |
| 49 | +func do_string(text: String): |
| 50 | + text = text.strip_edges() |
| 51 | + if text.is_empty(): |
| 52 | + return |
| 53 | + |
| 54 | + _history.append(text) |
| 55 | + _current_history = _history.size() |
| 56 | + _input.clear() |
| 57 | + _printn(text) |
| 58 | + |
| 59 | + # support for "= value" idiom from Lua 5.1 REPL |
| 60 | + text.trim_prefix("=") |
| 61 | + |
| 62 | + var result = _lua.do_string("return " + text) |
| 63 | + if result is LuaError: |
| 64 | + result = _lua.do_string(text) |
| 65 | + |
| 66 | + if result is LuaError: |
| 67 | + _print_error(result.message) |
| 68 | + else: |
| 69 | + _printn("Out[%d]: %s" % [_current_history, result]) |
| 70 | + _prompt() |
| 71 | + |
| 72 | + |
| 73 | +func clear(): |
| 74 | + _output.clear() |
| 75 | + _prompt() |
| 76 | + |
| 77 | + |
| 78 | +func set_history(index: int): |
| 79 | + if index < 0 or index >= _history.size(): |
| 80 | + return |
| 81 | + |
| 82 | + _current_history = index |
| 83 | + var text = _history[index] |
| 84 | + _input.text = text |
| 85 | + _input.caret_column = text.length() |
| 86 | + |
| 87 | + |
| 88 | +func _prompt(): |
| 89 | + _print("\nIn [%d]: " % [_current_history + 1]) |
| 90 | + |
| 91 | + |
| 92 | +func _print(msg: String): |
| 93 | + self._output.add_text(msg) |
| 94 | + |
| 95 | + |
| 96 | +func _printn(msg: String): |
| 97 | + _print(msg) |
| 98 | + _print("\n") |
| 99 | + |
| 100 | + |
| 101 | +func _print_error(msg: String): |
| 102 | + var color: Color = EditorInterface.get_editor_settings().get_setting("text_editor/theme/highlighting/brace_mismatch_color") |
| 103 | + self._output.append_text("[color=%s]%s[/color]\n" % [color.to_html(), msg.replace("[", "[lb]")]) |
| 104 | + |
| 105 | + |
| 106 | +func _on_history_popup_about_to_popup(): |
| 107 | + _history_popup.clear() |
| 108 | + for line in _history: |
| 109 | + _history_popup.add_item(line) |
| 110 | + |
| 111 | + |
| 112 | +func _on_history_popup_id_pressed(id: int): |
| 113 | + set_history(id) |
| 114 | + |
| 115 | + |
| 116 | +func _on_input_text_submitted(new_text: String): |
| 117 | + do_string(new_text) |
| 118 | + |
| 119 | + |
| 120 | +func _on_run_button_pressed(): |
| 121 | + do_string(_input.text) |
| 122 | + |
| 123 | + |
| 124 | +func _on_input_gui_input(event: InputEvent): |
| 125 | + var key_event = event as InputEventKey |
| 126 | + if not key_event or not key_event.pressed: |
| 127 | + return |
| 128 | + |
| 129 | + if key_event.keycode == KEY_UP: |
| 130 | + set_history(_current_history - 1) |
| 131 | + elif key_event.keycode == KEY_DOWN: |
| 132 | + set_history(_current_history + 1) |
0 commit comments