printstyled writes text with terminal styling, making command-line output easier to scan. It is especially useful for highlighting headings, labels, or important values in formatted displays such as pizza-sales and sine tables. section 5.0 section 5.1
Use printstyled when ordinary print or println output needs visual emphasis:
- Pass text as one or more arguments and select a named color with the color keyword, such as color = :cyan. section 5.1.1
- Use bold=true for bold output; available styling symbols include colors and attributes such as underline, reverse, blink, and hidden. section 5.1.1
- Use symbols for color names, for example :cyan, rather than memorizing terminal escape sequences. section 5.1.1
- Combine printstyled with print, println, rpad, and lpad to create aligned, readable terminal tables. section 5.0 section 5.1
Highlighting a table heading
printstyled(rpad("name", 12), color=:cyan) print(" │ ") printstyled("size", color=:cyan) print(" │ ") printstyled(rpad("price", 5), color=:cyan) println(" │") This pattern colors only the headings while ordinary print and println calls render the table structure and rows. section 5.1
Listing 5.1 Creating a pretty table of pizza sales
function print_pizzatable(pizzas)
print("│ ")
printstyled(rpad("name", 12), color=:cyan)
print(" │ ")
printstyled("size", color=:cyan)
print(" │ ")
printstyled(rpad("price", 5), color=:cyan)
println(" │")
for pz in pizzas
print("│ ", rpad(name(pz), 12))
print(" │ ", rpad(portion(pz), 4), " │ ")
println(lpad(price(pz), 5), " │")
end
end
Inspect the complete pizza-table function to see printstyled used with padding, tuple accessors, loops, and mixed colored and uncolored output. section 5.1
