Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/giann/sirocco

šŸ¦œ A collection of interactive command line prompts for Lua
https://github.com/giann/sirocco

cli command-line lua prompt terminal

Last synced: about 2 months ago
JSON representation

šŸ¦œ A collection of interactive command line prompts for Lua

Awesome Lists containing this project

README

        


sirocco

# Sirocco
A collection of interactive command line prompts for Lua


sirocco

**Note:** Sirocco is in active development.

## Installing

Requirements:
- Lua 5.1/JIT/5.2/5.3
- luarocks >= 3.0 (_Note: `hererocks -rlatest` will install 2.4, you need to specify it with `-r3.0`_)

```bash
luarocks install sirocco
```

## Quickstart

See [`example.lua`](https://github.com/giann/sirocco/blob/master/example.lua) for an exhaustive snippet of all sirocco's features.

### Text prompt


Prompt

Basic text prompt. Every prompt inherits from it so most of its options apply to them.

```lua
Prompt {
-- The prompt
prompt = "A simple question\nā± ",
-- A placeholder that will dissappear once the user types something
placeholder = "A simple answer",
-- Whether the answer is required or not
required = true,
-- The default answer
default = "A default answer",
-- When hitting `tab`, will try to autocomplete based on those values
possibleValues = {
"some",
"possible",
"values",
},
-- Must return whether the current text is valid + a message in case it's not
validator = function(text)
return text:match("[a-zA-Z]*"), "Message when not valid"
end,
-- If returns false, input will not appear at all
filter = function(input)
return input:match("[a-zA-Z]*")
end
}:ask() -- Returns the answer
```

### Password


Password

Obfuscates the input.

```lua
Password {
prompt = "Enter your secret (hidden answer)\nā± ",
-- When false *** are printed otherwise nothing
hidden = false
}:ask() -- Returns the actual answer
```

### Confirm


Confirm

A simple yes/no prompt.

```lua
Confirm {
prompt = "All finished?"
}:ask() -- Returns the answer
```

### List


Single Choice List


Multiple Choices List

Will choose the appropriate list (check list or radio list).

```lua
List {
prompt = "Where are you from?",
-- If true can select multiple choices (check list) otherwise one (radio list)
multiple = false,
-- List of choices
items = {
{
-- The actual value returned if selected
value = "New York",
-- The value displayed to the user
label = "New York"
},
{
value = "Paris",
label = "Paris"
},
{
value = "Rome",
label = "Rome"
}
},
-- Indexes of already selected choices
default = { 2, 4 },
}:ask() -- Returns a table of the selected choices
```

### Composite


Composite

Will jump from field to field when appropriate.

**TODO:** field's length should be optional

```lua
Composite {
prompt = "What's your birthday? ",
-- Separator between each fields
separator = " / ",
-- Fields definition
fields = {
{
placeholder = "YYYY",
filter = function(input)
return input:match("%d")
and input
or ""
end,
-- Required
length = 4,
},
{
placeholder = "mm",
filter = function(input)
return input:match("%d")
and input
or ""
end,
length = 2,
},
{
placeholder = "dd",
filter = function(input)
return input:match("%d")
and input
or ""
end,
length = 2,
},
}
}:ask() -- Returns a table of each field's answer
```