Skip to content

Commit

Permalink
Fix unwanted extra tab (#743)
Browse files Browse the repository at this point in the history
* Sanitize commandline path

* Application: only add tab when requested

* Fix regression of -t option

* Clarify boolean logic

* Clarify -t option description

* Fix CI

* Fix regression and CI

* Simplify and clarify

 - Restore tabs for all first window
 - Simplify MainWindow constructor
 - DRY

* Get wd from ApplicationCommandLine; fix test

* Revert sanitize

* Ensure new tab if -w option present

* Sanitize working directory in handle_local_options

* reduce diff

---------

Co-authored-by: Gustavo Marques <[email protected]>
  • Loading branch information
jeremypw and Marukesu authored Jan 15, 2024
1 parent 5c226d5 commit 501e689
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
31 changes: 16 additions & 15 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Terminal.Application : Gtk.Application {
// -n flag forces a new window
add_main_option ("new-window", 'n', 0, OptionArg.NONE, _("Open a new terminal window"), null);
// -t flag forces a new tab
add_main_option ("new-tab", 't', 0, OptionArg.NONE, _("Open a new terminal tab"), null);
add_main_option ("new-tab", 't', 0, OptionArg.NONE, _("Open a new terminal tab at current working directory"), null);
// -w flag defines the working directory that the new tab/window uses
add_main_option ("working-directory", 'w', 0, OptionArg.FILENAME, _("Set shell working directory"), "DIR");
// -e flag is used for running single strings as a command line
Expand Down Expand Up @@ -108,7 +108,10 @@ public class Terminal.Application : Gtk.Application {

if (options.lookup ("working-directory", "^&ay", out working_directory)) {
if (working_directory != "\0") {
Environment.set_current_dir (working_directory); // will be sent via platform-data
Environment.set_current_dir (
Utils.sanitize_path (working_directory, Environment.get_current_dir (), false)
); // will be sent via platform-data
options.insert ("new-tab", "b", true);
}

options.remove ("working-directory");
Expand Down Expand Up @@ -203,7 +206,9 @@ public class Terminal.Application : Gtk.Application {
);

var new_window_action = new SimpleAction ("new-window", null);
new_window_action.activate.connect (new_window);
new_window_action.activate.connect (() => {
new MainWindow (this, active_window == null).present ();
});

var quit_action = new SimpleAction ("quit", null);
quit_action.activate.connect (close);
Expand All @@ -228,24 +233,24 @@ public class Terminal.Application : Gtk.Application {
protected override int command_line (ApplicationCommandLine command_line) {
unowned var options = command_line.get_options_dict ();
var window = (MainWindow) active_window;
var is_first_window = window == null;
bool new_window;

if (window == null || options.lookup ("new-window", "b", out new_window) && new_window) {
/* Uncertain whether tabs should be restored when app is launched with working directory from commandline.
* Currently they are set to restore (subject to the restore-tabs setting).
* If it is desired that tabs should never be restored in these circimstances add another check below.
*/
bool restore_tabs = !("commandline" in options || "execute" in options) || window == null;
window = new MainWindow (this, restore_tabs);
// Always restore tabs if creating first window, but no extra tab at this stage
if (is_first_window || options.lookup ("new-window", "b", out new_window) && new_window) {
window = new MainWindow (this, is_first_window);
}

// If a specified working directory is not requested, use the current working directory from the commandline
unowned var working_directory = command_line.get_cwd ();
unowned string[] commands;
unowned string command;
bool new_tab, minimized;

options.lookup ("new-tab", "b", out new_tab);

// If "execute" option or "commandline" option used ignore any "new-tab option
// because these add new tab(s) already
if (options.lookup ("execute", "^a&ay", out commands)) {
for (var i = 0; commands[i] != null; i++) {
if (commands[i] != "\0") {
Expand All @@ -254,7 +259,7 @@ public class Terminal.Application : Gtk.Application {
}
} else if (options.lookup ("commandline", "^&ay", out command) && command != "\0") {
window.add_tab_with_working_directory (working_directory, command, new_tab);
} else {
} else if (new_tab || window.terminals.is_empty ()) {
window.add_tab_with_working_directory (working_directory, null, new_tab);
}

Expand All @@ -275,10 +280,6 @@ public class Terminal.Application : Gtk.Application {
base.dbus_unregister (connection, path);
}

private void new_window () {
new MainWindow (this, active_window == null).present ();
}

private void close () {
foreach (var window in get_windows ()) {
window.close (); // if all windows is closed, the main loop will stop automatically.
Expand Down
8 changes: 1 addition & 7 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,11 @@ namespace Terminal {
{ ACTION_OPEN_IN_BROWSER, action_open_in_browser }
};

public MainWindow (Terminal.Application app, bool recreate_tabs = true, bool ensure_tab = true) {
public MainWindow (Terminal.Application app, bool recreate_tabs = true) {
Object (
app: app,
recreate_tabs: recreate_tabs
);

if (!recreate_tabs && ensure_tab) {
new_tab ("");
}
}

static construct {
Expand Down Expand Up @@ -214,7 +210,6 @@ namespace Terminal {
/* 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 */
string? location = null;

if (directory == null || directory == "") {
if (notebook.tabs.first () == null || command != null || create_new_tab) { //Ensure at least one tab
new_tab ("", command);
Expand Down Expand Up @@ -494,7 +489,6 @@ namespace Terminal {
Idle.add (() => {
var new_window = new MainWindow (
app,
false,
false
);

Expand Down
14 changes: 10 additions & 4 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@


namespace Terminal.Utils {
public string? sanitize_path (string _path, string shell_location) {
public string? sanitize_path (string _path, string shell_location, bool add_file_scheme = true) {
/* Remove trailing whitespace, ensure scheme, substitute leading "~" and "..", remove extraneous "/" */
string scheme, path;
string scheme = "", path = "";

var parts_scheme = _path.split ("://", 2);
if (parts_scheme.length == 2) {
scheme = parts_scheme[0] + "://";
path = parts_scheme[1];
} else {
scheme = "file://";
} else if (parts_scheme.length == 1 ) {
if (add_file_scheme) {
scheme = "file://";
}

path = _path;
} else {
critical ("Invalid path");
return null;
}

path = Uri.unescape_string (path);
Expand Down

0 comments on commit 501e689

Please sign in to comment.