Skip to content

Commit

Permalink
Merge pull request #351 from HollowMan6/Term-exec-cmd
Browse files Browse the repository at this point in the history
Terminal: Rename buffer when running programme as in #331
  • Loading branch information
manateelazycat committed Jul 28, 2020
2 parents 45e2fd9 + 3670485 commit 631dadf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
18 changes: 15 additions & 3 deletions app/terminal/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self, buffer_id, url, config_dir, arguments, emacs_var_dict, module
self.command = arguments_dict["command"]
self.start_directory = arguments_dict["directory"]
self.current_directory = self.start_directory
self.executing_command = ""
self.index_file = os.path.join(os.path.dirname(__file__), "index.html")
self.server_js = os.path.join(os.path.dirname(__file__), "server.js")

Expand Down Expand Up @@ -83,10 +84,21 @@ def open_terminal_page(self):

def checking_status(self):
changed_directory = str(self.buffer_widget.execute_js("title"))
if not changed_directory == self.current_directory:
changed_executing_command = str(self.buffer_widget.execute_js("executing_command"))
if len(changed_executing_command) > 30:
changed_executing_command = changed_executing_command[:30]

if changed_executing_command != self.executing_command and changed_executing_command != "":
self.change_title(changed_executing_command)
self.executing_command = changed_executing_command
elif changed_executing_command == "" and self.executing_command != "" or not changed_directory == self.current_directory:
self.change_title(changed_directory)
self.eval_in_emacs.emit('''(setq default-directory "'''+ changed_directory +'''")''')
self.current_directory = changed_directory
if not changed_directory == self.current_directory:
self.eval_in_emacs.emit('''(setq default-directory "'''+ changed_directory +'''")''')
self.current_directory = changed_directory
if self.executing_command != "":
self.executing_command = ""

if subprocess.Popen.poll(self.background_process) is not None:
self.destroy_buffer()

Expand Down
32 changes: 31 additions & 1 deletion app/terminal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
var socket = new WebSocket("ws://127.0.0.1:%1");
}
catch(err) {
setTimeout('location.reload();',1000);
location.reload();
}
const term = new Terminal({
fontSize: "%4",
Expand All @@ -46,6 +46,7 @@
});

var title = "%5"
var executing_command = ""

const searchAddon = new SearchAddon.SearchAddon();

Expand Down Expand Up @@ -124,13 +125,42 @@

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('$')+1).trim()
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 re = /:([^\x07].*?)\x07/g;
arr = re.exec(msg.data)
if(arr) {
title = arr[1];
executing_command = ""
}
}

Expand Down

0 comments on commit 631dadf

Please sign in to comment.