add more console commands; support shift-tab to navigate backwards
This commit is contained in:
parent
3b039c177b
commit
60e50e902f
2 changed files with 33 additions and 26 deletions
|
@ -26,10 +26,14 @@ export default class AutocompleteLine extends LineEdit {
|
|||
}
|
||||
|
||||
_input(event: InputEvent): void {
|
||||
if (this.has_focus() && this.suppress_focus_change && event.is_action_pressed(Action.FocusNext)) {
|
||||
this.accept_event()
|
||||
this.autocomplete()
|
||||
}
|
||||
if (this.has_focus() && this.suppress_focus_change)
|
||||
if (event.is_action_pressed(Action.FocusPrevious)) {
|
||||
this.accept_event()
|
||||
this.autocomplete(-1)
|
||||
} else if (event.is_action_pressed(Action.FocusNext)) {
|
||||
this.accept_event()
|
||||
this.autocomplete(1)
|
||||
}
|
||||
}
|
||||
|
||||
suggest(value: string): boolean {
|
||||
|
@ -60,9 +64,9 @@ export default class AutocompleteLine extends LineEdit {
|
|||
this.caret_column = this.text.length + 1
|
||||
}
|
||||
|
||||
autocomplete() {
|
||||
autocomplete(direction: (-1 | 1)) {
|
||||
if (this.current_suggestions.length > 0) {
|
||||
this.suggestion_index = (this.suggestion_index + 1) % this.current_suggestions.length
|
||||
this.suggestion_index = (this.suggestion_index + direction) % this.current_suggestions.length
|
||||
this.text = this.current_suggestions.at(this.suggestion_index) || ''
|
||||
this.suggestion.clear()
|
||||
this.set_caret_to_end()
|
||||
|
|
|
@ -11,11 +11,10 @@ export const Action = {
|
|||
Down: 'ui_down',
|
||||
TextComplete: 'ui_text_completion_replace',
|
||||
FocusNext: 'ui_focus_next',
|
||||
FocusPrevious: 'ui_focus_prev',
|
||||
FocusPrevious: 'ui_focus_prev'
|
||||
} as const
|
||||
|
||||
export default class DevConsole extends CanvasLayer {
|
||||
|
||||
@onready("Container/VBoxContainer/Output")
|
||||
private output!: RichTextLabel
|
||||
|
||||
|
@ -36,7 +35,7 @@ export default class DevConsole extends CanvasLayer {
|
|||
this.input.autocomplete_list = method_names.toArray()
|
||||
this.parse_callable = Callable.create(
|
||||
this,
|
||||
this.submit_command
|
||||
this._submit_command
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -86,10 +85,6 @@ export default class DevConsole extends CanvasLayer {
|
|||
this.input.set_caret_to_end()
|
||||
}
|
||||
|
||||
_autocomplete() {
|
||||
this.input.autocomplete()
|
||||
}
|
||||
|
||||
async _show() {
|
||||
await this.get_tree().process_frame.as_promise()
|
||||
|
||||
|
@ -114,44 +109,52 @@ export default class DevConsole extends CanvasLayer {
|
|||
}
|
||||
}
|
||||
|
||||
print(value: string) {
|
||||
_print(value: string) {
|
||||
this.output.append_text(value)
|
||||
this.output.append_text('\n')
|
||||
}
|
||||
|
||||
wrap_in_tag(text: string, tag: [string, string]): string {
|
||||
_wrap_in_tag(text: string, tag: [string, string]): string {
|
||||
return `[${tag[0]}=${tag[1]}]${text}[/${tag[0]}]`
|
||||
}
|
||||
|
||||
format(text: string, tags: Record<string, string>): string {
|
||||
return Object.entries(tags).reduce(this.wrap_in_tag, text)
|
||||
_format(text: string, tags: Record<string, string>): string {
|
||||
return Object.entries(tags).reduce(this._wrap_in_tag, text)
|
||||
}
|
||||
|
||||
print_error(error: string) {
|
||||
this.print(this.format(error, { color: 'red' }))
|
||||
_print_error(error: string) {
|
||||
this._print(this._format(error, { color: 'red' }))
|
||||
}
|
||||
|
||||
echo(command: string) {
|
||||
this.print(this.format(`> ${command}`, { color: 'white' }))
|
||||
_echo(command: string) {
|
||||
this._print(this._format(`> ${command}`, { color: 'white' }))
|
||||
}
|
||||
|
||||
submit_command(command: string) {
|
||||
_submit_command(command: string) {
|
||||
this.input.clear()
|
||||
this.history.push(command)
|
||||
this.echo(command)
|
||||
this._echo(command)
|
||||
const error = this.expression.parse(command)
|
||||
|
||||
if (error !== GError.OK) {
|
||||
this.print_error(this.expression.get_error_text())
|
||||
this._print_error(this.expression.get_error_text())
|
||||
return
|
||||
}
|
||||
|
||||
const result = this.expression.execute(new GArray(), this)
|
||||
|
||||
if (this.expression.has_execute_failed()) {
|
||||
this.print_error(this.expression.get_error_text())
|
||||
this._print_error(this.expression.get_error_text())
|
||||
} else {
|
||||
this.print(result.toString())
|
||||
this._print(result.toString())
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.output.clear()
|
||||
}
|
||||
|
||||
echo(...args: any) {
|
||||
this._print(args.map((arg: any) => arg.toString()).join(' '))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue