Console tools for inputs and outputs in Python
Project description
PyConsoleIOTools
Installation
pip install consoleiotools # install
pip install --upgrade consoleiotools # update
python -m consoleiotools # examples
Get Started
import consoleiotools as cit
print(cit.__version__)
Prints on Screen
>>> cit.start()
# blank line
>>> cit.title("Session Name")
+--------------+
| SESSION NAME |
+--------------+
>>> cit.echo("Hello World")
| Hello World
>>> cit.echo("Hello World", pre="say", bar="!")
! (Say) Hello World
>>> cit.echo("Hello World", indent=1) # indent level, default is 0.
| |-- Hello World
>>> cit.echo("Hello World", indent=-1) # level < 0 means the last line of this indent.
| `-- Hello World
>>> cit.echo("Hello World", indent=3) # 3 level of indent.
| | | |-- Hello World
>>> cit.ask("Hello World")
| (?) Hello World
>>> cit.info("Hello World")
| (Info) Hello World
>>> cit.warn("Hello World")
| (Warning) Hello World
>>> cit.err("Hello World")
| (Error) Hello World
>>> cit.mute("Hello World")
| Hello World # muted by dim
>>> cit.print("[yellow]Hello World[/]") # print with styles
Hello World # yellow
>>> cit.print(cit.escape("[yellow]Hello World[/]")) # escape `[` -> `\[` if not escaped. escape `\` -> `\\` if not used as escape char.
[yellow]Hello World[/]
>>> cit.markdown("# Header") # print markdown
+--------------+
| Header |
+--------------+
>>> cit.rule() # print horizontal rule
----------------------------------------
>>> cit.rule("Title", style="blue", align="center") # print horizontal rule with Title. align = center|left|right
---------------- Title ----------------
>>> cit.panel("Panel", title="Panel Title", subtitle="Panel Subtitle") # print text in a panel
+---------- Panel Title ----------+
| Panel | # full width
+-------- Panel Subtitle ---------+
>>> cit.panel("Panel", title="Panel Title", subtitle="Panel Subtitle", expand=False, style="blue") # fit panel to text
+- Panel Title -+ # blue
| Panel |
+- Panel Subtit-+
>>> cit.end()
`
>>> cit.br()
# blank line
>>> cit.br(2)
# blank line
# blank line
>>> for var in cit.track(iterables, "Progress"): pass
| : Progress ---------------------=================== 52% 0:00:52 - 52/100
>>> cit.__ascii__ = True # use ascii chars only.
Get User Input
>>> cit.get_input() # Get user input from stdin
> Hello World
'Hello World'
>>> cit.get_input("Question?") # With a question
| (?) Question?
> Yes
'Yes'
>>> cit.get_input(prompt="Answer:") # With a customized prompt.
Answer: Apple
'Apple'
>>> cit.get_input("Continue?", default="yes") # With a default answer.
| (?) Continue?
> (yes) # Entered nothing
'yes'
>>> cit.get_input(strip=False) # Do not remove leading and trailing whitespaces from user input.
> # Whitespaces
' '
>>> cit.get_choice(["Apple", "Google"]) # Enter number to select.
| 1) Apple
| 2) Google
> 2
'Google'
>>> cit.get_choice(["Apple", "Google"]) # Enter string is ok too.
| 1) Apple
| 2) Google
> Google
'Google'
>>> cit.get_choice(["Apple", "Google"], exitable=True) # Add a choice of exit in menu.
| 1) Apple
| 2) Google
| 0) ** EXIT **
> 0
None
>>> cit.get_choices(["Apple", "Google"]) # Multiple Selection
| 1) [ ] Apple
| 2) [ ] Google
> 1 # Enter number to check or uncheck selections
| 1) [+] Apple
| 2) [ ] Google
| 0) ** DONE **
> Google # Enter string is ok too.
| 1) [+] Apple
| 2) [+] Google
| 0) ** DONE **
> 0 # Enter 0 when done.
['Apple', 'Google'] # return [] is no selections.
>>> cit.get_choices(["Apple", "Google"], allable=True) # Add a choice of select all in menu.
| a) ** ALL **
| 1) [ ] Apple
| 2) [ ] Google
> a # Enter `a` to check all. If `a` is in choices, enter `all`.
| a) ** ALL **
| 1) [+] Apple
| 2) [+] Google
| 0) ** DONE **
> 0
['Apple', 'Google']
>>> cit.get_choices(["Apple", "Google"], exitable=True) # Add a choice of exit in menu.
| 1) [ ] Apple
| 2) [ ] Google
| 0) ** EXIT **
> 0
[] # Empty list returned.
File IO
>>> cit.read_file("/path/to/file")
'Hello World'
>>> cit.read_file("/path/to/file", with_encoding=True)
('Hello World', 'utf-8')
>>> cit.write_file("/path/to/file", "Hello World") # Append content to file.
11 # writed bytes
>>> cit.write_file("/path/to/file", "Hello World", overwrite=True) # Overwrite if file exists.
11 # writed bytes
Controls
>>> cit.pause()
| Press [Enter] to Continue...
>>> cit.bye()
# exit
>>> cit.bye(0)
# exit with code 0
>>> cit.bye("Seeya")
Seeya
# exit
Decorators
@cit.as_session("Hello") # String as the Title of the session.
def my_func():
cit.echo("World")
>>> my_func()
+---------+
| HELLO() |
+---------+
| World
`
@cit.as_session # Use function name as the Title of the session.
def underscore_orCamel():
pass
>>> underscore_orCamel()
+-----------------------+
| UNDERSCORE OR CAMEL() |
+-----------------------+
`
@cit.deprecated_by(new_func): # A function object as argument.
def old_func(...):
pass # code here won't be actually executed.
>>> old_func(...)
Function `old_func` is deprecated, now calling `new_func` instead. # Warning printed to stderr.
# new_func(...) is called
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
consoleiotools-4.6.15.tar.gz
(11.4 kB
view details)
Built Distribution
File details
Details for the file consoleiotools-4.6.15.tar.gz
.
File metadata
- Download URL: consoleiotools-4.6.15.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05743d51e40611a4cb639676b708438bd8297982fc22c29defb90511078001c2 |
|
MD5 | 6f1f0ba95d04898c9dcab06734d03a4c |
|
BLAKE2b-256 | 7ad1dc8dc48321147eb2ec83bd751a3952f9a431de20e29795174ca9a274b049 |
File details
Details for the file consoleiotools-4.6.15-py3-none-any.whl
.
File metadata
- Download URL: consoleiotools-4.6.15-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d37a5ff93493d9bfd4f2144e51c0ed561c3ba1da04ca7ef6e5139c679e7d1b6 |
|
MD5 | 00d2b83ae2f83c44281aa3c59db8fe1a |
|
BLAKE2b-256 | 0138add58b26757e939e15c4b698a68b2b4eb99bdcfe41ad7d61af9c81063374 |