-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
923 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,90 @@ | ||
namespace FsSpectre | ||
|
||
open System | ||
open System.Collections.Generic | ||
open Spectre.Console | ||
|
||
[<AutoOpen>] | ||
module MultiSelectionPromptBuilder = | ||
|
||
type MulitSelectionPromptConfig<'T> = | ||
{ Title: string | ||
Comparer: IEqualityComparer<'T> | ||
Choices: ('T option * 'T array) array | ||
PageSize: int | ||
Required: bool | ||
MoreChoicesText: string | ||
InstructionsText: string | ||
Converter: Option<'T -> string> } | ||
|
||
static member Default = | ||
{ Title = String.Empty | ||
Comparer = EqualityComparer<'T>.Default | ||
Choices = Array.empty<('T option * 'T array)> | ||
PageSize = 10 | ||
Required = true | ||
MoreChoicesText = "[grey](Move up and down to reveal more choices)[/]" | ||
InstructionsText = "[grey](Press <space> to select, <enter> to accept)[/]" | ||
Converter = None } | ||
|
||
|
||
type MultiSelectionPromptBuilder<'T>() = | ||
member __.Yield _ = MultiSelectionPrompt<'T>() | ||
member __.Yield _ = MulitSelectionPromptConfig<'T>.Default | ||
|
||
member __.Run(config: MulitSelectionPromptConfig<'T>) = | ||
let result = MultiSelectionPrompt<'T>(config.Comparer) | ||
result.Title <- config.Title | ||
|
||
config.Choices | ||
|> Array.iter (fun (grpOpt, choices) -> | ||
match grpOpt with | ||
| Some grp -> result.AddChoiceGroup(grp, choices) |> ignore | ||
| None -> result.AddChoices(choices) |> ignore) | ||
|
||
result.PageSize <- config.PageSize | ||
result.Required <- config.Required | ||
result.MoreChoicesText <- config.MoreChoicesText | ||
result.InstructionsText <- config.InstructionsText | ||
config.Converter |> Option.iter (fun c -> result.Converter <- c) | ||
result | ||
|
||
[<CustomOperation "title">] | ||
member __.Title(multiSelectionPrompt: MultiSelectionPrompt<'T>, title: string) = | ||
multiSelectionPrompt.Title <- title | ||
multiSelectionPrompt | ||
member __.Title(config: MulitSelectionPromptConfig<'T>, title: string) = { config with Title = title } | ||
|
||
[<CustomOperation "comparer">] | ||
member __.Title(config: MulitSelectionPromptConfig<'T>, comparer: IEqualityComparer<'T>) = | ||
{ config with Comparer = comparer } | ||
|
||
[<CustomOperation "choices">] | ||
member __.Choices(multiSelectionPrompt: MultiSelectionPrompt<'T>, choices: 'T array) = | ||
multiSelectionPrompt.AddChoices(choices) | ||
member __.Choices(config: MulitSelectionPromptConfig<'T>, choices: 'T array) = | ||
{ config with | ||
Choices = Array.append config.Choices [| (None, choices) |] } | ||
|
||
[<CustomOperation "choice_group">] | ||
member __.ChoiceGroup(multiSelectionPrompt: MultiSelectionPrompt<'T>, choiceGroup: 'T, choices: 'T array) = | ||
multiSelectionPrompt.AddChoiceGroup(choiceGroup, choices) | ||
member __.ChoiceGroup(config: MulitSelectionPromptConfig<'T>, choiceGroup: 'T, choices: 'T array) = | ||
{ config with | ||
Choices = Array.append config.Choices [| (Some choiceGroup, choices) |] } | ||
|
||
[<CustomOperation "not_required">] | ||
member __.PageSize(multiSelectionPrompt: MultiSelectionPrompt<'T>) = multiSelectionPrompt.NotRequired() | ||
member __.PageSize(config: MulitSelectionPromptConfig<'T>) = { config with Required = false } | ||
|
||
[<CustomOperation "page_size">] | ||
member __.PageSize(multiSelectionPrompt: MultiSelectionPrompt<'T>, pageSize: int) = | ||
multiSelectionPrompt.PageSize <- pageSize | ||
multiSelectionPrompt | ||
member __.PageSize(config: MulitSelectionPromptConfig<'T>, pageSize: int) = { config with PageSize = pageSize } | ||
|
||
[<CustomOperation "more_choices_text">] | ||
member __.MoreChoicesText(multiSelectionPrompt: MultiSelectionPrompt<'T>, moreChoicesText: string) = | ||
multiSelectionPrompt.MoreChoicesText <- moreChoicesText | ||
multiSelectionPrompt | ||
member __.MoreChoicesText(config: MulitSelectionPromptConfig<'T>, moreChoicesText: string) = | ||
{ config with | ||
MoreChoicesText = moreChoicesText } | ||
|
||
[<CustomOperation "instructions_text">] | ||
member __.InstructionsText(multiSelectionPrompt: MultiSelectionPrompt<'T>, instructionsText: string) = | ||
multiSelectionPrompt.InstructionsText <- instructionsText | ||
multiSelectionPrompt | ||
member __.InstructionsText(config: MulitSelectionPromptConfig<'T>, instructionsText: string) = | ||
{ config with | ||
InstructionsText = instructionsText } | ||
|
||
[<CustomOperation "converter">] | ||
member __.Converter(multiSelectionPrompt: MultiSelectionPrompt<'T>, converter: 'T -> string) = | ||
multiSelectionPrompt.Converter <- converter | ||
multiSelectionPrompt | ||
member __.Converter(config: MulitSelectionPromptConfig<'T>, converter: 'T -> string) = | ||
{ config with | ||
Converter = Some converter } | ||
|
||
|
||
let multiSelectionPrompt<'T> = MultiSelectionPromptBuilder<'T>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
namespace FsSpectre | ||
|
||
open System | ||
open Spectre.Console | ||
|
||
[<AutoOpen>] | ||
module SelectionPromptBuilder = | ||
|
||
type SelectionPromptConfig<'T> = | ||
{ Title: string | ||
Choices: 'T array | ||
PageSize: int | ||
MoreChoicesText: string | ||
Converter: Option<'T -> string> } | ||
|
||
static member Default = | ||
{ Title = String.Empty | ||
Choices = Array.empty<'T> | ||
PageSize = 10 | ||
MoreChoicesText = "[grey](Move up and down to reveal more choices)[/]" | ||
Converter = None } | ||
|
||
type SelectionPromptBuilder<'T>() = | ||
member __.Yield _ = SelectionPrompt<'T>() | ||
member __.Yield _ = SelectionPromptConfig<'T>.Default | ||
|
||
member __.Run(config: SelectionPromptConfig<'T>) = | ||
let result = SelectionPrompt<'T>() | ||
result.Title <- config.Title | ||
result.AddChoices(config.Choices) |> ignore | ||
result.PageSize <- config.PageSize | ||
result.MoreChoicesText <- config.MoreChoicesText | ||
config.Converter |> Option.iter (fun c -> result.Converter <- c) | ||
result | ||
|
||
[<CustomOperation "title">] | ||
member __.Title(selectionPrompt: SelectionPrompt<'T>, title: string) = | ||
selectionPrompt.Title <- title | ||
selectionPrompt | ||
member __.Title(config: SelectionPromptConfig<'T>, title: string) = { config with Title = title } | ||
|
||
[<CustomOperation "choices">] | ||
member __.Choices(selectionPrompt: SelectionPrompt<'T>, choices: 'T array) = selectionPrompt.AddChoices(choices) | ||
member __.Choices(config: SelectionPromptConfig<'T>, choices: 'T array) = | ||
{ config with | ||
Choices = Array.append config.Choices choices } | ||
|
||
[<CustomOperation "page_size">] | ||
member __.PageSize(selectionPrompt: SelectionPrompt<'T>, pageSize: int) = | ||
selectionPrompt.PageSize <- pageSize | ||
selectionPrompt | ||
member __.PageSize(config: SelectionPromptConfig<'T>, pageSize: int) = { config with PageSize = pageSize } | ||
|
||
[<CustomOperation "more_choices_text">] | ||
member __.MoreChoicesText(selectionPrompt: SelectionPrompt<'T>, moreChoicesText: string) = | ||
selectionPrompt.MoreChoicesText <- moreChoicesText | ||
selectionPrompt | ||
member __.MoreChoicesText(config: SelectionPromptConfig<'T>, moreChoicesText: string) = | ||
{ config with | ||
MoreChoicesText = moreChoicesText } | ||
|
||
[<CustomOperation "converter">] | ||
member __.Converter(selectionPrompt: SelectionPrompt<'T>, converter: 'T -> string) = | ||
selectionPrompt.Converter <- converter | ||
selectionPrompt | ||
member __.Converter(config: SelectionPromptConfig<'T>, converter: 'T -> string) = | ||
{ config with | ||
Converter = Some converter } | ||
|
||
let selectionPrompt<'T> = SelectionPromptBuilder<'T>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.