diff src/main/kotlin/name/blackcap/passman/ListSubcommand.kt @ 7:f245b9a53495

Efficiency improvements.
author David Barts <n5jrn@me.com>
date Tue, 20 Sep 2022 21:54:32 -0700
parents 711cc42e96d7
children 698c4a3d758d
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/ListSubcommand.kt	Tue Sep 20 20:52:21 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/ListSubcommand.kt	Tue Sep 20 21:54:32 2022 -0700
@@ -35,7 +35,7 @@
         val REDACTED = "(redacted)".toCharArray()
     }
     private lateinit var commandLine: CommandLine
-    private val matchers = mutableMapOf<String, MutableList<(Any) -> Boolean>>()
+    private val matchers = mutableMapOf<String, MutableList<(Any?) -> Boolean>>()
 
     private data class OptionDescriptor(val name: String, val help: String)
 
@@ -75,7 +75,7 @@
                 }
                 try {
                     if (it.name !in matchers) {
-                        matchers[it.name] = mutableListOf<(Any) -> Boolean>()
+                        matchers[it.name] = mutableListOf<(Any?) -> Boolean>()
                     }
                     matchers[it.name]!! += { x -> x is String && x.contains(Regex(value, regexOptions)) }
                 } catch (e: PatternSyntaxException) {
@@ -101,7 +101,7 @@
                     throw RuntimeException("will never happen")
                 }
                 if (it.name !in matchers) {
-                    matchers[it.name] = mutableListOf<(Any) -> Boolean>()
+                    matchers[it.name] = mutableListOf<(Any?) -> Boolean>()
                 }
                 when(op) {
                     '>' -> matchers[it.name]!! += { x -> x is Long && x > value }
@@ -121,21 +121,21 @@
             val printer = if (commandLine.hasOption(LONG)) Entry::printLong else Entry::print
             var count = 0;
             while (results.next()) {
+                val entry = Entry(
+                    name = results.getDecryptedString(1, db.encryption),
+                    username = results.getDecryptedString(2, db.encryption),
+                    password = REDACTED,
+                    notes = results.getDecryptedString(3, db.encryption),
+                    created = results.getDate(4),
+                    modified = results.getDate(5),
+                    accessed = results.getDate(6)
+                )
                 val passed = matchers
-                    .map { entry -> entry.value.fold(true) {
-                            acc, pred -> acc && pred(results.getDecryptedString(entry.key, db.encryption)) } }
+                    .map { x -> x.value.fold(true) {
+                            acc, pred -> acc && pred(entry.getField(x.key)) } }
                     .reduceOrNull() { a, b -> a && b }
                     ?: true
                 if (passed) {
-                    val entry = Entry(
-                        name = results.getDecryptedString(1, db.encryption),
-                        username = results.getDecryptedString(2, db.encryption),
-                        password = REDACTED,
-                        notes = results.getDecryptedString(3, db.encryption),
-                        created = results.getDate(4),
-                        modified = results.getDate(5),
-                        accessed = results.getDate(6)
-                    )
                     if (count > 0) {
                         println()
                     }
@@ -151,7 +151,3 @@
         }
     }
 }
-
-private fun Options.addMultiOption(opt: String?, longOpt: String, hasArg: Boolean, description: String): Unit {
-    addOption(Option(opt, longOpt, hasArg, description).apply { args = Option.UNLIMITED_VALUES })
-}
\ No newline at end of file