Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
✨ Process name in tab titles for *nix
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSquared committed Mar 23, 2019
1 parent 5387728 commit 574fee0
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/classes/terminal.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,7 @@ class Terminal {
});
break;
case "Darwin":
// OK, the following is quite of a hacky solution
// Each $XX after the $9 in the awk commands provide support for one more space
// character in the path (otherwise it just gets cut)
// There's probably a better way to do this, PRs welcome
require("child_process").exec(`lsof -a -d cwd -p ${pid} | tail -1 | awk '{print $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20}'`, (e, cwd) => {
require("child_process").exec(`lsof -a -d cwd -p ${pid} | tail -1 | awk '{ for (i=9; i<=NF; i++) printf $i }'`, (e, cwd) => {
if (e !== null) {
reject(e);
} else {
Expand All @@ -276,6 +272,25 @@ class Terminal {
}
});
};
this._getTtyProcess = tty => {
return new Promise((resolve, reject) => {
let pid = tty._pid;
switch(require("os").type()) {
case "Linux":
case "Darwin":
require("child_process").exec(`ps -o comm --no-headers --sort=+pid -g ${pid} | tail -1`, (e, proc) => {
if (e !== null) {
reject(e);
} else {
resolve(proc.trim());
}
});
break;
default:
reject("Unsupported OS");
}
});
};
this._nextTickUpdateTtyCWD = false;
this._nextTickUpdateProcess = false;
this._tick = setInterval(() => {
Expand All @@ -302,11 +317,22 @@ class Terminal {

if (this.renderer && this._nextTickUpdateProcess) {
this._nextTickUpdateProcess = false;
try {
this.renderer.send("terminal_channel-"+this.port, "New process", this.tty._file);
} catch(e) {
// renderer closed
}
this._getTtyProcess(this.tty).then(process => {
if (this.tty._process === process) return;
this.tty._process = process;
if (this.renderer) {
this.renderer.send("terminal_channel-"+this.port, "New process", process);
}
}).catch(e => {
if (!this._closed) {
console.log("Error while retrieving TTY subprocess: ", e);
try {
this.renderer.send("terminal_channel-"+this.port, "New process", "");
} catch(e) {
// renderer closed
}
}
});
}
}, 1000);

Expand Down

0 comments on commit 574fee0

Please sign in to comment.