Skip to content

Commit

Permalink
(GH-663) Search by Id Starts With
Browse files Browse the repository at this point in the history
As an additional enhancement to searching by id, allow searching where
the id starts with the filter. This adds to tab completion.

The filters should be applied prior to searching by all versions gets
returned, so move the filters up.

Ensure the search is case-insensitive if it is local. A remote search
is case insensitive if the remote server supports it.
  • Loading branch information
ferventcoder committed Mar 22, 2016
1 parent ea161cc commit 8a689bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
.Add("e|exact",
"Exact - Only return packages with this exact name.",
option => configuration.ListCommand.Exact = option != null)
.Add("id|idonly|id-only",
"ByIdOnly - Only return packages with the filter being part of the id.",
.Add("by-id-only",
"ByIdOnly - Only return packages where the id contains the search filter.",
option => configuration.ListCommand.ByIdOnly = option != null)
.Add("id-starts-with",
"IdStartsWith - Only return packages where the id starts with the search filter.",
option => configuration.ListCommand.IdStartsWith = option != null)
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ public ListCommandConfiguration()
public int PageSize { get; set; }
public bool Exact { get; set; }
public bool ByIdOnly { get; set; }
public bool IdStartsWith { get; set; }
}

[Serializable]
Expand Down
41 changes: 26 additions & 15 deletions src/chocolatey/infrastructure.app/nuget/NugetList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -57,6 +58,31 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati

IQueryable<IPackage> results = packageRepository.Search(configuration.Input, configuration.Prerelease);


if (configuration.ListCommand.Page.HasValue)
{
results = results.Skip(configuration.ListCommand.PageSize * configuration.ListCommand.Page.Value).Take(configuration.ListCommand.PageSize);
}

if (configuration.ListCommand.Exact)
{
results = results.Where(p => p.Id == configuration.Input);
}

if (configuration.ListCommand.ByIdOnly)
{
results = isRemote ?
results.Where(p => p.Id.Contains(configuration.Input))
: results.Where(p => p.Id.contains(configuration.Input, StringComparison.OrdinalIgnoreCase));
}

if (configuration.ListCommand.IdStartsWith)
{
results = isRemote ?
results.Where(p => p.Id.StartsWith(configuration.Input))
: results.Where(p => p.Id.StartsWith(configuration.Input, StringComparison.OrdinalIgnoreCase));
}

if (configuration.AllVersions)
{
if (isRemote)
Expand Down Expand Up @@ -88,21 +114,6 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
.AsQueryable();
}

if (configuration.ListCommand.Page.HasValue)
{
results = results.Skip(configuration.ListCommand.PageSize * configuration.ListCommand.Page.Value).Take(configuration.ListCommand.PageSize);
}

if (configuration.ListCommand.Exact)
{
results = results.Where(p => p.Id == configuration.Input);
}

if (configuration.ListCommand.ByIdOnly)
{
results = results.Where(p => p.Id.Contains(configuration.Input));
}

return results.OrderBy(p => p.Id);
}

Expand Down

0 comments on commit 8a689bf

Please sign in to comment.