-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectreFSharp.fs
151 lines (107 loc) · 4.03 KB
/
SpectreFSharp.fs
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
module Spectre.Console.FSharp
open Spectre.Console
open Spectre.Console.Json
open Spectre.Console.Rendering
type Alignment =
| Left
| Center
| Right
type FigletConfig =
{ Aligned: Alignment
Color: Spectre.Console.Color
Text: string }
type FigletBuilder() =
member _.Yield _ =
{ Text = ""
Color = Spectre.Console.Color.Red
Aligned = Left }
member _.Run(config) =
let text = FigletText(config.Text)
match config.Aligned with
| Left -> text.Justification <- Justify.Left
| Center -> text.Justification <- Justify.Center
| Right -> text.Justification <- Justify.Right
text.Color <- config.Color
text |> AnsiConsole.Write
()
[<CustomOperation "text">]
member _.Text(state, text) = { state with Text = text }
[<CustomOperation "color">]
member _.Color(state, color) = { state with Color = color }
[<CustomOperation "aligned">]
member _.Aligned(state, alignment) = { state with Aligned = alignment }
type Choice = Choice of string
type SelectionConfig =
{ Title: string
PageSize: int
MoreChoicesText: string
Choices: Choice list }
type SelectionBuilder() =
member _.Yield _ =
{ Title = ""
PageSize = 5
MoreChoicesText = "More"
Choices = list.Empty }
member _.Run(config) =
let prompt = SelectionPrompt<string>()
prompt.Title <- config.Title
prompt.PageSize <- config.PageSize
prompt.MoreChoicesText <- config.MoreChoicesText
prompt.AddChoices(config.Choices |> Seq.map (fun (Choice choice) -> choice))
|> ignore
prompt |> AnsiConsole.Prompt |> Choice
[<CustomOperation("title")>]
member _.Title(state, title) = { state with Title = title }
[<CustomOperation("moreChoicesText")>]
member _.MoreChoices(state, moreChoicesText) =
{ state with
MoreChoicesText = moreChoicesText }
[<CustomOperation("pageSize")>]
member _.PageSize(state, pageSize) = { state with PageSize = pageSize }
[<CustomOperation("choices")>]
member _.Choices(state, choices) = { state with Choices = choices }
type TextPromptConfig =
{ Question: string
Validation: (string -> ValidationResult) option }
type TextPromptBuilder() =
member _.Yield _ = { Question = ""; Validation = None }
member _.Run(config) =
let prompt = TextPrompt<string>(config.Question)
match config.Validation with
| None -> ()
| Some validation -> prompt.Validator <- validation
prompt |> AnsiConsole.Prompt<string>
[<CustomOperation("question")>]
member _.Question(state, question) = { state with Question = question }
[<CustomOperation("validation")>]
member _.Validation(state, validation) = { state with Validation = validation }
type ErrorConfig = { Description: string }
type ErrorBuilder() =
member _.Yield _ = { Description = "" }
member _.Run(config) =
AnsiConsole.Write(Markup($"[red]{config.Description}[/]") :> Renderable)
AnsiConsole.WriteLine()
()
[<CustomOperation("description")>]
member _.Description(state, error) = { state with Description = error }
type JsonViewConfig = { Json: string; Header: string }
type JsonViewBuilder() =
member _.Yield _ = { Json = ""; Header = "" }
member _.Run(config: JsonViewConfig) =
let jsonText = JsonText(config.Json)
let panel = Panel(jsonText)
panel.Header <- PanelHeader(config.Header)
panel.RoundedBorder().Collapse().BorderColor(Color.Yellow) |> AnsiConsole.Write
()
[<CustomOperation("json")>]
member _.Json(state, json) = { state with Json = json }
[<CustomOperation("header")>]
member _.Header(state, header) = { state with Header = header }
type AnsiConsole() =
member _.Return(x) = x
let error = ErrorBuilder()
let jsonView = JsonViewBuilder()
let textPrompt = TextPromptBuilder()
let selection = SelectionBuilder()
let figlet = FigletBuilder()
let console = AnsiConsole()