adding VarText and removing text minimum length

This commit is contained in:
2021-06-27 22:24:53 -06:00
parent 39e0f51856
commit c9ed27f6e9
4 changed files with 32 additions and 12 deletions

View File

@@ -9,7 +9,8 @@ import uuid.Uuid;
enum CommandArgType {
SelectedEntry;
SelectedEntries(min:Null<Int>, max:Null<Int>);
Text(minLength:Null<Int>, maxLength:Null<Float>); // max length is a float so Math.POSITIVE_INFINITY can be used
Text(maxLength:Null<Float>); // max length is a float so Math.POSITIVE_INFINITY can be used
VarText(maxLength:Null<Float>);
Number(min:Null<Float>, max:Null<Float>, inStepsOf:Null<Float>);
OneEntry; // This constructor must be disambiguated from the typedef "Entry"
Entries(min:Null<Int>, max:Null<Int>);

View File

@@ -13,17 +13,35 @@
(if !(<= min selectedEntries.length max)
(ui.reportError "The requested command expects between $min and $max entries to be selected. You have selected: $selectedEntries.length")
(continuation selectedEntries)))
((Text minLength maxLength)
(unless minLength (set minLength 0))
((Text maxLength)
(unless maxLength (set maxLength Math.POSITIVE_INFINITY))
(ui.enterText
"${arg.name} (${minLength}-${maxLength} characters):"
"${arg.name} (up to ${maxLength} characters):"
(lambda :Void [text]
(if !(<= minLength text.length maxLength)
(ui.reportError "The requested command expected a string between $minLength and $maxLength characters long. You entered: $text.length characters")
(if !(<= text.length maxLength)
(ui.reportError "The requested command expected a string up to $maxLength characters long. You entered: $text.length characters")
(continuation text)))
minLength
maxLength))
((VarText maxLength)
(unless maxLength (set maxLength Math.POSITIVE_INFINITY))
(let [collectedText
[]
&mut :Void->Void enterTextAgain
null
_enterTextAgain
->:Void
(ui.enterText
"${arg.name} (up to ${maxLength} characters):"
(lambda :Void [text]
(if !text
(continuation collectedText)
(if !(<= text.length maxLength)
(ui.reportError "The requested command expected a list of strings up to $maxLength characters long. You entered: $text.length characters")
{(collectedText.push text)
(enterTextAgain)})))
maxLength)]
(set enterTextAgain _enterTextAgain)
(enterTextAgain)))
((Number min max inStepsOf)
(unless min (set min Math.NEGATIVE_INFINITY))
(unless max (set max Math.POSITIVE_INFINITY))
@@ -100,7 +118,8 @@
(exprCase type
((exprOr SelectedEntry OneEntry) `:nat.Entry ,name)
((exprOr (SelectedEntries _ _) (Entries _ _)) `:Array<nat.Entry> ,name)
((Text _ _) `:String ,name)
((Text _) `:String ,name)
((VarText _) `:Array<String> ,name)
((Number _ _ _) `:Float ,name)))
commandArgs
(for [name type] argPairs

View File

@@ -6,7 +6,7 @@ interface ArchiveUI {
/**
* Prompt the user to enter text
*/
function enterText(prompt:String, resolve:(String) -> Void, minLength:Int, maxLength:Float):Void;
function enterText(prompt:String, resolve:(String) -> Void, maxLength:Float):Void;
/**
* Prompt the user to enter a number

View File

@@ -16,11 +16,11 @@
(defnew [])
(defmethod :Void enterText [prompt resolve minLength maxLength]
(defmethod :Void enterText [prompt resolve maxLength]
(Sys.print "$prompt ")
(loop
(let [entered (.toString (.readLine (Sys.stdin)))]
(if !(<= minLength entered.length maxLength)
(if !(<= entered.length maxLength)
(Sys.print "Try again? ")
{(resolve entered)
(break)}))))
@@ -57,7 +57,7 @@
([] (chooseEntry "name $name doesn't match any entries. Try again?" archive resolve))
// TODO disambiguate entries with the same names by listing stringified versions of them and using enterNumber
(multipleEntries (throw "ambiguous between multiple entries")))))}
0 Math.POSITIVE_INFINITY))
Math.POSITIVE_INFINITY))
(defmethod :Void chooseEntries [prompt archive resolve min max]
(_chooseEntries prompt archive resolve min max []))