Auto Complete for CMD mode

This commit is contained in:
Yeechan Lu
2014-03-20 04:12:50 +08:00
parent 4fc1169fa3
commit 8b153121ac
2 changed files with 89 additions and 9 deletions

View File

@@ -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<Result> 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<string> 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;
}

View File

@@ -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;