diff --git a/src/MainWindow.vala b/src/MainWindow.vala index eace3dc76e..d072057b8a 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -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 */ @@ -781,6 +782,7 @@ 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); } @@ -788,6 +790,7 @@ namespace Terminal { 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); } @@ -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); @@ -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; @@ -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 = {}; diff --git a/src/Widgets/TerminalWidget.vala b/src/Widgets/TerminalWidget.vala index 1a2caa88e3..71aede57ad 100644 --- a/src/Widgets/TerminalWidget.vala +++ b/src/Widgets/TerminalWidget.vala @@ -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; } @@ -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 ( @@ -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 (() => { @@ -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) { @@ -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; } @@ -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; + } } } }