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
{
public class CMDHistory
public class Settings
{
public Shell Shell { get; set; } = Shell.CMD;
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 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">
<Grid Margin="10" VerticalAlignment="Top" >
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<CheckBox x:Name="cbReplaceWinR" Content="{DynamicResource wox_plugin_cmd_relace_winr}" ></CheckBox>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0 10 0 0">
<CheckBox x:Name="cbLeaveCmdOpen" Content="{DynamicResource wox_plugin_cmd_leave_cmd_open}">
</CheckBox>
</StackPanel>
<CheckBox Grid.Row="0" x:Name="ReplaceWinR" Content="{DynamicResource wox_plugin_cmd_relace_winr}" Margin="10" HorizontalAlignment="Left"/>
<CheckBox Grid.Row="1" x:Name="LeaveShellOpen" Content="{DynamicResource wox_plugin_cmd_leave_cmd_open}" Margin="10" HorizontalAlignment="Left"/>
<ComboBox Grid.Row="2" x:Name="ShellComboBox" Margin="10" HorizontalAlignment="Left" >
<ComboBoxItem>CMD</ComboBoxItem>
<ComboBoxItem>PowerShell</ComboBoxItem>
<ComboBoxItem>RunCommand</ComboBoxItem>
</ComboBox>
</Grid>
</Border>
</UserControl>

View File

@ -5,9 +5,9 @@ namespace Wox.Plugin.CMD
{
public partial class CMDSetting : UserControl
{
private readonly CMDHistory _settings;
private readonly Settings _settings;
public CMDSetting(CMDHistory settings)
public CMDSetting(Settings settings)
{
InitializeComponent();
_settings = settings;
@ -15,27 +15,35 @@ namespace Wox.Plugin.CMD
private void CMDSetting_OnLoaded(object sender, RoutedEventArgs re)
{
cbReplaceWinR.IsChecked = _settings.ReplaceWinR;
cbLeaveCmdOpen.IsChecked = _settings.LeaveCmdOpen;
ReplaceWinR.IsChecked = _settings.ReplaceWinR;
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;
};
cbReplaceWinR.Unchecked += (o, e) =>
ReplaceWinR.Unchecked += (o, e) =>
{
_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 readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
private readonly CMDHistory _settings;
private readonly PluginJsonStorage<CMDHistory> _storage;
private readonly Settings _settings;
private readonly PluginJsonStorage<Settings> _storage;
public CMD()
{
_storage = new PluginJsonStorage<CMDHistory>();
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
}
@ -79,7 +79,7 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png",
Action = c =>
{
ExecuteCMD(m);
ExecuteCommand(m);
return true;
}
}));
@ -112,7 +112,7 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png",
Action = c =>
{
ExecuteCMD(m.Key);
ExecuteCommand(m.Key);
return true;
}
};
@ -131,7 +131,7 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png",
Action = c =>
{
ExecuteCMD(cmd);
ExecuteCommand(cmd);
return true;
}
};
@ -149,28 +149,73 @@ namespace Wox.Plugin.CMD
IcoPath = "Images/cmd.png",
Action = c =>
{
ExecuteCMD(m.Key);
ExecuteCommand(m.Key);
return true;
}
}).Take(5);
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";
var info = new ProcessStartInfo
command = command.Trim();
command = Environment.ExpandEnvironmentVariables(command);
ProcessStartInfo info;
if (_settings.Shell == Shell.CMD)
{
UseShellExecute = true,
FileName = "cmd.exe",
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Arguments = arguments,
Verb = runAsAdministrator ? "runas" : ""
};
var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
info = new ProcessStartInfo
{
FileName = "cmd.exe",
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
{
Process.Start(info);
_settings.AddCmdHistory(cmd);
_settings.AddCmdHistory(command);
}
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)
{
this.context = context;
@ -237,7 +315,7 @@ namespace Wox.Plugin.CMD
Action = c =>
{
context.API.HideApp();
ExecuteCMD(selectedResult.Title, true);
ExecuteCommand(selectedResult.Title, true);
return true;
},
IcoPath = "Images/cmd.png"

View File

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