comparison src/main/kotlin/name/blackcap/passman/Console.kt @ 21:ea65ab890f66

More work to support interactive feature.
author David Barts <n5jrn@me.com>
date Tue, 02 Jul 2024 11:27:39 -0700
parents a6cfdffcaa94
children
comparison
equal deleted inserted replaced
20:4391afcf6bd0 21:ea65ab890f66
31 private fun _getPassword(prompt: String): CharArray = 31 private fun _getPassword(prompt: String): CharArray =
32 doConsoleIo({ System.console()?.readPassword(prompt) }, "unable to read password") 32 doConsoleIo({ System.console()?.readPassword(prompt) }, "unable to read password")
33 33
34 private fun <T> must(getter: () -> T, checker: (T) -> Boolean): T { 34 private fun <T> must(getter: () -> T, checker: (T) -> Boolean): T {
35 while (true) { 35 while (true) {
36 var got = getter() 36 val got = getter()
37 if (checker(got)) { 37 if (checker(got)) {
38 return got 38 return got
39 } 39 }
40 error("entry must not be empty, try again") 40 error("entry must not be empty, try again")
41 } 41 }
42 } 42 }
43 43
44 private fun <T> doConsoleIo(getter: () -> T?, message: String): T { 44 private fun <T> doConsoleIo(getter: () -> T?, message: String): T {
45 val ret = getter() 45 val ret = getter() ?: throw ConsoleException(message)
46 if (ret == null) { 46 return ret
47 die(message)
48 }
49 return ret!!
50 } 47 }
48
49 class ConsoleException(message: String, cause: Throwable? = null) : MessagedException(message, cause)