-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* prompt init * small fixes to repr and error * fixed small repr bugs * removed deps * wip errors * bump * bump * bump * bump * prompt wip * bump * prompt almost finished * added tests and docs for prompt * bump * coverage and CI
- Loading branch information
1 parent
cf92b7c
commit 143c9ba
Showing
42 changed files
with
521 additions
and
45 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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Prompts | ||
```@meta | ||
CurrentModule = Term.Prompts | ||
``` | ||
|
||
|
||
```@index | ||
Pages = ["api_prompt.md"] | ||
``` | ||
|
||
|
||
```@autodocs | ||
Modules = [Prompts] | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Prompt | ||
|
||
Time for a little example of a simple thing you can use `Term.jl` for: asking for some input. Use a `Prompt`, ask a question, get an answer. Simple, but a little extra style. That's what `AbstractPrompt` types are for. There's a few different flavors, which we'll look at in due time, but essentially a prompt is made of a bit of text, the `prompt` that is displayed to the user, and some machinery to capture the user's input and parse it/ validate it. | ||
|
||
For example: | ||
|
||
```@example prompt | ||
using Term.Prompts | ||
Prompt("Simple, right?") |> ask | ||
``` | ||
|
||
here we construct a basic `Prompt` and "ask" it: print the message and capture the reply. `ask` returns the answer, which you can do with as you please. A small warning before we carry on: | ||
|
||
!!! warning "Using VSCode" | ||
As you can see [here](https://discourse.julialang.org/t/vscode-errors-with-user-input-readline/75097/4?u=fedeclaudi), `readlines`, which `AbstractPrompts` use to get the user's input, is a bit troublesome in VSCode. In VSCode, after the prompt gets printed you need to enter "space" (hit the space bar) and then enter and **after** that you can enter your actual replies. | ||
|
||
## Prompt flavours | ||
There's a couple more ways you can use prompts like. One is to ensure you get an answer of the correct `Type`, using the immaginatively names `TypePrompt`: | ||
|
||
```@example prompt | ||
# this only accepts `Int`s | ||
TypePrompt(Int, "give me a number") |> ask | ||
``` | ||
|
||
If your answer can't be converted to the correct type you'll get a `AnswerValidationError`, not good. | ||
|
||
|
||
So, what if you want to get user inputs, but you don't want to handle any crazy input they can provide? Fear not, use `OptionsPrompt` so that only acceptable options will be ok. This will keep "asking" your prompt until the user's answer matches one of the given options | ||
|
||
```@example prompt | ||
OptionsPrompt(["a lot", "so much", "the most"], "How likely would you be to recomend Term.jl to a friend or colleague?") |> ask | ||
``` | ||
|
||
Okay, so much typing though. Let's be realistic, most likely you just want to ask a yes/no question and the answer is likely just yes. So just use a `DefaultPrompt`: | ||
|
||
```@example prompt | ||
# one says the first option is the default | ||
DefaultPropt(["yes", "no"], 1, "Confirm?") |> ask | ||
``` | ||
|
||
still too much typing? Ask the user to `confirm`: | ||
|
||
```@example prompt | ||
confirm() | ||
``` | ||
|
||
## Style | ||
The style of prompt elements (e.g. the color of the prompt's text or of the options) is defined in `Theme`. You can also pass style information during prompt creation: | ||
|
||
```@example prompt | ||
Promt("Do you like this color?", "red") |> println | ||
DefaultPropt(["yes", "no"], 1, "Confirm?", "green", "blue", "red") |
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
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.