Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminal: Rewrite #324, add support for indicating ssh and program running in terminal buffer name #325 #331

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 56 additions & 11 deletions app/terminal/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import subprocess
import signal
import threading
import socket
import getpass
import json

Expand All @@ -45,7 +46,9 @@ def __init__(self, buffer_id, url, config_dir, arguments, emacs_var_dict, module
self.current_directory = self.start_directory
self.index_file = os.path.join(os.path.dirname(__file__), "index.html")
self.server_js = os.path.join(os.path.dirname(__file__), "server.js")

self.host_destination = str(getpass.getuser())+"@"+str(socket.gethostname())
self.current_destination = self.host_destination
self.executing_command = ""
self.buffer_widget.titleChanged.connect(self.change_title)

# Start server process.
Expand All @@ -65,7 +68,7 @@ def __init__(self, buffer_id, url, config_dir, arguments, emacs_var_dict, module

self.timer=QTimer()
self.timer.start(250)
self.timer.timeout.connect(self.on_change_directory)
self.timer.timeout.connect(self.update_title)

def focus_terminal(self):
event = QMouseEvent(QEvent.MouseButtonPress, QPointF(0, 0), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
Expand All @@ -78,18 +81,60 @@ def open_terminal_page(self):
(self.emacs_var_dict["eaf-terminal-dark-mode"] == "" and self.emacs_var_dict["eaf-emacs-theme-mode"] == "dark"):
theme = "dark"
with open(self.index_file, "r") as f:
html = f.read().replace("%1", str(self.port)).replace("%2", "file://" + os.path.join(os.path.dirname(__file__))).replace("%3", theme).replace("%4", self.emacs_var_dict["eaf-terminal-font-size"]).replace("%5", self.current_directory)
html = f.read().replace("%1", str(self.port)).replace("%2", "file://" + os.path.join(os.path.dirname(__file__))).replace("%3", theme).replace("%4", self.emacs_var_dict["eaf-terminal-font-size"]).replace("%5", self.current_directory).replace("%6", self.host_destination)
self.buffer_widget.setHtml(html)

def on_change_directory(self):
changed_directory = self.buffer_widget.execute_js("title")
if not str(changed_directory) == self.current_directory:
self.update_title()
self.eval_in_emacs.emit('''(setq default-directory "'''+ str(changed_directory) +'''")''')
self.current_directory = str(changed_directory)

def update_title(self):
self.change_title(self.buffer_widget.execute_js("title"))
changed_directory = str(self.buffer_widget.execute_js("title"))
changed_destination = str(self.buffer_widget.execute_js("current_destination"))
changed_executing_command = ""
raw_message = str(self.buffer_widget.execute_js("executing_command")).split(" ",2)
if raw_message[0] == "sudo":
changed_executing_command = "sudo " + raw_message[1]
else:
changed_executing_command = raw_message[0]
HollowMan6 marked this conversation as resolved.
Show resolved Hide resolved
if changed_executing_command != self.executing_command:
if changed_destination == self.host_destination:
if changed_executing_command:
self.change_title(changed_executing_command + "- " + changed_directory)
else:
self.change_title(changed_directory)
self.eval_in_emacs.emit('''(setq default-directory "'''+ changed_directory +'''")''')
else:
if changed_executing_command:
self.change_title(changed_executing_command + "- " + changed_destination+ ":" +changed_directory)
else:
self.change_title(changed_destination+ ":" +changed_directory)
else:
if not changed_directory == self.current_directory:
if changed_destination == self.host_destination:
if changed_executing_command:
self.change_title(changed_executing_command + "- " +changed_directory)
else:
self.change_title(changed_directory)
self.eval_in_emacs.emit('''(setq default-directory "'''+ changed_directory +'''")''')
else:
if changed_executing_command:
self.change_title(changed_executing_command + "- "+ changed_destination+ ":" +changed_directory)
else:
self.change_title(changed_destination+ ":" +changed_directory)
else:
if not changed_destination == self.host_destination:
if not changed_destination == self.current_destination:
if changed_executing_command:
self.change_title(changed_executing_command + "- "+ changed_destination+ ":" +changed_directory)
else:
self.change_title(changed_destination+ ":" +changed_directory)
else:
if not changed_destination == self.current_destination:
if changed_executing_command:
self.change_title(changed_executing_command + "- "+ changed_directory)
else:
self.change_title(changed_directory)
self.eval_in_emacs.emit('''(setq default-directory "'''+ changed_directory +'''")''')
self.current_destination = changed_destination
self.current_directory = changed_directory
self.executing_command = changed_executing_command

def destroy_buffer(self):
os.kill(self.background_process.pid, signal.SIGKILL)
Expand Down
49 changes: 42 additions & 7 deletions app/terminal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
});

var title = "%5"
var host_destination = "%6"
var current_destination = "%6"
var executing_command = ""

const searchAddon = new SearchAddon.SearchAddon();

Expand Down Expand Up @@ -123,28 +126,60 @@
}

window.addEventListener("resize", sendSizeToServer);
term.onLineFeed(() => {
const buffer = term.buffer;
const new_line = buffer.getLine(buffer.baseY + buffer.cursorY);
if (new_line && !new_line.isWrapped) {
var input_data = get_line_data(buffer, buffer.baseY + buffer.cursorY - 1);
if (input_data.indexOf('$')!=-1){
executing_command = input_data.slice(input_data.indexOf('$')+2)
HollowMan6 marked this conversation as resolved.
Show resolved Hide resolved
if(executing_command === "exit" && host_destination === current_destination){
window.close();
}
if(executing_command.startsWith("cd ")){
executing_command = ""
}
}
}
})
function get_line_data(buffer, line_index) {
let line = buffer.getLine(line_index);
if (!line) {
return;
}
let line_data = line.translateToString(true);
while (line_index > 0 && line.isWrapped) {
line = buffer.getLine(--line_index);
if (!line) {
break;
}
line_data = line.translateToString(false) + line_data;
}
return line_data;
}
}

socket.onmessage = (msg) => {
var ssh_re = /]0;(.*?):/g;
var re = /:([^\x07].*?)\x07/g;
ssh_arr = ssh_re.exec(msg.data)
arr = re.exec(msg.data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在我ssh后这儿的bug有点多,match可能有点脆弱了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我这里一切正常,无论何种情况,可能是不同环境的msg不一样

if(ssh_arr) {
executing_command = ""
current_destination =ssh_arr[1]
}
if(arr) {
executing_command = ""
if (arr[1] === "/") {
title = arr[1];
}
else {
title = arr[1]+"/"
}
}

if(msg === "Closing") {
socket.close();
HollowMan6 marked this conversation as resolved.
Show resolved Hide resolved
}
}

socket.onclose = () => {
window.close();
}
socket.onclose = () => {}
socket.onerror = () => {}
</script>
</body>
Expand Down
4 changes: 1 addition & 3 deletions app/terminal/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
this.Pty = require("node-pty");
this.Websocket = require("ws").Server;

this.onclosed = () => {
ws.send("Closing");
};
this.onclosed = () => {};
this.onopened = () => {};
this.onresize = () => {};
this.ondisconnected = () => {};
Expand Down