From 5a62427bbe06fe88fad9e3001fd024883bf540a0 Mon Sep 17 00:00:00 2001 From: shobu13 Date: Wed, 6 Apr 2022 03:03:11 +0200 Subject: [PATCH 1/2] add basic windows terminal support add a basic terminal for Windows Terminal, with the current version, you can set Windows Terminal as your default terminal emulator, but if you launch a command using Flow, Windows Terminal don't use your the matching profile, but a default profile instead. So i've added a "use windows terminal" property to specify usage of WT, and a Textbox where you can input the Profile you want to use. Then, on query process, the plugin call WT, passing the Profile as -p option, followed by the actual query. --- .../Languages/en.xaml | 1 + Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 12 +++++ .../Flow.Launcher.Plugin.Shell/Settings.cs | 14 +++-- .../ShellSetting.xaml | 16 +++++- .../ShellSetting.xaml.cs | 51 +++++++++++++++---- 5 files changed, 77 insertions(+), 17 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Shell/Languages/en.xaml index 8b312bc93d5..7ade49da646 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Shell/Languages/en.xaml @@ -5,6 +5,7 @@ Replace Win+R Do not close Command Prompt after command execution Always run as administrator + Use Windows Terminal Run as different user Shell Allows to execute system commands from Flow Launcher. Commands should start with > diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index bed46425dc1..75737d52443 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -252,6 +252,18 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin throw new NotImplementedException(); } + if (_settings.UseWindowsTerminal) + { + ProcessStartInfo wtInfo = new(); + wtInfo.FileName = "wt.exe"; + wtInfo.ArgumentList.Add("-p"); + wtInfo.ArgumentList.Add(_settings.WindowsTerminalProfile); + wtInfo.ArgumentList.Add(info.FileName); + wtInfo.ArgumentList.Add(info.FileName + " " + String.Join(" ", info.ArgumentList)); + + info = wtInfo; + } + info.UseShellExecute = true; _settings.AddCmdHistory(command); diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs b/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs index a3cac1cb873..788c9b90b3a 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs @@ -5,18 +5,24 @@ namespace Flow.Launcher.Plugin.Shell public class Settings { public Shell Shell { get; set; } = Shell.Cmd; - + public bool ReplaceWinR { get; set; } = false; - + public bool LeaveShellOpen { get; set; } public bool RunAsAdministrator { get; set; } = true; + public bool UseWindowsTerminal { get; set; } = false; + + public string WindowsTerminalProfile { get; set; } + public bool ShowOnlyMostUsedCMDs { get; set; } public int ShowOnlyMostUsedCMDsNumber { get; set; } - public Dictionary CommandHistory { get; set; } = new Dictionary(); + public Dictionary CommandHistory + { get; set; + } = new Dictionary(); public void AddCmdHistory(string cmdName) { @@ -36,6 +42,6 @@ public enum Shell Cmd = 0, Powershell = 1, RunCommand = 2, - + WindowsTerminal = 3 } } diff --git a/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml b/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml index 39fb21c59cb..67126cf5d3f 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml +++ b/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml @@ -15,6 +15,7 @@ + + + + + + CMD PowerShell RunCommand - + () { 5, 10, 20 }; + ShowOnlyMostUsedCMDsNumber.ItemsSource = + new List() { 5, 10, 20 }; if (_settings.ShowOnlyMostUsedCMDsNumber == 0) { ShowOnlyMostUsedCMDsNumber.SelectedIndex = 0; - _settings.ShowOnlyMostUsedCMDsNumber = (int)ShowOnlyMostUsedCMDsNumber.SelectedItem; + _settings.ShowOnlyMostUsedCMDsNumber = + (int) ShowOnlyMostUsedCMDsNumber.SelectedItem; } LeaveShellOpen.Checked += (o, e) => @@ -58,6 +67,25 @@ private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re) _settings.RunAsAdministrator = false; }; + UseWindowsTerminal.Checked += (o, e) => + { + _settings.UseWindowsTerminal = true; + + WindowsTerminalProfile.IsEnabled = true; + }; + + UseWindowsTerminal.Unchecked += (o, e) => + { + _settings.UseWindowsTerminal = false; + + WindowsTerminalProfile.IsEnabled = false; + }; + + WindowsTerminalProfile.TextChanged += (o, e) => + { + _settings.WindowsTerminalProfile = WindowsTerminalProfile.Text; + }; + ReplaceWinR.Checked += (o, e) => { _settings.ReplaceWinR = true; @@ -89,12 +117,13 @@ private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re) ShowOnlyMostUsedCMDsNumber.IsEnabled = false; }; - ShowOnlyMostUsedCMDsNumber.SelectedItem = _settings.ShowOnlyMostUsedCMDsNumber; + ShowOnlyMostUsedCMDsNumber.SelectedItem = + _settings.ShowOnlyMostUsedCMDsNumber; ShowOnlyMostUsedCMDsNumber.SelectionChanged += (o, e) => { - _settings.ShowOnlyMostUsedCMDsNumber = (int)ShowOnlyMostUsedCMDsNumber.SelectedItem; + _settings.ShowOnlyMostUsedCMDsNumber = + (int) ShowOnlyMostUsedCMDsNumber.SelectedItem; }; - } } } From 22dab6de126478fccbd564c62b46f8464b65068d Mon Sep 17 00:00:00 2001 From: shobu13 Date: Wed, 6 Apr 2022 05:00:46 +0200 Subject: [PATCH 2/2] improvement and Powershell 7 support forget to add Verb & working directory to Windows Terminal section, + iterate on info.Arguments instead of parsing it to String --- Plugins/Flow.Launcher.Plugin.Shell/Main.cs | 24 ++++++++++++++++++- .../Flow.Launcher.Plugin.Shell/Settings.cs | 4 ++-- .../ShellSetting.xaml | 1 + 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs index 75737d52443..c0412e09561 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Main.cs @@ -224,6 +224,20 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin break; } + case Shell.Pwsh: + { + info.FileName = "pwsh.exe"; + if (_settings.LeaveShellOpen) + { + info.ArgumentList.Add("-NoExit"); + } + + info.ArgumentList.Add("-Command"); + info.ArgumentList.Add(command); + + break; + } + case Shell.RunCommand: { var parts = command.Split(new[] { ' ' }, 2); @@ -258,8 +272,16 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin wtInfo.FileName = "wt.exe"; wtInfo.ArgumentList.Add("-p"); wtInfo.ArgumentList.Add(_settings.WindowsTerminalProfile); + wtInfo.ArgumentList.Add(info.FileName); - wtInfo.ArgumentList.Add(info.FileName + " " + String.Join(" ", info.ArgumentList)); + + foreach (var argument in info.ArgumentList) + { + wtInfo.ArgumentList.Add(argument); + } + + wtInfo.WorkingDirectory = info.WorkingDirectory; + wtInfo.Verb = info.Verb; info = wtInfo; } diff --git a/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs b/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs index 788c9b90b3a..f2b7dba19ad 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs +++ b/Plugins/Flow.Launcher.Plugin.Shell/Settings.cs @@ -41,7 +41,7 @@ public enum Shell { Cmd = 0, Powershell = 1, - RunCommand = 2, - WindowsTerminal = 3 + Pwsh = 2, + RunCommand = 3 } } diff --git a/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml b/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml index 67126cf5d3f..00cf1dd49b4 100644 --- a/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml +++ b/Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml @@ -53,6 +53,7 @@ HorizontalAlignment="Left"> CMD PowerShell + Pwsh RunCommand