Permalink
Browse files

Added 'agner list --search'/'agner search' functionality

  • Loading branch information...
yrashk committed Jan 28, 2011
1 parent 6e75a53 commit cb4746d4efddc54b4a00820aa0cc7dc6e1a51c53
Showing with 61 additions and 27 deletions.
  1. +8 −0 README.md
  2. +53 −27 src/agner_main.erl
View
@@ -60,6 +60,7 @@ Command invocation
------------------
agner list [-d/--descriptions] [-p/--properties PROPERTY1,PROPERTY2]
+ [-s/--search SEARCH_TERM]
Will list all agner-packages. With the `-d` or `--descriptions`
option, it will also print out the descriptions of the packages, for
@@ -68,6 +69,13 @@ easy grepping to find relevant packages.
If `-p` or `--properties` with a comma-separated list of properties is specified, they will be also
included into each listing (when present).
+If `-s` or `--search` option is supplied, packages name, descriptions and keywords are matched against
+SEARCH_TERM and only matching items are shown.
+
+ agner search SEARCH_TERM [-d/--description] [-p/--properties PROPERTY1,PROPERTY2]
+
+This is an alias for `agner list -s`
+
agner spec PACKAGE [-v/--version package_version] [-b/--browser]
[-h/--homepage]
View
@@ -34,8 +34,17 @@ arg_proplist() ->
"List packages on stdout",
[
{descriptions, $d, "descriptions", {boolean, false}, "Show package descriptions"},
- {properties, $p, "properties", string, "Comma-separated list of properties to show"}
+ {properties, $p, "properties", string, "Comma-separated list of properties to show"},
+ {search, $s, "search", string, "Keyword to search"}
]}},
+ {"search",
+ {search,
+ "Search packages",
+ [
+ {search, undefined, undefined, string, "Keyword to search"},
+ {descriptions, $d, "descriptions", {boolean, false}, "Show package descriptions"},
+ {properties, $p, "properties", string, "Comma-separated list of properties to show"}
+ ]}},
{"fetch",
{fetch,
"Download a package",
@@ -134,35 +143,52 @@ handle_command(versions, Opts) ->
agner:versions(Package))])
end;
+handle_command(search, Opts) ->
+ handle_command(list, Opts);
+
handle_command(list, Opts) ->
ShowDescriptions = proplists:get_value(descriptions, Opts),
+ Search = proplists:get_value(search, Opts),
Properties = lists:map(fun list_to_atom/1, string:tokens(proplists:get_value(properties, Opts,""),",")),
- io:format("~s",[lists:usort(plists:map(fun (Name) ->
- Spec = agner:spec(Name),
- Result0 = case ShowDescriptions of
- true ->
- io_lib:format("~-40s ~s",[Name, proplists:get_value(description, Spec)]);
- false ->
- io_lib:format("~s",[Name])
- end,
- Result = case Properties of
- [] ->
- Result0;
- [_|_] ->
- [Result0|lists:map(fun (Prop) ->
- case lists:keyfind(Prop, 1, Spec) of
- false ->
- [];
- T ->
- Val = list_to_tuple(tl(tuple_to_list(T))),
- io_lib:format(" | ~s: ~p",[Prop,
- Val])
- end
- end, Properties)]
- end,
- Result ++ [$\n]
- end,agner:index()))
- ]);
+ io:format("~s",[lists:usort(plists:map(fun (Name) ->
+ Spec = agner:spec(Name),
+ Searchable = string:to_lower(lists:flatten([Name,proplists:get_value(description,Spec,[])|proplists:get_value(keywords,Spec,[])])),
+ Show =
+ case Search of
+ undefined ->
+ true;
+ [_|_] ->
+ string:rstr(Searchable, string:to_lower(Search)) > 0
+ end,
+ case Show of
+ true ->
+ Result0 = case ShowDescriptions of
+ true ->
+ io_lib:format("~-40s ~s",[Name, proplists:get_value(description, Spec)]);
+ false ->
+ io_lib:format("~s",[Name])
+ end,
+ Result = case Properties of
+ [] ->
+ Result0;
+ [_|_] ->
+ [Result0|lists:map(fun (Prop) ->
+ case lists:keyfind(Prop, 1, Spec) of
+ false ->
+ [];
+ T ->
+ Val = list_to_tuple(tl(tuple_to_list(T))),
+ io_lib:format(" | ~s: ~p",[Prop,
+ Val])
+ end
+ end, Properties)]
+ end,
+ Result ++ [$\n];
+ false ->
+ ""
+ end
+ end,agner:index()))
+ ]);
handle_command(fetch, Opts) ->
case proplists:get_value(package, Opts) of

0 comments on commit cb4746d

Please sign in to comment.