2022-11-09 21:57:46 +01:00
|
|
|
@tool
|
2020-09-29 11:16:59 +02:00
|
|
|
extends Control
|
|
|
|
# This scene demonstrates how we can control the Terminal node directly by
|
|
|
|
# sending and receiving strings and ANSI escape sequences to the terminal
|
|
|
|
# directly.
|
|
|
|
|
|
|
|
# References:
|
|
|
|
# - https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
|
|
|
|
# - https://www.youtube.com/watch?v=jTSQlIK_92w
|
|
|
|
|
2021-07-25 19:31:23 +02:00
|
|
|
const TPut = preload("res://addons/godot_xterm/util/tput.gd")
|
|
|
|
|
2022-08-23 04:49:26 +02:00
|
|
|
# Title generated using command: toilet -f pagga GODOTXTERM
|
2020-09-29 11:16:59 +02:00
|
|
|
const TITLE = """
|
2022-08-23 04:49:26 +02:00
|
|
|
░█▀▀░█▀█░█▀▄░█▀█░▀█▀░█░█░▀█▀░█▀▀░█▀▄░█▄█\r
|
|
|
|
░█░█░█░█░█░█░█░█░░█░░▄▀▄░░█░░█▀▀░█▀▄░█░█\r
|
|
|
|
░▀▀▀░▀▀▀░▀▀░░▀▀▀░░▀░░▀░▀░░▀░░▀▀▀░▀░▀░▀░▀\r
|
2020-09-29 11:16:59 +02:00
|
|
|
"""
|
|
|
|
const TITLE_WIDTH = 42
|
|
|
|
|
|
|
|
var menu_items := [
|
2021-05-26 05:39:48 +02:00
|
|
|
{"name": "Asciicast", "scene": preload("../asciicast/asciicast.tscn")},
|
2021-06-19 13:01:08 +02:00
|
|
|
{
|
|
|
|
"name": "Terminal",
|
|
|
|
"scene":
|
|
|
|
(
|
|
|
|
preload("../web_console/web_console.tscn")
|
|
|
|
if OS.has_feature("JavaScript")
|
|
|
|
else preload("../terminal/terminal.tscn")
|
|
|
|
)
|
|
|
|
},
|
2021-05-26 05:39:48 +02:00
|
|
|
{"name": "Exit"}
|
2020-09-29 11:16:59 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
var selected_index := 0
|
|
|
|
|
|
|
|
var row: int
|
|
|
|
var menu_start_row: int
|
|
|
|
var offset: int
|
|
|
|
|
2022-11-09 21:57:46 +01:00
|
|
|
@onready var tput = TPut.new($Terminal)
|
2020-09-29 11:16:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
2023-01-08 10:41:48 +01:00
|
|
|
if not $Terminal.is_connected("key_pressed", Callable(self, "_on_Terminal_key_pressed")):
|
2022-05-22 08:13:29 +02:00
|
|
|
# warning-ignore:return_value_discarded
|
2023-01-08 10:41:48 +01:00
|
|
|
$Terminal.connect("key_pressed", Callable(self, "_on_Terminal_key_pressed"))
|
2020-09-29 11:16:59 +02:00
|
|
|
# warning-ignore:return_value_discarded
|
2023-01-08 10:41:48 +01:00
|
|
|
$Terminal.connect("size_changed", Callable(self, "draw_all"))
|
2020-09-29 11:16:59 +02:00
|
|
|
$Terminal.grab_focus()
|
|
|
|
draw_all()
|
|
|
|
|
|
|
|
|
|
|
|
func draw_all(_size = Vector2.ZERO):
|
2022-08-15 00:35:01 +02:00
|
|
|
offset = int(floor(($Terminal.get_cols() / 2.0) - (TITLE_WIDTH / 2.0)))
|
2020-09-29 11:16:59 +02:00
|
|
|
tput.reset()
|
|
|
|
row = 5
|
2021-05-26 05:39:48 +02:00
|
|
|
tput.civis() # Hide the cursor.
|
2020-09-29 11:16:59 +02:00
|
|
|
draw_title()
|
|
|
|
draw_menu()
|
|
|
|
tput.sgr0()
|
|
|
|
row += 1
|
|
|
|
|
|
|
|
|
|
|
|
func draw_title():
|
2021-07-23 03:33:05 +02:00
|
|
|
tput.setaf(tput.ANSIColor.bright_yellow)
|
2020-09-29 11:16:59 +02:00
|
|
|
tput.cup(row, 0)
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
for line in TITLE.split("\r"):
|
|
|
|
row += 1
|
|
|
|
tput.cup(row, offset)
|
|
|
|
$Terminal.write(line)
|
2021-07-17 15:57:43 +02:00
|
|
|
tput.sgr0()
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
# Get the plugin version from the plugin's config file.
|
|
|
|
var config = ConfigFile.new()
|
|
|
|
var err = config.load("res://addons/godot_xterm/plugin.cfg")
|
|
|
|
if err == OK:
|
|
|
|
$Terminal.write("\n")
|
2021-05-26 05:39:48 +02:00
|
|
|
$Terminal.write("Version: %s" % config.get_value("plugin", "version", "unknown"))
|
2020-09-29 11:16:59 +02:00
|
|
|
row += 2
|
|
|
|
|
|
|
|
|
|
|
|
func draw_menu():
|
|
|
|
if not menu_start_row:
|
|
|
|
menu_start_row = row + 1
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
row = menu_start_row
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
var col_offset: int
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
for i in range(menu_items.size()):
|
|
|
|
row += 1
|
|
|
|
var item = menu_items[i]
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
if not col_offset:
|
2022-08-15 00:35:01 +02:00
|
|
|
col_offset = int(floor(($Terminal.get_cols() / 2) - (item.name.length() / 2)))
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
tput.cup(row, offset)
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
if selected_index == i:
|
2021-07-23 03:33:05 +02:00
|
|
|
tput.setab(tput.ANSIColor.red)
|
|
|
|
tput.setaf(tput.ANSIColor.black)
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
$Terminal.write("%s. %s" % [i + 1, item.name])
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
if selected_index == i:
|
|
|
|
tput.sgr0()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_Terminal_key_pressed(data: String, event: InputEventKey) -> void:
|
2021-05-26 05:39:48 +02:00
|
|
|
match data:
|
2022-12-29 10:52:13 +01:00
|
|
|
TPut.CURSOR_UP: # Up arrow key
|
2020-09-29 11:16:59 +02:00
|
|
|
selected_index = int(clamp(selected_index - 1, 0, menu_items.size() - 1))
|
|
|
|
draw_menu()
|
2022-12-29 10:52:13 +01:00
|
|
|
TPut.CURSOR_DOWN: # Down arrow key
|
2020-09-29 11:16:59 +02:00
|
|
|
selected_index = int(clamp(selected_index + 1, 0, menu_items.size() - 1))
|
|
|
|
draw_menu()
|
|
|
|
"1":
|
|
|
|
selected_index = 0
|
|
|
|
draw_menu()
|
|
|
|
"2":
|
|
|
|
selected_index = 1
|
|
|
|
draw_menu()
|
|
|
|
"3":
|
|
|
|
selected_index = 2
|
|
|
|
draw_menu()
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
# We can also match against the raw InputEventKey.
|
2022-12-29 10:52:13 +01:00
|
|
|
if event.keycode == KEY_ENTER:
|
2020-09-29 11:16:59 +02:00
|
|
|
var item = menu_items[selected_index]
|
2021-05-26 05:39:48 +02:00
|
|
|
|
2020-09-29 11:16:59 +02:00
|
|
|
match item.name:
|
|
|
|
"Asciicast":
|
2022-11-09 21:57:46 +01:00
|
|
|
var scene = item.scene.instantiate()
|
2020-09-29 11:16:59 +02:00
|
|
|
var animation_player: AnimationPlayer = scene.get_node("AnimationPlayer")
|
2023-01-08 10:41:48 +01:00
|
|
|
scene.connect(
|
|
|
|
"key_pressed",
|
|
|
|
Callable(self, "_on_Asciicast_key_pressed").bind(animation_player)
|
|
|
|
)
|
2021-07-23 03:33:05 +02:00
|
|
|
add_child(scene)
|
2020-09-29 11:16:59 +02:00
|
|
|
scene.grab_focus()
|
2022-11-09 21:57:46 +01:00
|
|
|
await animation_player.animation_finished
|
2021-07-23 03:33:05 +02:00
|
|
|
remove_child(scene)
|
2020-09-29 11:16:59 +02:00
|
|
|
$Terminal.grab_focus()
|
|
|
|
scene.queue_free()
|
|
|
|
"Terminal":
|
2020-11-10 04:04:08 +01:00
|
|
|
if OS.get_name() == "Windows":
|
2021-05-26 05:39:48 +02:00
|
|
|
return OS.call_deferred(
|
|
|
|
"alert",
|
|
|
|
(
|
|
|
|
"Psuedoterminal node currently"
|
2020-11-10 04:04:08 +01:00
|
|
|
+ " uses pty.h but needs to use either winpty or conpty"
|
2022-11-09 22:52:54 +01:00
|
|
|
+ " to work on Windows."
|
2021-05-26 05:39:48 +02:00
|
|
|
),
|
2022-11-09 22:52:54 +01:00
|
|
|
"Terminal not Supported on Windows"
|
2021-05-26 05:39:48 +02:00
|
|
|
)
|
2022-11-09 21:57:46 +01:00
|
|
|
var scene = item.scene.instantiate()
|
2021-07-02 19:27:34 +02:00
|
|
|
var pty = scene if OS.has_feature("JavaScript") else scene.get_node("PTY")
|
2021-07-23 03:33:05 +02:00
|
|
|
add_child(scene)
|
2020-09-29 11:16:59 +02:00
|
|
|
scene.grab_focus()
|
2022-11-09 21:57:46 +01:00
|
|
|
await pty.exited
|
2020-09-29 11:16:59 +02:00
|
|
|
$Terminal.grab_focus()
|
|
|
|
scene.queue_free()
|
|
|
|
"Exit":
|
2022-12-29 10:52:13 +01:00
|
|
|
pass
|
|
|
|
# FIXME
|
|
|
|
#if OS.has_feature("JavaScript"):
|
2023-01-08 10:41:48 +01:00
|
|
|
#JavaScript.eval("window.history.back() || window.close()")
|
2022-12-29 10:52:13 +01:00
|
|
|
#get_tree().quit()
|
2020-09-29 11:16:59 +02:00
|
|
|
|
|
|
|
|
2021-05-26 05:39:48 +02:00
|
|
|
func _on_Asciicast_key_pressed(
|
|
|
|
data: String, _event: InputEventKey, animation_player: AnimationPlayer
|
|
|
|
) -> void:
|
2020-09-29 11:16:59 +02:00
|
|
|
if data == "\u001b":
|
|
|
|
animation_player.emit_signal("animation_finished")
|