diff --git a/Wox.Plugin.System/CMD.cs b/Wox.Plugin.System/CMD.cs index 59f0ea3316..7142ada54f 100644 --- a/Wox.Plugin.System/CMD.cs +++ b/Wox.Plugin.System/CMD.cs @@ -51,23 +51,85 @@ namespace Wox.Plugin.System return true; } }; + + try + { + if (File.Exists(cmd) || Directory.Exists(cmd)) + { + result.IcoPath = cmd; + } + } + catch (Exception) { } + results.Add(result); IEnumerable history = cmdHistory.Where(o => o.Key.Contains(cmd)) .OrderByDescending(o => o.Value) - .Select(m => new Result - { - Title = m.Key, - SubTitle = "this command has been executed " + m.Value + " times", - IcoPath = "Images/cmd.png", - Action = (c) => + .Select(m => { + if (m.Key == cmd) { - ExecuteCmd(m.Key); - return true; + result.SubTitle = "this command has been executed " + m.Value + " times"; + return null; } - }).Take(4); + + var ret = new Result + { + Title = m.Key, + SubTitle = "this command has been executed " + m.Value + " times", + IcoPath = "Images/cmd.png", + Action = (c) => + { + ExecuteCmd(m.Key); + return true; + } + }; + try + { + if (File.Exists(m.Key) || Directory.Exists(m.Key)) + { + ret.IcoPath = m.Key; + } + } + catch (Exception) { } + + return ret; + }).Where(o => o != null).Take(4); results.AddRange(history); + try + { + string basedir = null; + string dir = null; + string excmd = Environment.ExpandEnvironmentVariables(cmd); + if (Directory.Exists(excmd) && (cmd.EndsWith("/") || cmd.EndsWith(@"\"))) + { + basedir = excmd; + dir = cmd; + } + else if (Directory.Exists(Path.GetDirectoryName(excmd))) + { + basedir = Path.GetDirectoryName(excmd); + var dirn = Path.GetDirectoryName(cmd); + dir = (dirn.EndsWith("/") || dirn.EndsWith(@"\")) ? dirn : cmd.Substring(0, dirn.Length + 1); + } + + if (basedir != null) + { + List autocomplete = Directory.GetFileSystemEntries(basedir).Select(o => dir + Path.GetFileName(o)).Where(o => o.StartsWith(cmd, StringComparison.OrdinalIgnoreCase) && !results.Any(p => o.Equals(p.Title, StringComparison.OrdinalIgnoreCase))).ToList(); + autocomplete.Sort(); + results.AddRange(autocomplete.ConvertAll(m => new Result() { + Title = m, + SubTitle = "", + IcoPath = m, + Action = (c) => + { + ExecuteCmd(m); + return true; + } + })); + } + } + catch (Exception) { } } return results; } diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 5d3b392f7b..3eb3e35561 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -180,8 +180,11 @@ namespace Wox } private bool isCMDMode = false; + private bool ignoreTextChange = false; private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e) { + if (ignoreTextChange) { ignoreTextChange = false; return; } + toolTip.IsOpen = false; resultCtrl.Dirty = true; Dispatcher.DelayInvoke("UpdateSearch", @@ -310,6 +313,17 @@ namespace Wox tbQuery.CaretIndex = tbQuery.Text.Length; } + private void updateCmdMode() + { + var selected = resultCtrl.AcceptSelect(); + if (selected != null) + { + ignoreTextChange = true; + tbQuery.Text = ">" + selected.Title; + tbQuery.CaretIndex = tbQuery.Text.Length; + } + } + private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e) { //when alt is pressed, the real key should be e.SystemKey @@ -323,24 +337,28 @@ namespace Wox case Key.Down: resultCtrl.SelectNext(); + if (isCMDMode) updateCmdMode(); toolTip.IsOpen = false; e.Handled = true; break; case Key.Up: resultCtrl.SelectPrev(); + if (isCMDMode) updateCmdMode(); toolTip.IsOpen = false; e.Handled = true; break; case Key.PageDown: resultCtrl.SelectNextPage(); + if (isCMDMode) updateCmdMode(); toolTip.IsOpen = false; e.Handled = true; break; case Key.PageUp: resultCtrl.SelectPrevPage(); + if (isCMDMode) updateCmdMode(); toolTip.IsOpen = false; e.Handled = true; break;