From 966c83177ef5a3f58518ef6d26398cf4d55b395e Mon Sep 17 00:00:00 2001 From: MattMX Date: Fri, 28 Jun 2024 13:53:49 +0100 Subject: [PATCH] impl partial consumers --- .../arg/consumers/ArgumentConsumer.kt | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt index e84252a..ccee89e 100644 --- a/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt +++ b/api/src/main/kotlin/com/mattmx/ktgui/commands/declarative/arg/consumers/ArgumentConsumer.kt @@ -10,22 +10,27 @@ fun interface ArgumentConsumer { infix fun untilFalse(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor -> var current = processor.next() - val list = arrayListOf() - - if (current != null) { - list.add(current) - } + var fullString = current ?: "" - while (current != null && predicate(processor, current)) { + while (current != null) { current = processor.next() - if (current != null) { - list.add(current) + + if (current == null) { + return@ArgumentConsumer null + } + + fullString += " $current" + + if (!predicate(processor, fullString)) { + return@ArgumentConsumer fullString } } - list.joinToString(" ") + null } - infix fun until(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor -> + infix fun until(predicate: (ArgumentProcessor, String) -> Boolean) = untilFalse { p, s -> !predicate(p, s) } + + infix fun untilFalsePartial(predicate: (ArgumentProcessor, String) -> Boolean) = ArgumentConsumer { processor -> var current = processor.next() var fullString = current ?: "" @@ -38,13 +43,15 @@ fun interface ArgumentConsumer { fullString += " $current" - if (predicate(processor, fullString)) { + if (!predicate(processor, current)) { return@ArgumentConsumer fullString } } null } + infix fun untilPartial(predicate: (ArgumentProcessor, String) -> Boolean) = untilFalsePartial { p, s -> !predicate(p, s) } + fun remaining() = untilFalse { processor, _ -> processor.pointer < processor.args.size } infix fun variable(amount: Int): ArgumentConsumer {