Skip to content

Commit 9560420

Browse files
committed
Fill out the README
1 parent 4e2502c commit 9560420

File tree

1 file changed

+163
-4
lines changed

1 file changed

+163
-4
lines changed

README.md

+163-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Prompts
22

3-
Prompts helps you to add beautiful and user-friendly forms to your command-line applications, with browser-like features including placeholder text and validation.
3+
Prompts helps you to add beautiful and user-friendly forms to your command-line applications, with browser-like features including label text, help text, validation, and inline errors.
44

5-
It is heavily inspired by the [Laravel Prompts](https://laravel.com/docs/11.x/prompts) package.
5+
It was originally inspired by the [Laravel Prompts](https://laravel.com/docs/11.x/prompts) package.
66

77
## Installation
88

@@ -18,9 +18,168 @@ If bundler is not being used to manage dependencies, install the gem by executin
1818
gem install prompts
1919
```
2020

21-
## Usage
21+
## Available Prompts
2222

23-
TODO: Write usage instructions here
23+
### Text
24+
25+
A `Text` prompt will prompt the user with the given question, accept their input, and then return it:
26+
27+
```ruby
28+
name = Prompts::TextPrompt.ask(label: "What is your name?")
29+
```
30+
31+
You may also include a default value and an informational hint:
32+
33+
```ruby
34+
name = Prompts::TextPrompt.ask(
35+
label: "What is your name?",
36+
default: "John Doe",
37+
hint: "This will be displayed on your profile."
38+
)
39+
```
40+
41+
#### Required values
42+
43+
If you require a value to be entered, you may pass the `required` argument:
44+
45+
```ruby
46+
name = Prompts::TextPrompt.ask(
47+
label: "What is your name?",
48+
required: true
49+
)
50+
```
51+
52+
If you would like to customize the validation message, you may also pass a string:
53+
54+
```ruby
55+
name = Prompts::TextPrompt.ask(
56+
label: "What is your name?",
57+
required: "Your name is required."
58+
)
59+
```
60+
61+
#### Additional Validation
62+
63+
Finally, if you would like to perform additional validation logic, you may pass a block/proc to the validate argument:
64+
65+
```ruby
66+
name = Prompts::TextPrompt.ask(
67+
label: "What is your name?",
68+
validate: ->(value) do
69+
if value.length < 3
70+
"The name must be at least 3 characters."
71+
elsif value.length > 255
72+
"The name must not exceed 255 characters."
73+
end
74+
end
75+
)
76+
```
77+
78+
The block will receive the value that has been entered and may return an error message, or `nil` if the validation passes.
79+
80+
### Select
81+
82+
If you need the user to select from a predefined set of choices, you may use the `Select` prompt:
83+
84+
```ruby
85+
role = Prompts::SelectPrompt.ask(
86+
label: "What role should the user have?",
87+
options: ["Member", "Contributor", "Owner"]
88+
)
89+
```
90+
91+
You may also include a default value and an informational hint:
92+
93+
```ruby
94+
role = Prompts::SelectPrompt.ask(
95+
label: "What role should the user have?",
96+
options: ["Member", "Contributor", "Owner"],
97+
default: "Owner",
98+
hint: "The role may be changed at any time."
99+
)
100+
```
101+
102+
You may also pass a hash to the `options` argument to have the selected key returned instead of its value:
103+
104+
```ruby
105+
role = Prompts::SelectPrompt.ask(
106+
label: "What role should the user have?",
107+
options: {
108+
member: "Member",
109+
contributor: "Contributor",
110+
owner: "Owner",
111+
},
112+
default: "owner"
113+
)
114+
```
115+
116+
#### Additional Validation
117+
118+
Unlike other prompt classes, the `SelectPrompt` doesn't accept the `required` argument because it is not possible to select nothing. However, you may pass a block/proc to the `validate` argument if you need to present an option but prevent it from being selected:
119+
120+
```ruby
121+
role = Prompts::SelectPrompt.ask(
122+
label: "What role should the user have?",
123+
options: {
124+
member: "Member",
125+
contributor: "Contributor",
126+
owner: "Owner",
127+
},
128+
validate: ->(value) do
129+
if value == "owner" && User.where(role: "owner").exists?
130+
"An owner already exists."
131+
end
132+
end
133+
)
134+
```
135+
136+
If the `options` argument is a hash, then the block will receive the selected key, otherwise it will receive the selected value. The block may return an error message, or `nil` if the validation passes.
137+
138+
### Confirm
139+
140+
If you need to ask the user for a "yes or no" confirmation, you may use the `ConfirmPrompt`. Users may press `y` or `n` (or `Y` or `N`) to select their response. This function will return either `true` or `false`.
141+
142+
```ruby
143+
confirmed = Prompts::ConfirmPrompt.ask(label: "Do you accept the terms?")
144+
```
145+
146+
You may also include a default value and an informational hint:
147+
148+
```ruby
149+
confirmed = Prompts::ConfirmPrompt.ask(
150+
label: "Do you accept the terms?",
151+
default: false,
152+
hint: "The terms must be accepted to continue.",
153+
)
154+
```
155+
156+
#### Requiring "Yes"
157+
158+
If necessary, you may require your users to select "Yes" by passing the `required` argument:
159+
160+
```ruby
161+
confirmed = Prompts::ConfirmPrompt.ask(
162+
label: "Do you accept the terms?",
163+
required: true
164+
)
165+
```
166+
167+
If you would like to customize the validation message, you may also pass a string:
168+
169+
```ruby
170+
confirmed = Prompts::ConfirmPrompt.ask(
171+
label: "Do you accept the terms?",
172+
required: "You must accept the terms to continue."
173+
)
174+
```
175+
176+
### Pause
177+
178+
The `PausePrompt` may be used to display informational text to the user and wait for them to confirm their desire to proceed by pressing the Enter / Return key:
179+
180+
```ruby
181+
Prompts::PausePrompt.ask
182+
```
24183

25184
## Development
26185

0 commit comments

Comments
 (0)