Implemented search.
h3rald h3rald@h3rald.com
Thu, 30 Nov 2023 13:47:48 +0100
2 files changed,
38 insertions(+),
4 deletions(-)
M
min.nim
→
min.nim
@@ -160,6 +160,7 @@ init Sets up the current directory as a managed min module.
install [<module> <version>] Install the specified managed min module or all dependent modules. uninstall [<module> <version>] Uninstall the specified managed min module or all dependent modules. update [<module> <version>] Update the specified managed min module or all dependent modules. + search [...terms...] Search for a module matching the specified terms. Options: -a, --asset-path Specify a directory containing the asset files to include in the compiled executable (if -c is set)@@ -291,8 +292,10 @@ let name = args[1]
let version = args[2] executeMmmCmd(proc () = MMM.update(name, version, GLOBAL)) elif file == "search": - logging.error "[search] Not implemented." - quit(100) + var str = "" + if args.len > 1: + str = args[1 .. ^1].join(" ") + executeMmmCmd(proc () = MMM.search(str)) minFile fn, op elif SIMPLEREPL: minSimpleRepl()
M
minpkg/core/mmm.nim
→
minpkg/core/mmm.nim
@@ -4,7 +4,8 @@ os,
httpclient, strutils, sequtils, - logging + logging, + algorithm ] import env@@ -270,4 +271,34 @@ except CatchableError:
debug getCurrentExceptionMsg() warn "Update of module '$#@$#' failed." % [name, version] - +proc search*(MMM: var MinModuleManager, search="") = + let rateModule = proc(it: JsonNode): JsonNode = + var score = 0 + if it["name"].getStr.contains(search): + score += 4 + if it["description"].getStr.contains(search): + score += 2 + if it["author"].getStr.contains(search): + score += 1 + it["score"] = %score + return it + let sortModules = proc(x, y: JsonNode): int = + cmp(x["score"].getInt, y["score"].getInt) + let formatDeps = proc(deps: JsonNode): string = + result = deps.pairs.toSeq().mapIt("$#@$#" % [it.key, it.val.getStr]).join(", ") + if result == "": + result = "n/a" + var results = MMM.modules.getElems().map(rateModule).filterIt(it["score"].getInt > 0) + results.sort(sortModules) + var msg = "$# results found:" + if results.len == 1: + msg = "$# result found:" + elif results.len == 0: + msg = "No results found." + notice msg % [$results.len] + for m in results: + notice "-> $#" % [m["name"].getStr] + notice " Description: $#" % [m["description"].getStr] + notice " Author: $#" % [m["author"].getStr] + notice " License: $#" % [m["license"].getStr] + notice " Dependencies: $#" % [m["deps"].formatDeps]