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 [2012/12/30 17:36]
ormaaj Must have duplicated this one
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 49: Line 50:
  
 ===== The more advanced printf way ===== ===== The more advanced printf way =====
-This one is a bit tricky. The format for the ''​printf''​ command is ''​%.0s'',​ which specified a field with the **maximum** length of **zero**. After this field, ''​printf''​ is told to print a dash. You might remember that it's the nature of ''​printf''​ to repeat, if the number of format ​specifications is less than the number of given arguments. With brace expansion ''​{1..20}'',​ 20 arguments are given (you could easily write ''​1 2 3 4 ... 20'',​ of course!). Following happens: The **zero-length field** plus the dash is repeated 20 times. A zero length field is, naturally, invisible. What you see is the dash, repeated 20 times.+This one is a bit tricky. The format for the ''​printf''​ command is ''​%.0s'',​ which specified a field with the **maximum** length of **zero**. After this field, ''​printf''​ is told to print a dash. You might remember that it's the nature of ''​printf''​ to repeat, if the number of conversion ​specifications is less than the number of given arguments. With brace expansion ''​{1..20}'',​ 20 arguments are given (you could easily write ''​1 2 3 4 ... 20'',​ of course!). Following happens: The **zero-length field** plus the dash is repeated 20 times. A zero length field is, naturally, invisible. What you see is the dash, repeated 20 times.
  
 <code bash> <code bash>
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.1356888969.txt
  • Last modified: 2012/12/30 17:36
  • by ormaaj