Skip to content
81 changes: 26 additions & 55 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ namespace Terminal {
current_terminal.remember_command_end_position ();
get_simple_action (ACTION_COPY_LAST_OUTPUT).set_enabled (false);
}

break;

case Gdk.Key.@1: //alt+[1-8]
Expand Down Expand Up @@ -738,7 +739,6 @@ namespace Terminal {
var t = get_term_widget (tab);
terminals.append (t);
t.window = this;
schedule_name_check ();
}

private void on_tab_removed (Granite.Widgets.Tab tab) {
Expand All @@ -747,8 +747,6 @@ namespace Terminal {

if (notebook.n_tabs == 0) {
destroy ();
} else {
schedule_name_check ();
}
}

Expand Down Expand Up @@ -776,6 +774,10 @@ namespace Terminal {
}

current_terminal.grab_focus ();
if (t != current_terminal) { // Otherwise current_terminal will change triggering a name check
check_for_tabs_with_same_name ();
}

return true;
}

Expand All @@ -792,7 +794,7 @@ namespace Terminal {
notebook.insert_tab (tab, -1);
notebook.current = tab;
term.grab_focus ();
schedule_name_check ();
check_for_tabs_with_same_name ();
}

private void on_tab_moved (Granite.Widgets.Tab tab, int x, int y) {
Expand All @@ -811,6 +813,7 @@ namespace Terminal {
notebook.remove_tab (tab);
new_notebook.insert_tab (tab, -1);
new_window.current_terminal = t;
check_for_tabs_with_same_name (); // Update remaining tabs
return false;
});
}
Expand Down Expand Up @@ -939,7 +942,6 @@ namespace Terminal {
Granite.Widgets.Tab new_tab) {

current_terminal = get_term_widget (new_tab);
title = current_terminal.tab_label ?? TerminalWidget.DEFAULT_LABEL;
/* The font-scales of all terminals are currently the synchronized through saved-state binding */
new_tab.icon = null;
Idle.add (() => {
Expand Down Expand Up @@ -1072,21 +1074,10 @@ namespace Terminal {
t.tab.close ();
return;
}

schedule_name_check ();
}
});

/* This signal is not emitted when the .bashrc is missing or does not force terminal to
* set its title. So we cannot detect path changes in the shell. Tab name remains "Terminal".
*/
t.window_title_changed.connect (() => {
if (t == current_terminal) {
title = t.window_title;
}

schedule_name_check ();
});
t.cwd_changed.connect (check_for_tabs_with_same_name);

t.set_font (term_font);

Expand Down Expand Up @@ -1133,7 +1124,7 @@ namespace Terminal {
sw.add (term);
var tab = new Granite.Widgets.Tab (label, icon, sw);
term.tab = tab;
tab.ellipsize_mode = Pango.EllipsizeMode.START;
tab.ellipsize_mode = Pango.EllipsizeMode.MIDDLE;

var reload_menu_item = new Gtk.MenuItem.with_label (_("Reload"));
tab.menu.append (reload_menu_item);
Expand Down Expand Up @@ -1301,6 +1292,7 @@ namespace Terminal {
private void action_close_tab () {
current_terminal.tab.close ();
current_terminal.grab_focus ();
// Closing a tab will switch to another, which will trigger check for same names
}

private void action_new_window () {
Expand Down Expand Up @@ -1421,63 +1413,42 @@ namespace Terminal {
return (TerminalWidget)((Gtk.Bin)tab.page).get_child ();
}

private uint name_check_timeout_id = 0;
private void schedule_name_check () {
if (name_check_timeout_id > 0) {
Source.remove (name_check_timeout_id);
}

name_check_timeout_id = Timeout.add (50, () => {
save_opened_terminals ();

if (!check_for_tabs_with_same_name ()) {
return true;
} else {
name_check_timeout_id = 0;
return false;
}
});
}

/** Compare every tab label with every other and resolve ambiguities **/
private bool check_for_tabs_with_same_name () {
private void check_for_tabs_with_same_name () {
/* Take list copies so foreach clauses can be nested safely*/
var terms = terminals.copy ();
var terms2 = terminals.copy ();

foreach (TerminalWidget terminal in terms) {
string term_path = terminal.get_shell_location ();
string term_label = terminal.window_title;
string term_path = terminal.current_working_directory;
string term_label = Path.get_basename (term_path);

if (term_label == "") { /* No point in continuing - tabs not finished updating */
return false; /* Try again later */
}
if (term_label == "" ||
terminal.tab_label == TerminalWidget.DEFAULT_LABEL) {

if (terminal.tab_label == TerminalWidget.DEFAULT_LABEL) { /* Absent or incorrect .bashrc */
return true;
continue;
}

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

foreach (TerminalWidget terminal2 in terms2) {
string term2_path = terminal2.get_shell_location ();
string term2_name = terminal2.window_title;
string term2_path = terminal2.current_working_directory;
string term2_name = Path.get_basename (term2_path);

if (terminal2 != terminal && term2_name == term_label) {
if (term2_path != term_path) {
terminal2.tab_label = disambiguate_label (term2_path, term_path);
terminal.tab_label = disambiguate_label (term_path, term2_path);
if (terminal2 != terminal && term2_name == term_label && term2_path != term_path) {
terminal2.tab_label = disambiguate_label (term2_path, term_path);
terminal.tab_label = disambiguate_label (term_path, term2_path);
// Overwrite tooltip_text set by changing tab_label
terminal2.tooltip_text = term2_path;
terminal.tooltip_text = term_path;

if (terminal == current_terminal) {
title = terminal.tab_label;
}
}
}
}
}

return true;
title = current_terminal.current_working_directory;
return;
}

private void save_opened_terminals () {
Expand Down
15 changes: 15 additions & 0 deletions src/Widgets/TerminalWidget.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Terminal {
internal const string DEFAULT_LABEL = _("Terminal");
public Terminal.Application app;
public string terminal_id;
public string current_working_directory { get; private set; default = "";}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codestyle

Suggested change
public string current_working_directory { get; private set; default = "";}
public string current_working_directory { get; private set; default = ""; }

static int terminal_id_counter = 0;
private bool init_complete;
public bool resized {get; set;}
Expand Down Expand Up @@ -123,6 +124,8 @@ namespace Terminal {

private double total_delta_y = 0.0;

public signal void cwd_changed ();

public TerminalWidget (MainWindow parent_window) {
pointer_autohide = true;

Expand Down Expand Up @@ -223,6 +226,8 @@ namespace Terminal {
resized = true;
});

contents_changed.connect (check_cwd_changed);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing that still bother me a little bit, is that this is also triggered for foreground processes. Guarding it with a has_foreground_process check probably doesn't make sense, because I assume they are comparatively expensive.

Anyway it probably doesn't matter so much, I didn't see any performance differences on my machine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One solution would be to unregistered the callback when a foreground process is running and re-register it on child_exited. But this is probably a unnecessary micro optimizations, just came to my mind.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if foreground processes cannot change the cwd then this check is unnecessary. But I doubt whether the extra complexity of code is worth the (undetectable) performance gain.


child_exited.connect (on_child_exited);

/* target entries specify what kind of data the terminal widget accepts */
Expand Down Expand Up @@ -377,6 +382,8 @@ namespace Terminal {
} catch (Error e) {
warning (e.message);
}

check_cwd_changed ();
}

public void run_program (string program_string, string? working_directory) {
Expand Down Expand Up @@ -609,5 +616,13 @@ namespace Terminal {
reset (true, true);
active_shell (old_loc);
}

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