From 238458b3fbe0a0ee6088594287aafe407e02f2e0 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 31 Oct 2023 09:23:59 +0000 Subject: [PATCH 01/13] Sanitize commandline path --- src/MainWindow.vala | 3 ++- src/Utils.vala | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 34622dc090..2413d54e75 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -276,7 +276,8 @@ namespace Terminal { return; } else { - location = directory; + // Substitute "~" etc but do not add the "file://" scheme + location = Utils.sanitize_path (directory, Environment.get_current_dir (), false); } /* We can match existing tabs only if there is no command and create_new_tab == false */ diff --git a/src/Utils.vala b/src/Utils.vala index 1926461ceb..1a4281fa3d 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -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); From 31bc9379251a985abf344252caa340a25d28d460 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 31 Oct 2023 09:25:28 +0000 Subject: [PATCH 02/13] Application: only add tab when requested --- src/Application.vala | 35 +++++++++++++++++++++-------------- src/MainWindow.vala | 5 ++--- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 311dc0a5b8..8336e582ce 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -108,8 +108,6 @@ public class Terminal.Application : Gtk.Application { if (working_directory != "\0") { Environment.set_current_dir (working_directory); // will be sent via platform-data } - - options.remove ("working-directory"); } if (options.lookup (OPTION_REMAINING, "^a&ay", out args)) { @@ -201,7 +199,10 @@ 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 (() => { + // Ensure at least one tab when window created by action + new MainWindow (this, active_window == null, true).present (); + }); var quit_action = new SimpleAction ("quit", null); quit_action.activate.connect (close); @@ -223,27 +224,37 @@ public class Terminal.Application : Gtk.Application { * 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); + bool restore_tabs = !("commandline" in options || "execute" in options || "working-directory" in options) || window == null; + window = new MainWindow (this, restore_tabs, false); } - unowned var working_directory = command_line.get_cwd (); unowned string[] commands; unowned string command; + unowned string wd; bool new_tab, minimized; options.lookup ("new-tab", "b", out new_tab); + options.lookup ("working-directory", "^&ay", out wd); + if (options.lookup ("execute", "^a&ay", out commands)) { for (var i = 0; commands[i] != null; i++) { if (commands[i] != "\0") { - window.add_tab_with_working_directory (working_directory, commands[i], new_tab); + window.add_tab_with_working_directory ( + wd != null ? wd : command_line.get_cwd (), + commands[i], + new_tab + ); } } } else if (options.lookup ("commandline", "^&ay", out command) && command != "\0") { - window.add_tab_with_working_directory (working_directory, command, new_tab); - } else { - window.add_tab_with_working_directory (working_directory, null, new_tab); + window.add_tab_with_working_directory ( + wd != null ? wd : command_line.get_cwd (), + command, + new_tab + ); + } else if (wd != null) { + window.add_tab_with_working_directory (wd, null, new_tab); } if (options.lookup ("minimized", "b", out minimized) && minimized) { @@ -263,10 +274,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. diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 2413d54e75..26fadbce09 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -105,13 +105,13 @@ namespace Terminal { { ACTION_OPEN_IN_BROWSER, action_open_in_browser } }; - public MainWindow (Terminal.Application app, bool recreate_tabs = true) { + public MainWindow (Terminal.Application app, bool recreate_tabs = true, bool ensure_tab = false) { Object ( app: app, recreate_tabs: recreate_tabs ); - if (!recreate_tabs) { + if (!recreate_tabs && ensure_tab) { new_tab (""); } } @@ -268,7 +268,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); From 1a188d084ec61ed4eacb9affa620c1e09a6fad40 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 10 Jan 2024 12:16:09 +0000 Subject: [PATCH 03/13] Fix regression of -t option --- src/Application.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.vala b/src/Application.vala index 4752716e51..c8fdd79972 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -265,7 +265,7 @@ public class Terminal.Application : Gtk.Application { command, new_tab ); - } else if (wd != null) { + } else { window.add_tab_with_working_directory (wd, null, new_tab); } From 193323cab949bb45422e6db9fe2245819ee05366 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 10 Jan 2024 12:16:35 +0000 Subject: [PATCH 04/13] Clarify boolean logic --- src/Application.vala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index c8fdd79972..08ab1d0fd6 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -231,7 +231,7 @@ public class Terminal.Application : Gtk.Application { var window = (MainWindow) active_window; bool new_window; - if (window == null || options.lookup ("new-window", "b", out new_window) && 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. @@ -248,7 +248,6 @@ public class Terminal.Application : Gtk.Application { options.lookup ("new-tab", "b", out new_tab); options.lookup ("working-directory", "^&ay", out wd); - if (options.lookup ("execute", "^a&ay", out commands)) { for (var i = 0; commands[i] != null; i++) { if (commands[i] != "\0") { From 0cc97a8e60ce08c652037cc4bcc1ce1c109e7307 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 10 Jan 2024 12:18:02 +0000 Subject: [PATCH 05/13] Clarify -t option description --- src/Application.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.vala b/src/Application.vala index 08ab1d0fd6..27cd859f60 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -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 From 3718ce74f625dac94e2c060389d207fe1293147f Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 10 Jan 2024 12:39:34 +0000 Subject: [PATCH 06/13] Fix CI --- src/Application.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.vala b/src/Application.vala index 27cd859f60..c1692d8cfe 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -265,7 +265,7 @@ public class Terminal.Application : Gtk.Application { new_tab ); } else { - window.add_tab_with_working_directory (wd, null, new_tab); + window.add_tab_with_working_directory (wd != null ? wd : command_line.get_cwd (), null, new_tab); } if (options.lookup ("minimized", "b", out minimized) && minimized) { From e65faf8e0866827462854f2eeeaea70c110acfaf Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Wed, 10 Jan 2024 12:49:43 +0000 Subject: [PATCH 07/13] Fix regression and CI --- src/Application.vala | 6 ++++-- src/tests/Application.vala | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index c1692d8cfe..9e91473815 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -246,7 +246,7 @@ public class Terminal.Application : Gtk.Application { bool new_tab, minimized; options.lookup ("new-tab", "b", out new_tab); - options.lookup ("working-directory", "^&ay", out wd); + var wd_option_present = options.lookup ("working-directory", "^&ay", out wd); if (options.lookup ("execute", "^a&ay", out commands)) { for (var i = 0; commands[i] != null; i++) { @@ -264,8 +264,10 @@ public class Terminal.Application : Gtk.Application { command, new_tab ); - } else { + } else if (wd_option_present) { window.add_tab_with_working_directory (wd != null ? wd : command_line.get_cwd (), null, new_tab); + } else if (new_tab) { + window.add_tab_with_working_directory (Environment.get_current_dir (), null, new_tab); } if (options.lookup ("minimized", "b", out minimized) && minimized) { diff --git a/src/tests/Application.vala b/src/tests/Application.vala index 150f66b2d0..b01f68c0d4 100644 --- a/src/tests/Application.vala +++ b/src/tests/Application.vala @@ -199,7 +199,7 @@ namespace Terminal.Test.Application { unowned var window = (MainWindow) application.active_window; assert_nonnull (window); var terminal_directory = window.current_terminal.get_shell_location (); - assert_cmpstr (terminal_directory, CompareOperator.EQ, working_directory); + assert_cmpstr (terminal_directory, CompareOperator.EQ, Environment.get_current_dir ()); }); }); From 97ce246d66bd08394e4e754dc7be5c9c01b7ef29 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 14 Jan 2024 16:48:12 +0000 Subject: [PATCH 08/13] Simplify and clarify - Restore tabs for all first window - Simplify MainWindow constructor - DRY --- src/Application.vala | 35 ++++++++++++++++++----------------- src/MainWindow.vala | 7 +------ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 9e91473815..8aa1abbd87 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -202,8 +202,7 @@ public class Terminal.Application : Gtk.Application { var new_window_action = new SimpleAction ("new-window", null); new_window_action.activate.connect (() => { - // Ensure at least one tab when window created by action - new MainWindow (this, active_window == null, true).present (); + new MainWindow (this, active_window == null).present (); }); var quit_action = new SimpleAction ("quit", null); @@ -229,30 +228,34 @@ 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; - bool new_window; + var is_first_window = window == null; + bool new_window_requested; + options.lookup ("new-window", "b", out new_window_requested); - 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 || "working-directory" in options) || window == null; - window = new MainWindow (this, restore_tabs, false); + if (is_first_window || new_window_requested) { + // Always restore tabs if creating first window, but no extra tab at this stage + window = new MainWindow (this, is_first_window); } unowned string[] commands; unowned string command; unowned string wd; bool new_tab, minimized; - options.lookup ("new-tab", "b", out new_tab); + + // If a specified working directory is not requested, use the current working directory from the commandline var wd_option_present = options.lookup ("working-directory", "^&ay", out wd); + if (wd == null) { + command_line.get_cwd (); + } + // 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") { window.add_tab_with_working_directory ( - wd != null ? wd : command_line.get_cwd (), + wd, commands[i], new_tab ); @@ -260,14 +263,12 @@ public class Terminal.Application : Gtk.Application { } } else if (options.lookup ("commandline", "^&ay", out command) && command != "\0") { window.add_tab_with_working_directory ( - wd != null ? wd : command_line.get_cwd (), + wd, command, new_tab ); - } else if (wd_option_present) { - window.add_tab_with_working_directory (wd != null ? wd : command_line.get_cwd (), null, new_tab); - } else if (new_tab) { - window.add_tab_with_working_directory (Environment.get_current_dir (), null, new_tab); + } else if (new_tab || window.terminals.length () == 0) { + window.add_tab_with_working_directory (wd, null, new_tab); } if (options.lookup ("minimized", "b", out minimized) && minimized) { diff --git a/src/MainWindow.vala b/src/MainWindow.vala index b4e8913315..650f9937fd 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -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 = false) { + 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 { @@ -494,7 +490,6 @@ namespace Terminal { Idle.add (() => { var new_window = new MainWindow ( app, - false, false ); From 9462bda6c271b3e8aed89cf4918556fe03a03e69 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 14 Jan 2024 17:10:59 +0000 Subject: [PATCH 09/13] Get wd from ApplicationCommandLine; fix test --- src/Application.vala | 9 ++++----- src/tests/Application.vala | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 8aa1abbd87..b02e1bb36c 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -124,6 +124,9 @@ public class Terminal.Application : Gtk.Application { options.insert ("commandline", "^&ay", commandline.escape ()); } + + options.remove ("working-directory"); + return -1; } @@ -239,16 +242,12 @@ public class Terminal.Application : Gtk.Application { unowned string[] commands; unowned string command; - unowned string wd; bool new_tab, minimized; options.lookup ("new-tab", "b", out new_tab); // If a specified working directory is not requested, use the current working directory from the commandline - var wd_option_present = options.lookup ("working-directory", "^&ay", out wd); - if (wd == null) { - command_line.get_cwd (); - } + var wd = command_line.get_cwd (); // 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)) { diff --git a/src/tests/Application.vala b/src/tests/Application.vala index b01f68c0d4..150f66b2d0 100644 --- a/src/tests/Application.vala +++ b/src/tests/Application.vala @@ -199,7 +199,7 @@ namespace Terminal.Test.Application { unowned var window = (MainWindow) application.active_window; assert_nonnull (window); var terminal_directory = window.current_terminal.get_shell_location (); - assert_cmpstr (terminal_directory, CompareOperator.EQ, Environment.get_current_dir ()); + assert_cmpstr (terminal_directory, CompareOperator.EQ, working_directory); }); }); From 46a7a8f2ce870f42330f890f6cf0b11d99565441 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 14 Jan 2024 17:32:14 +0000 Subject: [PATCH 10/13] Revert sanitize --- src/MainWindow.vala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 650f9937fd..582116ab3f 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -217,8 +217,7 @@ namespace Terminal { return; } else { - // Substitute "~" etc but do not add the "file://" scheme - location = Utils.sanitize_path (directory, Environment.get_current_dir (), false); + location = directory; } /* We can match existing tabs only if there is no command and create_new_tab == false */ From 158eea88234fcc09e039d726aec2011c601eed51 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 14 Jan 2024 17:58:53 +0000 Subject: [PATCH 11/13] Ensure new tab if -w option present --- src/Application.vala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Application.vala b/src/Application.vala index b02e1bb36c..c5f8576441 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -109,6 +109,7 @@ 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 + options.insert ("new-tab", "b", true); } } From dbd407431a32ce6a8b1f3d04efd7eeef6df8ba04 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 14 Jan 2024 18:16:55 +0000 Subject: [PATCH 12/13] Sanitize working directory in handle_local_options --- src/Application.vala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Application.vala b/src/Application.vala index c5f8576441..8ebef86daf 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -108,7 +108,9 @@ 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); } } From 5e76b381ee7777b25ffdcce95eaa176af93ee011 Mon Sep 17 00:00:00 2001 From: Gustavo Marques Date: Sun, 14 Jan 2024 17:31:49 -0300 Subject: [PATCH 13/13] reduce diff --- src/Application.vala | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index 8ebef86daf..5e95df98ef 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -113,6 +113,8 @@ public class Terminal.Application : Gtk.Application { ); // will be sent via platform-data options.insert ("new-tab", "b", true); } + + options.remove ("working-directory"); } if (options.lookup (OPTION_REMAINING, "^a&ay", out args)) { @@ -127,9 +129,6 @@ public class Terminal.Application : Gtk.Application { options.insert ("commandline", "^&ay", commandline.escape ()); } - - options.remove ("working-directory"); - return -1; } @@ -235,42 +234,33 @@ public class Terminal.Application : Gtk.Application { unowned var options = command_line.get_options_dict (); var window = (MainWindow) active_window; var is_first_window = window == null; - bool new_window_requested; - options.lookup ("new-window", "b", out new_window_requested); + bool new_window; - if (is_first_window || new_window_requested) { - // Always restore tabs if creating first window, but no extra tab at this stage + // 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 a specified working directory is not requested, use the current working directory from the commandline + options.lookup ("new-tab", "b", out new_tab); - var wd = command_line.get_cwd (); // 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") { - window.add_tab_with_working_directory ( - wd, - commands[i], - new_tab - ); + window.add_tab_with_working_directory (working_directory, commands[i], new_tab); } } } else if (options.lookup ("commandline", "^&ay", out command) && command != "\0") { - window.add_tab_with_working_directory ( - wd, - command, - new_tab - ); - } else if (new_tab || window.terminals.length () == 0) { - window.add_tab_with_working_directory (wd, null, new_tab); + window.add_tab_with_working_directory (working_directory, command, new_tab); + } else if (new_tab || window.terminals.is_empty ()) { + window.add_tab_with_working_directory (working_directory, null, new_tab); } if (options.lookup ("minimized", "b", out minimized) && minimized) {