snipplets:print_horizontal_line

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
snipplets:print_horizontal_line [2014/09/03 23:15]
fkl Directives introduced by ''%'' are called conversion specifications.
snipplets:print_horizontal_line [2018/05/09 23:37] (current)
pdeyoreo added missing newline between sections "The iterative way" and "The simple printf way"
Line 28: Line 28:
 echo echo
 </​code>​ </​code>​
 +
 ===== The simple printf way ===== ===== The simple printf way =====
 This one uses the ''​printf''​ command to print an **empty** field with a **minimum field width** of 20 characters. The text is padded with spaces, since there is no text, you get 20 spaces. The spaces are then converted to ''​-''​ by the ''​tr''​ command. This one uses the ''​printf''​ command to print an **empty** field with a **minimum field width** of 20 characters. The text is padded with spaces, since there is no text, you get 20 spaces. The spaces are then converted to ''​-''​ by the ''​tr''​ command.
Line 82: Line 83:
 ---------------------------------------------------------------- ----------------------------------------------------------------
 printf '​%s\n'​ "​${hr:​0:​${COLUMNS:​-$(tput cols)}}"​ printf '​%s\n'​ "​${hr:​0:​${COLUMNS:​-$(tput cols)}}"​
 +</​code>​
 +
 +A more flexible approach, and also using modal terminal line-drawing characters instead of hyphens:
 +
 +<code bash>
 +hr() {
 +  local start=$'​\e(0'​ end=$'​\e(B'​ line='​qqqqqqqqqqqqqqqq'​
 +  local cols=${COLUMNS:​-$(tput cols)}
 +  while ((${#line} < cols)); do line+="​$line";​ done
 +  printf '​%s%s%s\n'​ "​$start"​ "​${line:​0:​cols}"​ "​$end"​
 +}
 </​code>​ </​code>​
  
 ===== Related articles ===== ===== Related articles =====
   * [[commands:​builtin:​printf]]   * [[commands:​builtin:​printf]]
  • snipplets/print_horizontal_line.1409786110.txt
  • Last modified: 2014/09/03 23:15
  • by fkl