From 3241bc15d595ff3d59f604a341a29177b99df4fd Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Sun, 15 Mar 2020 05:52:38 +0800 Subject: [PATCH] Make terminal start with current directory and support any command from elisp side. --- app/terminal/buffer.py | 27 +++++++++++++++++++++++---- eaf.el | 9 +++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/terminal/buffer.py b/app/terminal/buffer.py index b3c25d3..ab4bb25 100644 --- a/app/terminal/buffer.py +++ b/app/terminal/buffer.py @@ -37,9 +37,16 @@ class AppBuffer(BrowserBuffer): self.port = get_free_port() self.url = url + argument_list = arguments.split("ᛡ") + self.command = argument_list[0] + self.start_directory = argument_list[1] + # Start wetty process. self.background_process = subprocess.Popen( - "wetty -p {0} --base / --sshuser {1} --sshauth publickey -c {2}".format(self.port, getpass.getuser(), os.environ["SHELL"]), + "wetty -p {0} --base / --sshuser {1} --sshauth publickey -c {2}".format( + self.port, + getpass.getuser(), + "'{}'".format(self.command)), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) @@ -53,9 +60,21 @@ class AppBuffer(BrowserBuffer): def load_wetty_server(self): self.buffer_widget.setUrl(QUrl("http://localhost:{0}".format(self.port))) - paths = os.path.split(self.url) - if len(paths) > 0: - self.change_title(paths[-1]) + self.update_title() + + def update_title(self): + self.change_title("{0}-{1}".format( + os.path.basename(os.path.normpath(os.path.expanduser(self.start_directory))), + self.random_string() + )) def before_destroy_buffer(self): os.kill(self.background_process.pid, signal.SIGTERM) + + def random_string(self): + import hashlib + import time + + hash = hashlib.sha1() + hash.update(str(time.time()).encode("utf-8")) + return hash.hexdigest()[:4] diff --git a/eaf.el b/eaf.el index a7da531..40a411f 100644 --- a/eaf.el +++ b/eaf.el @@ -1475,7 +1475,7 @@ This function works best if paired with a fuzzy search package." (if history-file-exists (mapcar (lambda (h) (when (string-match history-pattern h) - (format "[%s] ⇰ %s" (match-string 1 h) (match-string 2 h)))) + (format "[%s] ⇰ %s" (match-string 1 h) (match-string 2 h)))) (with-temp-buffer (insert-file-contents browser-history-file-path) (split-string (buffer-string) "\n" t))) nil))) @@ -1533,7 +1533,12 @@ choose a search engine defined in `eaf-browser-search-engines'" (defun eaf-open-terminal () "Open EAF terminal application." (interactive) - (eaf-open (eaf--generate-terminal-buffer-name) "terminal")) + (eaf-open (eaf--generate-terminal-buffer-name) + "terminal" + (format "%sᛡ%s" (eaf--generate-terminal-command) default-directory))) + +(defun eaf--generate-terminal-command () + (format "cd %s && exec %s --login" default-directory (getenv "SHELL"))) (defun eaf--generate-terminal-buffer-name () (format "%s-%04x" "eaf-terminal" (random (expt 16 4))))