Bring run command functionality back

#636
This commit is contained in:
bao-qian
2016-05-19 09:04:31 +01:00
parent 937ce34c36
commit f06c4f4049
5 changed files with 135 additions and 42 deletions

View File

@@ -2,10 +2,11 @@
namespace Wox.Plugin.CMD namespace Wox.Plugin.CMD
{ {
public class CMDHistory public class Settings
{ {
public Shell Shell { get; set; } = Shell.CMD;
public bool ReplaceWinR { get; set; } = true; public bool ReplaceWinR { get; set; } = true;
public bool LeaveCmdOpen { get; set; } public bool LeaveShellOpen { get; set; }
public Dictionary<string, int> Count = new Dictionary<string, int>(); public Dictionary<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName) public void AddCmdHistory(string cmdName)
@@ -20,4 +21,12 @@ namespace Wox.Plugin.CMD
} }
} }
} }
public enum Shell
{
CMD = 0,
Powershell = 1,
RunCommand = 2,
}
} }

View File

@@ -9,17 +9,17 @@
<Border BorderBrush="Gray" Margin="10" BorderThickness="1"> <Border BorderBrush="Gray" Margin="10" BorderThickness="1">
<Grid Margin="10" VerticalAlignment="Top" > <Grid Margin="10" VerticalAlignment="Top" >
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition></RowDefinition> <RowDefinition/>
<RowDefinition></RowDefinition> <RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal"> <CheckBox Grid.Row="0" x:Name="ReplaceWinR" Content="{DynamicResource wox_plugin_cmd_relace_winr}" Margin="10" HorizontalAlignment="Left"/>
<CheckBox x:Name="cbReplaceWinR" Content="{DynamicResource wox_plugin_cmd_relace_winr}" ></CheckBox> <CheckBox Grid.Row="1" x:Name="LeaveShellOpen" Content="{DynamicResource wox_plugin_cmd_leave_cmd_open}" Margin="10" HorizontalAlignment="Left"/>
</StackPanel> <ComboBox Grid.Row="2" x:Name="ShellComboBox" Margin="10" HorizontalAlignment="Left" >
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0 10 0 0"> <ComboBoxItem>CMD</ComboBoxItem>
<CheckBox x:Name="cbLeaveCmdOpen" Content="{DynamicResource wox_plugin_cmd_leave_cmd_open}"> <ComboBoxItem>PowerShell</ComboBoxItem>
<ComboBoxItem>RunCommand</ComboBoxItem>
</CheckBox> </ComboBox>
</StackPanel>
</Grid> </Grid>
</Border> </Border>
</UserControl> </UserControl>

View File

@@ -5,9 +5,9 @@ namespace Wox.Plugin.CMD
{ {
public partial class CMDSetting : UserControl public partial class CMDSetting : UserControl
{ {
private readonly CMDHistory _settings; private readonly Settings _settings;
public CMDSetting(CMDHistory settings) public CMDSetting(Settings settings)
{ {
InitializeComponent(); InitializeComponent();
_settings = settings; _settings = settings;
@@ -15,27 +15,35 @@ namespace Wox.Plugin.CMD
private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re) private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re)
{ {
cbReplaceWinR.IsChecked = _settings.ReplaceWinR; ReplaceWinR.IsChecked = _settings.ReplaceWinR;
cbLeaveCmdOpen.IsChecked = _settings.LeaveCmdOpen; LeaveShellOpen.IsChecked = _settings.LeaveShellOpen;
LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand;
cbLeaveCmdOpen.Checked += (o, e) => LeaveShellOpen.Checked += (o, e) =>
{ {
_settings.LeaveCmdOpen = true; _settings.LeaveShellOpen = true;
}; };
cbLeaveCmdOpen.Unchecked += (o, e) => LeaveShellOpen.Unchecked += (o, e) =>
{ {
_settings.LeaveCmdOpen = false; _settings.LeaveShellOpen = false;
}; };
cbReplaceWinR.Checked += (o, e) => ReplaceWinR.Checked += (o, e) =>
{ {
_settings.ReplaceWinR = true; _settings.ReplaceWinR = true;
}; };
cbReplaceWinR.Unchecked += (o, e) => ReplaceWinR.Unchecked += (o, e) =>
{ {
_settings.ReplaceWinR = false; _settings.ReplaceWinR = false;
}; };
ShellComboBox.SelectedIndex = (int) _settings.Shell;
ShellComboBox.SelectionChanged += (o, e) =>
{
_settings.Shell = (Shell) ShellComboBox.SelectedIndex;
LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand;
};
} }
} }
} }

View File

@@ -19,12 +19,12 @@ namespace Wox.Plugin.CMD
private bool WinRStroked; private bool WinRStroked;
private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator()); private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
private readonly CMDHistory _settings; private readonly Settings _settings;
private readonly PluginJsonStorage<CMDHistory> _storage; private readonly PluginJsonStorage<Settings> _storage;
public CMD() public CMD()
{ {
_storage = new PluginJsonStorage<CMDHistory>(); _storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load(); _settings = _storage.Load();
} }
@@ -79,7 +79,7 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = c => Action = c =>
{ {
ExecuteCMD(m); ExecuteCommand(m);
return true; return true;
} }
})); }));
@@ -112,7 +112,7 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = c => Action = c =>
{ {
ExecuteCMD(m.Key); ExecuteCommand(m.Key);
return true; return true;
} }
}; };
@@ -131,7 +131,7 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = c => Action = c =>
{ {
ExecuteCMD(cmd); ExecuteCommand(cmd);
return true; return true;
} }
}; };
@@ -149,28 +149,73 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png", IcoPath = "Images/cmd.png",
Action = c => Action = c =>
{ {
ExecuteCMD(m.Key); ExecuteCommand(m.Key);
return true; return true;
} }
}).Take(5); }).Take(5);
return history.ToList(); return history.ToList();
} }
private void ExecuteCMD(string cmd, bool runAsAdministrator = false) private void ExecuteCommand(string command, bool runAsAdministrator = false)
{ {
var arguments = _settings.LeaveCmdOpen ? $"/k {cmd}" : $"/c {cmd} & pause"; command = command.Trim();
var info = new ProcessStartInfo command = Environment.ExpandEnvironmentVariables(command);
ProcessStartInfo info;
if (_settings.Shell == Shell.CMD)
{ {
UseShellExecute = true, var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
FileName = "cmd.exe", info = new ProcessStartInfo
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), {
Arguments = arguments, FileName = "cmd.exe",
Verb = runAsAdministrator ? "runas" : "" Arguments = arguments,
}; };
}
else if (_settings.Shell == Shell.RunCommand)
{
var parts = command.Split(new[] { ' ' }, 2);
var filename = parts[0];
var path = FullPath(filename);
if (string.IsNullOrEmpty(path) || parts.Length == 1)
{
info = new ProcessStartInfo(command);
}
else
{
var arguemtns = parts[1];
info = new ProcessStartInfo
{
FileName = filename,
Arguments = arguemtns
};
}
}
else
{
string arguments;
if (_settings.LeaveShellOpen)
{
arguments = $"-NoExit \"{command}\"";
}
else
{
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
}
info = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = arguments
};
}
info.UseShellExecute = true;
info.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
info.Verb = runAsAdministrator ? "runas" : "";
try try
{ {
Process.Start(info); Process.Start(info);
_settings.AddCmdHistory(cmd); _settings.AddCmdHistory(command);
} }
catch (FileNotFoundException e) catch (FileNotFoundException e)
{ {
@@ -178,6 +223,39 @@ namespace Wox.Plugin.CMD
} }
} }
private string FullPath(string filename)
{
if (File.Exists(filename))
{
return filename;
}
else
{
var values = Environment.GetEnvironmentVariable("PATH");
if (values != null)
{
foreach (var path in values.Split(';'))
{
var fullPath1 = Path.Combine(path, filename);
var fullPath2 = Path.Combine(path, filename + ".exe");
if (File.Exists(fullPath1))
{
return fullPath1;
}
else if (File.Exists(fullPath2))
{
return fullPath2;
}
}
return string.Empty;
}
else
{
return string.Empty;
}
}
}
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
this.context = context; this.context = context;
@@ -237,7 +315,7 @@ namespace Wox.Plugin.CMD
Action = c => Action = c =>
{ {
context.API.HideApp(); context.API.HideApp();
ExecuteCMD(selectedResult.Title, true); ExecuteCommand(selectedResult.Title, true);
return true; return true;
}, },
IcoPath = "Images/cmd.png" IcoPath = "Images/cmd.png"

View File

@@ -204,8 +204,6 @@ namespace Wox.Helper
where TApplication: Application , ISingleInstanceApp where TApplication: Application , ISingleInstanceApp
{ {
public const string Restart = "Restart";
#region Private Fields #region Private Fields
/// <summary> /// <summary>