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:

p "Some ${strong}bold${x} text."
p "Some ${txtBlue}${em}blue italic${x} text."
text Colortext Backgroundfont Style
txtBlackbgBlackstrong
txtRedbgReddisabled
txtGreenbgGreenem
txtYellowbgYellowu
txtBluebgBlue
txtMagentabgMagenta
txtCyanbgCyan
txtWhitebgLightGray

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:

# This
p "${btnSuccess} Done! ${x} Well played ;)"

# Instead of that
p "${btnSuccess}Done!${x} Well played ;)"

text-center()

As it's names suggest, this function will place the argument it expects, in the center of the terminal window.

text-center "Nice day today, huh?"

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
h1 " -= ScriptTitle =-"
hr "*" "-"

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
h2 "Section title"
p  "In this section you will find..."

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
h3 "Social Links"
a  "https://github.com/Dev"
a  "https://twitter.com/Dev"

p()

The paragraph tag. General purpose text, that can recieve formating tags inside.

p "See ${link}https://acme.com${x} for more."

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.

p "Some paragraph."
br
em "Footer italic text."

span()

An "inline element", just a string folowed by no linebreak

span "some inline text, with 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:

em "Gosh, I'm so tilted!"

strong()

And the same goes for bold, strong text.

strong "Look at me, I'm so Strong!"

a()

Or if the whole line is just a link. This anchor tag will be styled as the ${link} shortcut.

a "https://website.io"

hr()

Taking two arguments, that function will place the first in the center of the terminal window, and fill the rest with the second.

# This:
hr "=*=" "-"

# Will turn into this:
# ----------=*=----------

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.

src/bin.sh
use "std/fmt"
article="https://some/api/endpoint"

# Getting the data
title="$(fetch $article | jq .title)"
excerpt="$(fetch $article | jq .excerpt)"
link="$(fetch $article | jq .link)"

main() {
  h1 "$title"
  p "$excerpt"
  a "$link"
}