std/fmt
Standard/Format library
Inspired by the Pug syntax, that collection of functions will put some semantics in our Bash scripts text output, instead of just "echo out something".
Color, backgroud, font-style
One or more of those variables will start the formating, and ${x} will end it. Example:
| text Color | text Background | font Style |
|---|---|---|
| txtBlack | bgBlack | strong |
| txtRed | bgRed | disabled |
| txtGreen | bgGreen | em |
| txtYellow | bgYellow | u |
| txtBlue | bgBlue | |
| txtMagenta | bgMagenta | |
| txtCyan | bgCyan | |
| txtWhite | bgLightGray |
The txtPrimary is a dynamic value, based on the value of the 'package.primaryColor' field in Sherpa.yaml or fallback to green.
Combinations (like CSS classes)
Instead of cluttering the script with multiple formating tags, some color/bg/style combinations are declared as variables so we would have just one starting tag then the ${x} to end it.
- btnSuccess : Black text on Green background
- btnWarning : Black text on Yellow background
- btnDanger : Black text on Red background
- link : Blue, italic, underlined text
For btnSomething tags, allow at least a space unit around the text, when used like a button or badge. It's better for visual effect:
text-center()
As it's names suggest, this function will place the argument it expects, in the center of the terminal window.
In order to do that, it will calculate the length of the provided string, substract it from the total "window columns" available, split that number in half, then prints: first half of empty spaces - string - the other half of empty spaces.
Semantic Formating & Styling
Using printf and text formating as we defined it earlier, here we are setting some defaults with expected visual styles for some semantic styles.
h1()
Heading1, expected to appear only once per terminal "page", it will give to the text:
- Blank line above
- Text: Strong + txtPrimary color
- Blank line below
h2()
Heading2, it's usual purpose is to serve as section title, to indicate a commun chunk of information:
- Text: txtPrimary color
- Blank line below
h3()
Heading3 is more on the "aside" or "footer" side of the semantic story, it will give to the text:
- Text: txtPrimary color + italic
- Blank line below
p()
The paragraph tag. General purpose text, that can recieve formating tags inside.
br()
A spacer helper, adding a blank line to the output in semantic fashion instead of the usual insertion of blank echo statements, or adding \n at the end or start of a string.
Having a clear tag for that purpose, separates the content from the formating.
span()
An "inline element", just a string folowed by no linebreak
em()
When the whole line is supposed to be italic, we can avoid cluttering the script with ${em} and ${x} tags inside a p "string", by just using the sort of <em> alias:
strong()
And the same goes for bold, strong text.
a()
Or if the whole line is just a link. This anchor tag will be styled as the ${link} shortcut.
hr()
Taking two arguments, that function will place the first in the center of the terminal window, and fill the rest with the second.
fetch()
This one is supposed to be elsewhere, but ended up here in the early days, and will be eventually moved later if needed.
It will simply take an API endpoint as argument, and GET the value of a field using jq.