| .. | ||
| bashformatterexample.gif | ||
| example_usage.sh | ||
| formatter_new.sh | ||
| README.md | ||
| task_formatter.sh | ||
BASH CLI Formatter with Spinner
This repository contains two scripts, task_formatter.sh and example_usage.sh, that work together to execute tasks with a spinner and formatted output. The task_formatter.sh script provides functions for running tasks with a spinner, while the example_usage.sh script demonstrates how to use these functions by defining and executing a set of tasks.
Example Output
Files
task_formatter.sh
This script contains functions for running tasks with a spinner and formatted output.
Functions
spinner(pid): Displays a spinner while a background task is running.run_task(task_description, task_command): Executes a task with a spinner and captures its output.run_all_tasks(tasks): Runs all tasks passed as arguments, using therun_taskfunction.
Usage
The task_formatter.sh script is designed to be sourced by another script, which can then call its functions to run tasks with formatted output.
example_usage.sh
This script demonstrates how to use the functions provided by task_formatter.sh to run a set of tasks.
Usage
-
Ensure both
task_formatter.shandexample_usage.share executable:chmod +x task_formatter.sh chmod +x example_usage.sh -
Run the
example_usage.shscript:./example_usage.sh
Example Tasks
The example_usage.sh script defines three example tasks:
Task 1: Runs thesample_taskfunction, which simulates a task running for 3 seconds.Task 2: Runs theanother_sample_taskfunction, which simulates a task running for 5 seconds and then returns an error.Task 3: Runs thesleep 2command, which simulates a task running for 2 seconds.
How It Works
- The
example_usage.shscript sources thetask_formatter.shscript to gain access to its functions. - The
example_usage.shscript defines a set of tasks in an array, with each task specified as a description and a command separated by a colon (:). - The
example_usage.shscript calls therun_all_tasksfunction fromtask_formatter.sh, passing the tasks as arguments. - The
run_all_tasksfunction iterates over the tasks, splits each task into a description and a command, and calls therun_taskfunction for each task. - The
run_taskfunction executes the task with a spinner and captures its output, displaying the task's status when it completes.
Spinner
The Spinner was adjusted from https://github.com/sindresorhus/cli-spinners
Example Output
Task 1 Running [⠼] [✔] Task 1 Completed 3s
Task 2 Running [⠧] [✘] Task 2 Error 5s
Error output:
Another sample task running
Task 3 Running [⠹] [✔] Task 3 Completed 2s
