Skip to content

About

ibnew_shell.sh is a lightweight Bash library that simplifies building interactive terminal applications with automatic cursor management, ANSI coloured output, and structured logging.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

libnew_shell.sh

A lightweight, terminal‑oriented Bash library for interactive scripts

License: MIT Bash 4.4+ ShellCheck


libnew_shell.sh provides a reusable set of Bash functions that simplify building interactive terminal applications. It handles cursor positioning, coloured output, logging, user input forms, and mirror configuration – all with a clean, minimal API.


✨ Features

  • Cursor‑aware input fields – field:read positions the prompt and cursor dynamically, wrapping long lines automatically.
  • Line counting – count:line computes how many terminal lines a string will occupy, perfect for multi‑line prompts.
  • ANSI colour support – ready‑to‑use functions for 16‑colour (color:*) and 256‑colour (color256:*) output, with begin/end blocks.
  • Structured logging – log:success, log:failure, log:waring print coloured, formatted messages to stdout or stderr.
  • Interactive mirror wizard – tui:mirror:source walks the user through mirror setup, with optional reuse of an existing mirror.
  • Array membership check – str:found_match efficiently tests if a string is present in a space‑separated list.

📦 Installation

Simply source the library in your script:

source /path/to/libnew_shell.sh

Or, if you keep it in a standard location:

source "${HOME}/.local/lib/libnew_shell.sh"

🚀 Usage Examples

Coloured output

# Simple coloured message (green)
color:print "✔ All tests passed" $green

# 256‑colour output to stderr
color256:print "⚠ Warning: disk space low" 208 1   # 1 = stderr

Input forms with cursor management

x=1; y=1                # initialise coordinates
field:read "Name: " name 2
field:read "Email: " email 2
field:end               # reset cursor to (1,1)

Logging helpers

log:success "setup" "Configuration applied successfully"
log:failure "network" "Could not reach the mirror"
log:waring "disk" "Only 10% free space remaining"

Mirror configuration wizard

tui:mirror:source       # interactively set up a mirror source

📖 Function Reference

field:read

field:read <prompt> <variable_name> [offset]
  • Reads user input with read -e -r -p.
  • Updates global x/y coordinates, wrapping lines as needed.
  • offset (default: 1) adds extra spacing.

field:end

field:end

Resets x=1; y=1 and prints a newline to restore normal terminal flow.

count:line

count:line <string>

Returns the number of terminal lines the string will occupy, or 0 if the terminal width cannot be determined.

Colour functions

Function Description
color:print <text> <colour_code> [stderr] [no_newline] Print coloured text
color:start <colour_code> / color:end Begin/end a colour block
color256:print <text> <colour_code> [stderr] Print 256‑colour text
color256:start <colour_code> / color256:end Begin/end a 256‑colour block

Logging

Function Description
log:success <function_name> <message> Log success message
log:failure <function_name> <message> Log failure message
log:waring <function_name> <message> Log warning message
log:show <function_name> <message> <name_colour> <msg_colour> [stderr] Low‑level logging builder

tui:mirror:source

tui:mirror:source

Interactive mirror configuration. Asks whether to reuse an existing mirror, then collects name, dists, components, and arch. Calls mirror:source and mirror:default (which you must provide in your script).

str:found_match

str:found_match <space_separated_list> <target>

Returns 0 if <target> is found in the list, 1 otherwise. Useful for validating user input against a set of allowed values.


⚙️ Global Variables

The library uses several global variables – make sure they are not overwritten accidentally:

Variable Purpose
__LIB_TOOLS_LOADED__ Guard against multiple sourcing
red, green, yellow, blue, purple Standard ANSI colour codes (31–35)
pipeline_normal, pipeline_error Output stream flags (0 = stdout, 1 = stderr)
no_new_line Flag (1) to suppress trailing newline
x, y Current cursor coordinates (used by field:read)

🔧 Dependencies

  • Bash 4.4 or later
  • tput from ncurses (for terminal width detection)
  • A terminal that supports ANSI escape codes

📝 License

This project is licensed under the MIT License – see the LICENSE file for details.


🤝 Contributing

Issues and pull requests are welcome!
Please keep the code POSIX‑compatible where possible, and document any new functions with a brief header comment.


📬 Contact

For questions or suggestions, please open an issue on GitHub.


Made with ❤️ for interactive Bash scripting.

About

ibnew_shell.sh is a lightweight Bash library that simplifies building interactive terminal applications with automatic cursor management, ANSI coloured output, and structured logging.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages