👷 To fully customize your prompts, you'll have to take total control of formatting and colors. Here's what you need to know.
- General
bullet
Objects- More Customization: Extending Existing Prompts
Always create an UI object with a prompt specified.
from bullet import Bullet, Check, YesNo, Input # and etc...
cli = Bullet(prompt = "Choose from the items below: ") # Create a Bullet or Check object
result = cli.launch() # Launch a prompt
cli = Bullet(choices = ["first item", "second item", "third item"])
cli = Bullet(bullet = "★")
cli = Check(check = "√")
cli = Password(hidden = "*")
cli = ScrollBar(pointer = "→")
You can also use emojis!
It is recommended to EXPLICITLY specify ALL colors for an UI object.
from bullet import colors
🎨 The following colors (both background and foreground) are supported in
bullets
. Note thatdefault
is the color of your default terminal.
default, black, red, green, yellow, blue, magenta, cyan, white
🎨 Remember to specify
foreground
andbackground
.
black_foreground = colors.foreground["black"]
white_background = colors.background["white"]
🎨 You can wrap a color with the
bright
function
bright_cyan = colors.bright(colors.foreground["cyan"])
🎨 Define the following colors when initializing the UI components.
- Use foreground colors:
bullet_color
check_color
pointer_color
indicator_color
check_on_switch
word_color
word_on_switch
separator_color
- Use background colors:
background_color
background_on_switch
📐 Define the following UI components (not all is needed for some objects).
indent
: distance from left-boundary to start of prompt.pad_right
: extended background length.align
: distance between bullet (or check) and start of prompt.margin
: distance between list item and bullets (or checks).shift
: number of new lines between prompt and first item.
👷 Currently only styles for
Bullet
is supported.
from bullet import styles
client = Bullet(**styles.Greece)
Single-choice prompt.
- Define
bullet
when initializingBullet
object. - Move current position up and down using arrow keys.
- Returns the chosen item after pressing enter.
Multiple-choice prompt.
- Define
check
when initializingCheck
object. - Move current position up and down using arrow keys.
- Check/Un-check an item by pressing space.
- Returns the a list of chosen items after pressing enter.
Just vanilla user input.
strip: bool
: whether to strip trailing spaces.pattern: str
: Default is""
. If defined, user input should match pattern.
Guarded Yes/No question.
- Only enter
y/Y
orn/N
. Other invalid inputs will be guarded, and the user will be asked to re-enter.
Enter passwords.
- Define
hidden
when initializingPassword
object. This would be the character shown on the terminal when passwords are entered. - In convention, space characters
' '
are guarded and should not be in a password.
Enter numeric values.
- Non-numeric values will be guarded, and the user will be asked to re-enter.
- Define
type
to cast return value. For example,type = float
, will cast return value tofloat
.
Wrapping it all up.
- Stack
bullet
UI components into one vertically-rendered prompt. - Returns a list of tuples
(prompt, result)
. spacing
: number of lines between adjacent UI components.- Or, if
separator
is defined, each UI will be separated by a sequence ofseparator
characters. - See
./examples/prompt.py
to get a better understanding.
cli = VerticalPrompt(
[
YesNo("Are you a student? "),
Input("Who are you? "),
Numbers("How old are you? "),
Bullet("What is your favorite programming language? ",
choices = ["C++", "Python", "Javascript", "Not here!"]),
],
spacing = 1
)
result = cli.launch()
- Link
bullet
UI components into a multi-stage prompt. Previous prompts will be cleared upon entering the next stage. - Returns a list of tuples
(prompt, result)
.
For
Prompt
ojects, callsummarize()
after launching the prompt to print out user input.
Enhanced
Bullet
: Too many items? It's OK!
pointer
: points to item currently selected.up_indicator
,down_indicator
: indicators shown in first and last row of the rendered items.height
: maximum items rendered on terminal.- For example, your can have 100 choices (
len(choices) = 100
) but defineheight = 5
.
- For example, your can have 100 choices (
See
./examples/check.py
for the big picture of what's going on.
In bullet
, you can easily inherit a base class (existing bullet
objects) and create your customized prompt. This is done by introducing the keyhandler
module to register user-defined keyboard events.
from bullet import keyhandler
Say you want the user to choose at least 1 and at most 3 items from a list of 5 items. You can inherit the Check
class, and register a customized keyboard event as a method.
@keyhandler.register(NEWLINE_KEY)
def accept(self):
# do some validation checks: chosen items >= 1 and <= 3.
Note that accept()
is the method for all prompts to return user input. The binded keyboard event by default is NEWLINE_KEY
pressed.
See
./bullet/charDef.py
LINE_BEGIN_KEY
: Ctrl + HLINE_END_KEY
: Ctrl + ETAB_KEY
NEWLINE_KEY
: EnterESC_KEY
BACK_SPACE_KEY
ARROW_UP_KEY
ARROW_DOWN_KEY
ARROW_RIGHT_KEY
ARROW_LEFT_KEY
INSERT_KEY
DELETE_KEY
END_KEY
PG_UP_KEY
PG_DOWN_KEY
SPACE_CHAR
INTERRUPT_KEY
: Ctrl + C