Skip to content

Commit

Permalink
Change tab label to name of foreground child process
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Wootten authored and Jeremy Wootten committed Jan 1, 2025
1 parent 2d6c2b7 commit 0ad51a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
30 changes: 22 additions & 8 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ namespace Terminal {
string? command = null,
bool create_new_tab = false
) {

/* This requires all restored tabs to be initialized first so that
* the shell location is available.
* Do not add a new tab if location is already open in existing tab */
Expand Down Expand Up @@ -781,13 +782,15 @@ namespace Terminal {
terminal_widget.child_exited.connect (on_terminal_child_exited);
terminal_widget.notify["font-scale"].connect (on_terminal_font_scale_changed);
terminal_widget.cwd_changed.connect (on_terminal_cwd_changed);
terminal_widget.foreground_process_changed.connect (on_terminal_program_changed);
terminal_widget.window_title_changed.connect (on_terminal_window_title_changed);
}

private void disconnect_terminal_signals (TerminalWidget terminal_widget) {
terminal_widget.child_exited.disconnect (on_terminal_child_exited);
terminal_widget.notify["font-scale"].disconnect (on_terminal_font_scale_changed);
terminal_widget.cwd_changed.disconnect (on_terminal_cwd_changed);
terminal_widget.foreground_process_changed.disconnect (on_terminal_program_changed);
terminal_widget.window_title_changed.disconnect (on_terminal_window_title_changed);
}

Expand All @@ -797,12 +800,13 @@ namespace Terminal {
// TabView already removed tab - ignore signal
return;
}
if (!tw.killed) {
if (!tw.killed) {
if (tw.program_string != null) {
/* If a program was running, do not close the tab so that output of program
* remains visible */
tw.program_string = null;
tw.active_shell (tw.current_working_directory);
check_for_tabs_with_same_name ();
} else {
if (tw.tab != null) {
notebook.tab_view.close_page (tw.tab);
Expand Down Expand Up @@ -1119,16 +1123,21 @@ namespace Terminal {
int j = 0;
for (i = 0; i < notebook.n_pages; i++) {
var term = get_term_widget (notebook.tab_view.get_nth_page (i));
string term_path = term.current_working_directory;
string term_label = Path.get_basename (term_path);
if (term_label == "" ||
term.tab_label == TerminalWidget.DEFAULT_LABEL) {
string term_path, term_label;
if (term.program_string != "") {
term.tab_label = term.program_string;
continue;
} else {
term_path = term.current_working_directory;
term_label = Path.get_basename (term_path);
if (term_label == "" || term_label == "/") {
term.tab_label = TerminalWidget.DEFAULT_LABEL;
continue;
} else {
term.tab_label = term_label;
}
}

/* Reset tab_name to basename so long name only used when required */
term.tab_label = term_label;

for (j = 0; j < notebook.n_pages; j++) {
var term2 = get_term_widget (notebook.tab_view.get_nth_page (j));
string term2_path = term2.current_working_directory;
Expand All @@ -1153,6 +1162,11 @@ namespace Terminal {
save_opened_terminals (true, false);
}

private void on_terminal_program_changed (TerminalWidget src, string cmdline) {
src.program_string = cmdline;
check_for_tabs_with_same_name (); // Also sets window title
}

private void save_opened_terminals (bool save_tabs, bool save_zooms) {
string[] zooms = {};
string[] opened_tabs = {};
Expand Down
28 changes: 24 additions & 4 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ namespace Terminal {
internal const string DEFAULT_LABEL = _("Terminal");
public string terminal_id;
public string current_working_directory { get; private set; default = "";}
public string? program_string { get; set; default = null; }
public string program_string { get; set; default = ""; }
static int terminal_id_counter = 0;
private bool init_complete;
public bool resized {get; set;}

GLib.Pid child_pid;
GLib.Pid fg_pid;

public unowned MainWindow main_window { get; construct set; }

Expand Down Expand Up @@ -160,6 +161,7 @@ namespace Terminal {
private double scroll_delta = 0.0;

public signal void cwd_changed (string cwd);
public signal void foreground_process_changed (string cmdline);

public TerminalWidget (MainWindow parent_window) {
Object (
Expand Down Expand Up @@ -236,7 +238,7 @@ namespace Terminal {

selection_changed.connect (() => copy_action.set_enabled (get_has_selection ()));
size_allocate.connect (() => resized = true);
contents_changed.connect (check_cwd_changed);
contents_changed.connect (on_contents_changed);
child_exited.connect (on_child_exited);
ulong once = 0;
once = realize.connect (() => {
Expand Down Expand Up @@ -718,7 +720,7 @@ namespace Terminal {
warning (e.message);
}

check_cwd_changed ();
on_contents_changed ();
}

public void run_program (string _program_string, string? working_directory) {
Expand Down Expand Up @@ -796,6 +798,16 @@ namespace Terminal {
}
}

public string get_pid_name (int pid) {
try {
string cmdline;
GLib.FileUtils.get_contents ("/proc/%d/cmdline".printf (pid), out cmdline);
return cmdline;
} catch (GLib.Error e) {
return "";
}
}

protected override void increase_font_size () {
font_scale += 0.1;
}
Expand Down Expand Up @@ -939,13 +951,21 @@ namespace Terminal {
}
}

private void check_cwd_changed () {
private void on_contents_changed () {
var cwd = get_shell_location ();
if (cwd != current_working_directory) {
current_working_directory = cwd;
tab.tooltip = current_working_directory;
cwd_changed (cwd);
}

int pid;
try_get_foreground_pid (out pid);
if (pid != fg_pid) {
var cmdline = get_pid_name (pid);
foreground_process_changed (cmdline);
fg_pid = pid;
}
}
}
}

0 comments on commit 0ad51a1

Please sign in to comment.