Message types and animations¶
You can view example output of the library using the python scripts in the provided examples directory.
Basic Message Types¶
cliasi provides several methods for common message types, each with its own symbol and color.
Here is how they look in the console:
examples/basic_messages.py¶
from cliasi import cli
cli.info("Starting process...")
cli.success("Process completed!")
cli.warn("Disk space is low.")
cli.fail("Failed to connect to server.")
cli.log("Debug info")
cli.list("List item")
i [CLI] | Starting process...
✔ [CLI] | Process completed!
! [CLI] | Disk space is low.
X [CLI] | Failed to connect to server.
LOG [CLI] | Debug info
- [CLI] | List item
If an exception is raised or a traceback is logged, it will be formatted using the fail message style:
examples/exception_message.py¶
import cliasi
# Importing cliasi automatically installs the logging handler
raise ValueError("An example error")
X [CLI] | Uncaught exception:
X [CLI] | Traceback (most recent call last):
| File "examples/exception_message.py", line 4, in <module>
| raise ValueError("An example error")
| ValueError: An example error
Animations and Progress Bars¶
Blocking Animation¶
Blocking animations run in the main thread and block further execution until complete.
examples/blocking_animation.py¶
from cliasi import cli
import time
cli.animate_message_blocking("Saving.. [CTRL-C] to stop", time=3)
# You cant do anything else while the animation is running
# Useful if you save something to a file at the end of a program
# User can CTRL-C while this is running
cli.success("Data saved!")
Non-Blocking Animation¶
examples/non_blocking_animation.py¶
import time
from cliasi import cli
cli.messages_stay_in_one_line = True # To hide animation after finished.
task = cli.animate_message_non_blocking("Processing...")
# Do other stuff while the animation is running
time.sleep(3) # Simulate a long task
task.stop() # Stop the animation when done
cli.success("Done!")
Progress Bars¶
examples/progress_bar.py¶
import time
from cliasi import cli
for i in range(101):
cli.progressbar("Calculating", progress=i, show_percent=True)
time.sleep(0.02)
cli.newline() # Add a newline after the progress bar is complete
cli.success("Calculation complete.")
# Use cli.progressbar_download() for download-style progress bars.
Animated Progress Bars¶
examples/animated_progress_bar.py¶
import time
from cliasi import cli
task = cli.progressbar_animated_download("Downloading", )
for i in range(100):
time.sleep(0.05) # Simulate work
task.update(progress=i) # Update progress by 1
task.stop() # Finish the progress bar
cli.success("Download complete.")
User Input¶
You can ask for user input, including passwords.
examples/user_input_interactive.py¶
from cliasi import cli
name = cli.ask("What is your name?")
code = cli.ask("Enter your secret code:", hide_input=True)
cli.info(f"Hello, {name} with code {code}")