mirror of
https://github.com/git-learning-game/oh-my-git.git
synced 2024-11-24 16:20:08 +01:00
Start implementing an async run function for the shell
This commit is contained in:
parent
2711d6ed8b
commit
888ff54d44
4 changed files with 56 additions and 8 deletions
|
@ -1,8 +1,12 @@
|
|||
extends Button
|
||||
|
||||
export var async = false
|
||||
|
||||
func _ready():
|
||||
pass
|
||||
|
||||
|
||||
func pressed():
|
||||
$"../..".send_command(text)
|
||||
if async:
|
||||
$"../..".send_command_async(text)
|
||||
else:
|
||||
$"../..".send_command(text)
|
||||
|
|
25
shell.gd
25
shell.gd
|
@ -4,6 +4,8 @@ class_name Shell
|
|||
var _cwd
|
||||
var _fake_editor
|
||||
|
||||
signal output(text)
|
||||
|
||||
func _init():
|
||||
# Copy fake-editor to tmp directory (because the original might be in a .pck file).
|
||||
_fake_editor = game.tmp_prefix + "fake-editor"
|
||||
|
@ -38,6 +40,29 @@ func run(command):
|
|||
|
||||
return output
|
||||
|
||||
var _t
|
||||
func run_async(command):
|
||||
_t = Thread.new()
|
||||
_t.start(self, "run_async_thread", command)
|
||||
|
||||
func run_async_thread(command):
|
||||
var port = 1000 + (randi() % 1000)
|
||||
var s = TCP_Server.new()
|
||||
s.listen(port)
|
||||
OS.execute("ncat", ["127.0.0.1", str(port), "-c", command], false, [], true)
|
||||
while not s.is_connection_available():
|
||||
pass
|
||||
var c = s.take_connection()
|
||||
print("ok")
|
||||
while c.get_status() == StreamPeerTCP.STATUS_CONNECTED:
|
||||
var available = c.get_available_bytes()
|
||||
if available > 0:
|
||||
var data = c.get_utf8_string(available)
|
||||
emit_signal("output", data)
|
||||
print(data)
|
||||
c.disconnect_from_host()
|
||||
s.stop()
|
||||
|
||||
# Run a simple command with arguments, blocking, using OS.execute.
|
||||
func _exec(command, args=[]):
|
||||
var debug = false
|
||||
|
|
10
terminal.gd
10
terminal.gd
|
@ -9,6 +9,9 @@ onready var input = $Control/Input
|
|||
onready var output = $Control/Output
|
||||
onready var repo = $"../Repositories/ActiveRepository"
|
||||
|
||||
func _ready():
|
||||
repo.shell.connect("output", self, "receive_output")
|
||||
|
||||
func _input(event):
|
||||
if history.size() > 0:
|
||||
if event.is_action_pressed("ui_up"):
|
||||
|
@ -32,9 +35,16 @@ func send_command(command):
|
|||
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)
|
||||
|
||||
input.text = ""
|
||||
output.text = output.text + "$ " + command + "\n" + o
|
||||
repo.update_everything() # FIXME
|
||||
|
||||
func receive_output(text):
|
||||
output.text += text
|
||||
|
|
|
@ -4,14 +4,14 @@
|
|||
[ext_resource path="res://terminal.gd" type="Script" id=2]
|
||||
[ext_resource path="res://command_button.tscn" type="PackedScene" id=4]
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id=2]
|
||||
[sub_resource type="StyleBoxFlat" id=1]
|
||||
content_margin_left = 10.0
|
||||
content_margin_right = 10.0
|
||||
content_margin_top = 10.0
|
||||
content_margin_bottom = 10.0
|
||||
bg_color = Color( 0, 0, 0, 1 )
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id=1]
|
||||
[sub_resource type="StyleBoxFlat" id=2]
|
||||
content_margin_left = 10.0
|
||||
content_margin_right = 10.0
|
||||
content_margin_top = 10.0
|
||||
|
@ -36,9 +36,9 @@ __meta__ = {
|
|||
|
||||
[node name="Output" type="RichTextLabel" parent="Control"]
|
||||
margin_right = 1920.0
|
||||
margin_bottom = 1039.0
|
||||
margin_bottom = 1019.0
|
||||
size_flags_vertical = 3
|
||||
custom_styles/normal = SubResource( 2 )
|
||||
custom_styles/normal = SubResource( 1 )
|
||||
custom_fonts/normal_font = ExtResource( 1 )
|
||||
scroll_following = true
|
||||
|
||||
|
@ -65,14 +65,23 @@ margin_bottom = 1011.0
|
|||
text = "git checkout HEAD^"
|
||||
align = 0
|
||||
|
||||
[node name="Button4" parent="Control" instance=ExtResource( 4 )]
|
||||
margin_left = 0.0
|
||||
margin_top = 1019.0
|
||||
margin_right = 1920.0
|
||||
margin_bottom = 1039.0
|
||||
text = "sleep 1;echo hey"
|
||||
align = 0
|
||||
async = true
|
||||
|
||||
[node name="Input" type="LineEdit" parent="Control"]
|
||||
margin_top = 1039.0
|
||||
margin_right = 1920.0
|
||||
margin_bottom = 1080.0
|
||||
custom_styles/normal = SubResource( 1 )
|
||||
custom_styles/normal = SubResource( 2 )
|
||||
custom_fonts/font = ExtResource( 1 )
|
||||
caret_blink = true
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
[connection signal="text_entered" from="Control/Input" to="." method="send_command"]
|
||||
[connection signal="text_entered" from="Control/Input" to="." method="send_command_async"]
|
||||
|
|
Loading…
Reference in a new issue