Skip to main content

Console helper tools for CLIs

Project description

dt-console

dt-console is a Python library to simplify CLI input and output for your python apps/scripts. It has been tested in Windows and Linux.

Features include:

  • ConsoleHelper - manage window and cursor control
    • Set console window title
    • Show/Hide console window, set fg/bg colors
    • Set cursor style, location, ...
    • Routines to print and colorize output text
  • ConsoleInputHelper - Keyboard input prompts with validation and timeouts.
  • MessageBox - GUI messsagebox (alert, confirm, input, password)
  • ProgressBar - Display visual progress on screen via configurable progress bar
  • Spinner - Display visual progress on screen via configuable spinner control

Package donumentation can be found here.

Installation

Download source code from githup via git

git clone https://github.com/JavaWiz1/dt-console.git

Note, when downloading source, Poetry was used as the package manager. Poetry handles creating the virtual environment and all dependent packages installs with proper versions.

To setup virtual environment with required production AND dev (sphinx) dependencies:

poetry install

with ONLY production packages (no sphinx):

poetry install --without dev

use the package manager pip to install dt-console.

pip install dt-console [--user]

Usage

A demo cli has been included to show how these modules can be used. The demo selectively displays each control (console tools, input prompt, messagebox, ProgressBar and Spinner) and source is provided to review for implementation details.

See dt_tools.cli.dt_console_demos.py for detailed demo examples (runnable demo)

To run the demo type:

python -m dt_tools.cli.dt_console_demos

# or if via source (and poetry)
poetry run python -m dt_tools.cli.dt_console_demos

Developer package documentation contains details on all classes and supporting code (i.e. constant namespaces and enums) use for method calls. Docs can be found here.

Main classes/modules Overview

ConsoleHelper

ConsoleHelper provides methods for managing the console windows.

    from dt_tools.console.console_helper import ConsoleHelper
    import time

    console.clear_screen(cursor_home=True)
 
    console_size = console.get_console_size()
    row, col = console.cursor_current_position()
    print(f'Console size: {console_size}, cur pos: {row},{col}')
 
    console.print_at(row=3, col=5, msg="Here we are at row 3, column 5", eol='\n\n')
    time.sleep(.5)
 
    blue = console.cwrap('blue', cc.CBLUE)
    brown = console.cwrap('brown', cc.CBEIGE)
    green = console.cwrap('green', cc.CGREEN)
    text = f"The {blue} skies and the {brown} bear look wonderful in the {green} forest!"
    print(text)
 
    row, col = console.cursor_current_position()
    print(f'         at ({row},{col})', flush=True)
    time.sleep(2)
    console.print_at(row,col,'Finished')

ConsoleInputHelper

ConsoleInputHelper provides a customizable input prompt.

    from dt_tools.console.console_helper import ConsoleInputHelper

    console_input = ConsoleInputHelper()

    resp = console_input.get_input_with_timeout(prompt='Do you want to continue (y/n) > ', 
                                                valid_responses=console_input.YES_NO_RESPONSE, 
                                                default='y', 
                                                timeout_secs=5)
    print(f'  returns: {resp}')

MessageBox

Message box implements Alert, Confirmation, Input Prompt, Password Prompt message boxes.

    import dt_tools.console.msgbox as msgbox

    resp = msgbox.alert(text='This is an alert box', title='ALERT no timeout')
    print(f'  mxgbox returns: {resp}')

    resp = msgbox.alert(text='This is an alert box', title='ALERT w/Timeout', timeout=3000)
    print(f'  mxgbox returns: {resp}')

ProgressBar

ProgressBar is an easy to use, customizable console ProgressBar which displays percentage complete and elapsed time.

    from dt_tools.console.progress_bar import ProgressBar
    import time

    print('Progress bar...')
    pbar = ProgressBar(caption="Test bar 1", bar_length=40, max_increments=50, show_elapsed=False)
    for incr in range(1,51):
        pbar.display_progress(incr, f'incr [{incr}]')
        time.sleep(.15)    

    print('\nProgress bar with elapsed time...')
    pbar = ProgressBar(caption="Test bar 2", bar_length=40, max_increments=50, show_elapsed=True)
    for incr in range(1,51):
        pbar.display_progress(incr, f'incr [{incr}]')
        time.sleep(.15)

Spinner

Spinner is an easy to use, customizable console Spinner control which displays spinning icon and elapsed time.

    from dt_tools.console.spinner import Spinner, SpinnerType
    import time

    # Example to display all spinner types for approx 5 sec. apiece
    for spinner_type in SpinnerType:
        spinner = Spinner(caption=spinner_type, spinner=spinner_type, show_elapsed=True)
        spinner.start_spinner()
        
        # Do long task...
        for cnt in range(1,20):
            time.sleep(.25)

        spinner.stop_spinner()

License

MIT

The MessageBox code was derived from:
PyMsgBox - BSD for PyMsgBox (see source for msgbox.py)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dt_console-0.1.12.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

dt_console-0.1.12-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file dt_console-0.1.12.tar.gz.

File metadata

  • Download URL: dt_console-0.1.12.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.6 Windows/10

File hashes

Hashes for dt_console-0.1.12.tar.gz
Algorithm Hash digest
SHA256 7e294ec7327c6c40cd9f1eb5bb83692d2ae636c55973a100b1b051f103757f7e
MD5 7ae006c936877b034c6816988f0231c1
BLAKE2b-256 d0f904b71fbc247e6ccc746333ffc6cfc836daffaf4ff0251889c2b0370a193b

See more details on using hashes here.

File details

Details for the file dt_console-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: dt_console-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.6 Windows/10

File hashes

Hashes for dt_console-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 172ca1a96ada20169186be7ac37ef776e44e1d475a70b78e83e4adaaeb5f1fb3
MD5 c17870ed1475791cf8e4b477978b0af6
BLAKE2b-256 aedbbba14cf47d194f665a5fc0f81364e2d3df2cdc2a4920513490552e691449

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page