๐Ÿ“ฆ sleepyfran / duets

๐Ÿ“„ Prompt.fs ยท 166 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166[<AutoOpen>]
module Duets.Cli.Components.Prompt

open Duets.Cli.Text
open Duets.Common
open Duets.Entities
open Spectre.Console

/// <summary>
/// Renders a basic confirmation prompt that accepts yes/no answers.
/// </summary>
/// <param name="title">Title of the prompt to show when asking</param>
/// <returns>
/// <i>true</i> if the user answered yes or <i>false</i> if no
/// </returns>
let showConfirmationPrompt title = AnsiConsole.Confirm(title)

/// <summary>
/// Renders a basic text prompt, forcing the user to write at least one character.
/// </summary>
/// <param name="title">Title of the prompt to show when asking</param>
/// <returns>The text given by the user</returns>
let showTextPrompt title = AnsiConsole.Ask<string>(title)

/// <summary>
/// Renders a basic text prompt, allowing the user to write nothing.
/// </summary>
/// <param name="title">Title of the prompt to show when asking</param>
/// <returns>The text given by the user or None if empty.</returns>
let showOptionalTextPrompt title =
    let mutable txtPrompt = TextPrompt<string> title
    txtPrompt.AllowEmpty <- true
    let result = AnsiConsole.Prompt txtPrompt

    match result with
    | "" -> None
    | _ -> Some result

/// <summary>
/// Renders a basic integer prompt, forcing the user to give a valid number.
/// </summary>
/// <param name="title">Title of the prompt to show when asking</param>
/// <returns>The integer given by the user</returns>
let showNumberPrompt title = AnsiConsole.Ask<int>(title)

/// <summary>
/// Renders a basic integer prompt that forces the user to give a valid number
/// between the given inclusive range.
/// </summary>
/// <param name="min">Minimum number allowed</param>
/// <param name="max">Maximum number allowed</param>
/// <param name="title">Title of the prompt to show when asking</param>
let showRangedNumberPrompt (min: int<'a>) (max: int<'a>) title =
    let title = $"""{title} {Styles.faded $"(Between {min} and {max})"}"""

    TextPrompt<int<'a>>(
        title,
        Validator =
            (fun number ->
                match number with
                | n when n >= min && n <= max -> ValidationResult.Success()
                | n ->
                    ValidationResult.Error(
                        $"{n} is not between {min} and {max}. Choose another number"
                        |> Styles.error
                    ))
    )
    |> AnsiConsole.Prompt

/// <summary>
/// Renders a decimal prompt that forces the user to give a valid decimal
/// number between the given inclusive range.
/// </summary>
/// <param name="min">Minimum number allowed</param>
/// <param name="max">Maximum number allowed</param>
/// <param name="title">Title of the prompt to show when asking</param>
let showRangedDecimalPrompt min max title =
    let title = $"""{title} {Styles.faded $"(Between {min} and {max})"}"""

    TextPrompt<decimal>(
        title,
        Validator =
            (fun number ->
                match number with
                | n when n >= min && n <= max -> ValidationResult.Success()
                | n ->
                    ValidationResult.Error(
                        $"{n} is not between {min} and {max}. Choose another number"
                        |> Styles.error
                    ))
    )
    |> AnsiConsole.Prompt

/// <summary>
/// Renders a basic decimal prompt, forcing the user to give a valid number.
/// </summary>
/// <param name="title">Title of the prompt to show when asking</param>
/// <returns>The decimal given by the user</returns>
let showDecimalPrompt title = AnsiConsole.Ask<decimal>(title)

type private InteractiveDatePromptOption =
    | Date of Date
    | NextSeason

/// <summary>
/// Renders a choice prompt with all the dates of the season after the given
/// first date. Returns some date if character selected something, none if the
/// prompt was cancelled.
/// </summary>
let rec showInteractiveDatePrompt title firstDate =
    let seasonDays = Calendar.Query.seasonDaysFrom firstDate |> Seq.map Date
    let nextSeasonDate = Calendar.Query.firstDayOfNextSeason firstDate

    let toText opt =
        match opt with
        | Date date -> Generic.dateWithDay date
        | NextSeason -> Generic.moreDates

    let selectedDate =
        showOptionalChoicePrompt
            title
            Generic.cancel
            toText
            (seq {
                yield! seasonDays
                yield NextSeason
            })

    match selectedDate with
    | Some(Date date) -> Some date
    | Some NextSeason -> showInteractiveDatePrompt title nextSeasonDate
    | None -> None

/// <summary>
/// Renders a prompt that accepts lengths in the format minutes:seconds.
/// </summary>
/// <param name="title">Title of the prompt to show when asking</param>
/// <returns>The length given by the user</returns>
let showLengthPrompt title =
    let mutable lengthPrompt = TextPrompt<string>(title)

    let validate (length: string) =
        match Time.Length.parse length with
        | Ok _ -> ValidationResult.Success()
        | Error _ -> ValidationResult.Error(Generic.invalidLength)

    lengthPrompt.Validator <- Func.toFunc validate

    AnsiConsole.Prompt(lengthPrompt)
    |> fun length ->
        match Time.Length.parse length with
        | Ok length -> length
        | _ ->
            raise (
                invalidOp
                    "The given input was not a correct length. This should've been caught by the validator but apparently it didn't :)"
            )

/// <summary>
/// Renders a prompt that blocks the user until they press any key.
/// </summary>
let showContinuationPrompt () =
    "Press any key to continue..." |> showMessage

    AnsiConsole.Console.Input.ReadKey(true) |> ignore