From 3d483fd0f46aa766710044384a6ec9d1d01fd556 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Thu, 1 May 2025 14:41:00 +1200 Subject: [PATCH] esvapi: modify searchFormat() to take a function --- esvapi.d | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/esvapi.d b/esvapi.d index 78fa28c..fd6fb11 100644 --- a/esvapi.d +++ b/esvapi.d @@ -178,6 +178,12 @@ verseValid(in char[] verse) 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, + 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 - searchFormat(alias fmt = "\033[1m%s\033[0m\n %s\n") - (in string query, int lineLength = 0) /* 0 means default */ + searchFormat(in string query, + string function(string, string) fmt = &defaultSearchFmt) { char[] layout; JSONValue resp; @@ -334,15 +341,9 @@ struct ESVApi enforce!ESVException(resp["total"].integer != 0, "No results for search"); - lineLength = lineLength == 0 ? 80 : lineLength; - () @trusted { foreach (JSONValue item; resp["results"].array) { - layout ~= format!fmt( - item["reference"].str, - item["content"].str - .wrap(lineLength) - ); + layout ~= fmt(item["reference"].str, item["content"].str); } }();