Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching is more robust and uses glob style #524

Merged
merged 2 commits into from
Aug 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions SuperPutty/Gui/QuickSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace SuperPutty.Gui
public partial class QuickSelector : Form
{
private static readonly ILog Log = LogManager.GetLogger(typeof(QuickSelector));
private static char[] sanitizeChars = { '*', '[', ',' };
private static char[] splitChars = { ' ', '.', '*' };

public QuickSelector()
{
Expand Down Expand Up @@ -43,13 +45,69 @@ void UpdateFilter()
this.DataView.RowFilter = String.Empty;
}
else
{
this.DataView.RowFilter = string.Format(
"[Name] LIKE '%{0}%' OR [Detail] LIKE '%{0}%'", this.textBoxData.Text.Replace("[", "[[]"));
{
this.DataView.RowFilter = FormatFilterString(this.textBoxData.Text);
}
this.Text = string.Format("{0} [{1}]", this.Options.BaseText, this.DataView.Count);
}

string FormatFilterString(string text)
{
string filter = "";

int i = 0;
int tokenCount = cleanTokens(text.Split(splitChars)).Length;

foreach (string token in cleanTokens(text.Split(splitChars)))
{
if (i > 0 && i < tokenCount)
{
filter += " AND ";
}

filter += string.Format("([Name] LIKE '%{0}%' OR [Detail] LIKE '%{0}%')", tokenSanitize(token));

i++;
}

return filter;
}

string[] cleanTokens(string[] tokens)
{
int i = 0;
foreach (string token in tokens)
{
if (token.Length > 0)
{
i++;
}
}

string[] result = new string[i];

i = 0;
foreach(string token in tokens)
{
if (token.Length > 0)
{
result[i] = token;
i++;
}
}
return result;
}

string tokenSanitize(string token)
{
foreach (char sanitizeChar in sanitizeChars)
{
token = token.Replace(Convert.ToString(sanitizeChar), "");
}

return token;
}

protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
Expand Down