oh-my-git/terminal.gd

58 lines
1.5 KiB
GDScript3
Raw Normal View History

2020-09-01 21:25:24 +02:00
extends Control
2020-06-10 18:25:41 +02:00
var thread
2020-09-01 19:59:07 +02:00
var history = []
var history_position = 0
2020-09-01 21:25:24 +02:00
onready var input = $Control/Input
onready var output = $Control/Output
onready var repo = $"../Repositories/ActiveRepository"
onready var main = get_parent()
2020-09-01 21:25:24 +02:00
func _ready():
repo.shell.connect("output", self, "receive_output")
2020-09-01 19:59:07 +02:00
func _input(event):
if history.size() > 0:
if event.is_action_pressed("ui_up"):
2020-09-05 10:47:38 +02:00
if history_position > 0:
history_position -= 1
input.text = history[history_position]
input.caret_position = input.text.length()
2020-09-03 18:10:09 +02:00
# This prevents the Input taking the arrow as a "skip to beginning" command.
get_tree().set_input_as_handled()
2020-09-01 19:59:07 +02:00
if event.is_action_pressed("ui_down"):
2020-09-05 10:47:38 +02:00
if history_position < history.size()-1:
history_position += 1
input.text = history[history_position]
input.caret_position = input.text.length()
2020-09-03 18:10:09 +02:00
get_tree().set_input_as_handled()
2020-09-01 19:59:07 +02:00
func send_command(command):
2020-09-01 19:59:07 +02:00
history.push_back(command)
2020-09-05 10:47:38 +02:00
history_position = history.size()
2020-09-01 19:59:07 +02:00
thread = Thread.new()
thread.start(self, "run_command_in_a_thread", command)
func send_command_async(command):
repo.shell.run_async(command)
input.text = ""
func run_command_in_a_thread(command):
var o = repo.shell.run(command)
2020-09-10 12:03:46 +02:00
check_win_condition()
2020-09-01 21:25:24 +02:00
input.text = ""
output.text = output.text + "$ " + command + "\n" + o
2020-09-10 12:03:46 +02:00
repo.update_everything()
func receive_output(text):
output.text += text
2020-09-10 12:03:46 +02:00
func check_win_condition():
if repo.shell.run("bash /tmp/win && echo yes || echo no") == "yes\n":
main.load_next_level()
print("you win!")