Skip to content

Commit

Permalink
Add Default for the TextPromptBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
galassie committed Feb 17, 2023
1 parent b85b41d commit af0e8ac
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/FsSpectre/Prompts/TextPromptBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ module TextPromptBuilder =
Validator: Option<'T -> ValidationResult>
Converter: Option<'T -> string>
ShowChoices: bool
ChoicesStyle: Style option
Choices: 'T array
ChoicesStyle: Style option }
ShowDefaultValue: bool
DefaultValueStyle: Style option
DefaultValue: 'T option }

static member Default =
{ Text = String.Empty
Expand All @@ -33,8 +36,11 @@ module TextPromptBuilder =
Validator = None
Converter = None
ShowChoices = false
ChoicesStyle = None
Choices = Array.empty<'T>
ChoicesStyle = None }
ShowDefaultValue = true
DefaultValueStyle = None
DefaultValue = None }

type TextPromptBuilder<'T>() =
member __.Yield _ = TextPromptConfig<'T>.Default
Expand All @@ -57,6 +63,10 @@ module TextPromptBuilder =
result.AddChoices(config.Choices) |> ignore
config.ChoicesStyle |> Option.iter (fun cs -> result.ChoicesStyle <- cs)

result.ShowDefaultValue <- config.ShowDefaultValue
config.DefaultValueStyle |> Option.iter (fun vs -> result.DefaultValueStyle <- vs)
config.DefaultValue |> Option.iter (fun dv -> result.DefaultValue(dv) |> ignore)

result

[<CustomOperation "text">]
Expand Down Expand Up @@ -109,14 +119,29 @@ module TextPromptBuilder =
{ config with
ShowChoices = true }

[<CustomOperation "choices_style">]
member __.ChoicesStyle(config: TextPromptConfig<'T>, style: Style) =
{ config with
ChoicesStyle = Some style }

[<CustomOperation "choices">]
member __.Choices(config: TextPromptConfig<'T>, choices: 'T array) =
{ config with
Choices = Array.append config.Choices choices }

[<CustomOperation "choices_style">]
member __.ChoicesStyle(config: TextPromptConfig<'T>, style: Style) =
[<CustomOperation "hide_default_value">]
member __.HideDefaultValue(config: TextPromptConfig<'T>) =
{ config with
ShowDefaultValue = false }

[<CustomOperation "default_value_style">]
member __.DefaultValueStyle(config: TextPromptConfig<'T>, style: Style) =
{ config with
ChoicesStyle = Some style }

[<CustomOperation "default_value">]
member __.DefaultValue(config: TextPromptConfig<'T>, value: 'T) =
{ config with
DefaultValue = Some value }

let textPrompt<'T> = TextPromptBuilder<'T>()

0 comments on commit af0e8ac

Please sign in to comment.