Skip to content

Commit 3855ccd

Browse files
committed
Remove converter in favor of a complex type
1 parent 2c44369 commit 3855ccd

2 files changed

Lines changed: 30 additions & 25 deletions

File tree

src/spectrecoff-cli/commands/Prompt.fs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@ open SpectreCoff
66
type PromptSettings() =
77
inherit CommandSettings()
88

9-
type Value =
10-
{ num: int
11-
description: string }
12-
139
type PromptExample() =
1410
inherit Command<PromptSettings>()
1511
interface ICommandLimiter<PromptSettings>
1612

1713
override _.Execute(_context, _) =
1814

1915
let fruits = ["Kiwi"; "Pear"; "Grape"; "Plum"; "Banana" ; "Orange"; "Durian"]
20-
let chosenFruit = "If you had to pick one, which would it be?" |> chooseFrom fruits
21-
let chosenFruits = "Which all do you actually like?" |> chooseMultipleFrom fruits
16+
let chosenFruit = "If you had to pick one, which would it be?" |> choose fruits
17+
let chosenFruits = "Which all do you actually like?" |> chooseMultiple fruits
2218

2319
match chosenFruits.Count with
2420
| 0 -> "You don't like any fruit??"
@@ -49,16 +45,13 @@ type PromptExample() =
4945
|> toConsole
5046

5147
// example for chooseFromValues
52-
let getDescription (value: Value) =
53-
value.description
54-
55-
let values = [
56-
{ num = 42; description = "the answer" }
57-
{ num = 13; description = "some say it's unlucky" }
48+
let choices = [
49+
{ Value = 42; Label = "the answer" }
50+
{ Value = 13; Label = "some say it's unlucky" }
5851
]
5952

60-
let chosenValue = chooseFromValues getDescription values "Which number do you like best from its description?"
61-
$"I like {chosenValue.num}, too"
53+
let chosenValue = chooseWithLabel choices "Which number do you like best from its description?"
54+
$"I like {chosenValue}, too"
6255
|> printMarkedUp
6356
0
6457

@@ -99,10 +92,10 @@ type PromptDocumentation() =
9992
emptyRule
10093
C "If the set of choices is finite, one of the following can be used:"
10194
BI [
102-
Many [P "chooseFrom = (choices: string list) (question: string) -> string"; C "a simple single selection prompt"]
103-
Many [P "chooseFromValues<'T> (converter : 'T -> string) (values: 'T list) (question: string) -> string"; C "a single selection prompt that gives a typed response and needs a converter to get a displayable string from each value"]
104-
Many [P "chooseMultipleFrom = (choices: string list) (question: string) -> string list"; C "select multiple values from a list of choices"]
105-
Many [P "chooseMultipleFromWith = (options: MultiSelectionPromptOptions) (choices: string list) (question: string) -> string list"; C " selecting multiple entries with configuration of the number of visible entries per page (default: 10)"]
95+
Many [P "choose = (choices: string list) (question: string) -> string"; C "a simple single selection prompt"]
96+
Many [P "chooseWithLabel<'T> (choices: ChoiceWithLabel<'T> list) (question: string) -> string"; C "a single selection prompt that gives a typed response"]
97+
Many [P "chooseMultiple = (choices: string list) (question: string) -> string list"; C "select multiple values from a list of choices"]
98+
Many [P "chooseMultipleWith = (options: MultiSelectionPromptOptions) (choices: string list) (question: string) -> string list"; C " selecting multiple entries with configuration of the number of visible entries per page (default: 10)"]
10699
]
107100
BL
108101
emptyRule

src/spectrecoff/Prompt.fs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type PromptOptions =
1010
type MultiSelectionPromptOptions =
1111
{ PageSize: int }
1212

13+
type ChoiceWithLabel<'T when 'T: equality> =
14+
{ Value: 'T
15+
Label: string }
16+
1317
let defaultOptions =
1418
{ Secret = false
1519
Optional = false }
@@ -52,17 +56,25 @@ module private Prompts =
5256
let private prompt prompter =
5357
AnsiConsole.Prompt prompter;
5458

55-
let chooseFrom (choices: string list) question =
59+
let choose (choices: string list) question =
5660
prompt (Prompts.selectionPrompt question choices)
5761

58-
let chooseFromValues<'T> (converter : 'T -> string) (values: 'T list) question =
59-
prompt (Prompts.selectionPromptT converter question values)
60-
61-
let chooseMultipleFromWith options (choices: string list) question =
62+
let chooseWithLabel<'T when 'T: equality> (choices: ChoiceWithLabel<'T> list ) question =
63+
let choiceLabelMap =
64+
choices
65+
|> Seq.map (fun choice -> choice.Value, choice.Label)
66+
|> dict
67+
let converter = (fun choice -> choiceLabelMap[choice])
68+
69+
choiceLabelMap.Keys
70+
|> Prompts.selectionPromptT converter question
71+
|> prompt
72+
73+
let chooseMultipleWith options (choices: string list) question =
6274
prompt (Prompts.multiSelectionPrompt question choices options)
6375

64-
let chooseMultipleFrom =
65-
chooseMultipleFromWith defaultMultiSelectionOptions
76+
let chooseMultiple =
77+
chooseMultipleWith defaultMultiSelectionOptions
6678

6779
let ask<'T> question =
6880
prompt (Prompts.textPrompt<'T> question defaultOptions)

0 commit comments

Comments
 (0)