Skip to content

Instantly share code, notes, and snippets.

@reflexdemon
Last active September 15, 2021 12:30
Show Gist options
  • Save reflexdemon/703220bd436543a225e34b727007e653 to your computer and use it in GitHub Desktop.
Save reflexdemon/703220bd436543a225e34b727007e653 to your computer and use it in GitHub Desktop.
Auto boxing on Console and Terminal

Introduction

If at any given time you wanted to print something on terminal or your console and show box around it, have you tried playing with echo/printf command? With neatly typing each characters like below,

echo "┌────────────────┐";
echo "│ Cool New title │";
echo "└────────────────┘";

Here is a simple script that can be used to do the stuff in much cleaner way and has been inspired by this Youtube Video https://www.youtube.com/watch?v=1Uv8KfUjRBU.

Basic Function

Now that we wanted to print somehting inside the box, we could create a simple function that can be extended to print a box as we like it.

Title_base() {
        inputText="${1}"
        top_left="${2}"
        top_right="${3}"
	bottom_left="${4}"
	bottom_right="${5}"
	left="${6}"
	right="${7}"
	top="${8}"
	bottom="${9}"
        printf -v Bar '%*s' $((${#1} + 2)) ' '
        top_bar=${Bar// /${top}}
        bottom_bar=${Bar// /${bottom}}
        printf "%s\n${left} %s ${right}\n%s\n" "${top_left}${top_bar}${top_right}" "${inputText}" "${bottom_left}${bottom_bar}${bottom_right}"
}

The above defines a function for us to print the Title in a box. Here is an example how we can call it.

Title_base "Cool New title" "" "" "" "" "" "" "" ""

Output:
┌────────────────┐
│ Cool New title │
└────────────────┘

Wrapper to simplify

We could simplyfy this function call by writing simple wrappers like this.

# Simple Box
Title() {
	inputText="$1"
	top_left=''
	top_right=''
	bottom_left=''
	bottom_right=''
	left=''
	right=''
	top=''
	bottom=''
        Title_base "$inputText" "$top_left" "$top_right" "$bottom_left" "$bottom_right" "$left" "$right" "$top" "$bottom"
}
# Bold Box
Title_b() {
        inputText="$1"
	top_left=''
	top_right=''
	bottom_left=''
	bottom_right=''
	left=''
	right=''
	top=''
	bottom=''
        Title_base "$inputText" "$top_left" "$top_right" "$bottom_left" "$bottom_right" "$left" "$right" "$top" "$bottom"
}
# Curved Edges Box
Title_c() {
        inputText="$1"
	top_left=''
	top_right=''
	bottom_left=''
	bottom_right=''
	left=''
	right=''
	top=''
        Title_base "$inputText" "$top_left" "$top_right" "$bottom_left" "$bottom_right" "$left" "$right" "$top"
}

Thus, you may call them like this and see the output for youself.

$ Title 'Cool New title'
┌────────────────┐
│ Cool New title │
└────────────────┘

$ Title_b 'Cool New title'
▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ Cool New title ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟

$ Title_c 'Cool New title'
╭────────────────╮
│ Cool New title │
╰────────────────╯

Conclution

This is just somehting new I learnt and thought it might be helpful for others to learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment