esvapi: modify searchFormat() to take a function
This commit is contained in:
parent
94a954d634
commit
3d483fd0f4
1 changed files with 11 additions and 10 deletions
21
esvapi.d
21
esvapi.d
|
@ -178,6 +178,12 @@ verseValid(in char[] verse)
|
||||||
assert(verseValid("15:12-17"));
|
assert(verseValid("15:12-17"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string
|
||||||
|
defaultSearchFmt(string reference, string content) pure
|
||||||
|
{
|
||||||
|
return format!"\033[1m%s\033[0m\n %s\n"(reference, content.wrap(80));
|
||||||
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
+ Structure containing the authentication key, API URL,
|
+ Structure containing the authentication key, API URL,
|
||||||
+ any parameters to use when making a request as well as the
|
+ any parameters to use when making a request as well as the
|
||||||
|
@ -319,11 +325,12 @@ struct ESVApi
|
||||||
}
|
}
|
||||||
|
|
||||||
/++
|
/++
|
||||||
+ Calls search() and formats the results nicely as plain text.
|
+ Calls search() and formats the results nicely as plain text,
|
||||||
|
+ unless a custom function is provided.
|
||||||
+/
|
+/
|
||||||
string
|
string
|
||||||
searchFormat(alias fmt = "\033[1m%s\033[0m\n %s\n")
|
searchFormat(in string query,
|
||||||
(in string query, int lineLength = 0) /* 0 means default */
|
string function(string, string) fmt = &defaultSearchFmt)
|
||||||
{
|
{
|
||||||
char[] layout;
|
char[] layout;
|
||||||
JSONValue resp;
|
JSONValue resp;
|
||||||
|
@ -334,15 +341,9 @@ struct ESVApi
|
||||||
enforce!ESVException(resp["total"].integer != 0,
|
enforce!ESVException(resp["total"].integer != 0,
|
||||||
"No results for search");
|
"No results for search");
|
||||||
|
|
||||||
lineLength = lineLength == 0 ? 80 : lineLength;
|
|
||||||
|
|
||||||
() @trusted {
|
() @trusted {
|
||||||
foreach (JSONValue item; resp["results"].array) {
|
foreach (JSONValue item; resp["results"].array) {
|
||||||
layout ~= format!fmt(
|
layout ~= fmt(item["reference"].str, item["content"].str);
|
||||||
item["reference"].str,
|
|
||||||
item["content"].str
|
|
||||||
.wrap(lineLength)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue