Advertisement
Guest User

Untitled

a guest
Mar 31st, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.37 KB | None | 0 0
  1. [:~/tmp]4 $ cat ./progressbar
  2. progress_bar() {
  3.     local progress="$1"
  4.     local bar_length=48
  5.  
  6.     # Force progress to 100 if it is >= 100.
  7.     if (( $(echo "$progress >= 100" | bc -l) )); then
  8.         progress=100
  9.     fi
  10.  
  11.     # Calculate number of filled characters (rounded)
  12.     local filled
  13.     filled=$(printf "%.0f" "$(echo "$progress * $bar_length / 100" | bc -l)")
  14.     local empty=$((bar_length - filled))
  15.  
  16.     local filled_char="█"
  17.     local empty_char="_"
  18.  
  19.     # Build the underlying bar
  20.     local bar_filled
  21.     bar_filled=$(printf "%0.s$filled_char" $(seq 1 $filled))
  22.  
  23.     # if the progress is 0, then the bar should be empty
  24.     if [[ $(echo "$1 == 0" | bc) -eq 1 ]]; then
  25.         bar_filled=""
  26.     fi
  27.  
  28.     local bar_empty
  29.     bar_empty=$(printf "%0.s$empty_char" $(seq 1 $empty))
  30.     local bar="${bar_filled}${bar_empty}"
  31.  
  32.     # Format percentage string with 2 decimals and exactly one space before and after.
  33.     local percent_value
  34.     percent_value=$(printf "%.2f%%" "$progress")
  35.     local percent_str=" ${percent_value} "
  36.     local percent_len=${#percent_str}
  37.  
  38.     # Calculate insertion point (index where the percent_str should start within bar)
  39.     local insert_index=$(( (bar_length - percent_len) / 2 ))
  40.  
  41.     # Build final bar by replacing a segment of the underlying bar with percent_str.
  42.     local final_bar="${bar:0:insert_index}${percent_str}${bar:insert_index+percent_len}"
  43.  
  44.     #removing last character from final_bar
  45.     final_bar=${final_bar%?}
  46.  
  47.     # Enclose with vertical bars.
  48.     echo "${GREEN}|${LIGHT_GREEN}${final_bar}${GREEN}|${NC}"
  49. }
  50.  
  51. for i in 100 {0..110..3}.12; do progress_bar $i; done
  52. [:~/tmp]4 $ bash ./progressbar
  53. |███████████████████ 100.00% ████████████████████|
  54. |█___________________ 0.12% _____________________|
  55. |█___________________ 3.12% ____________________|
  56. |███_________________ 6.12% ____________________|
  57. |████________________ 9.12% ____________________|
  58. |██████______________ 12.12% ___________________|
  59. |███████_____________ 15.12% ___________________|
  60. |█████████___________ 18.12% ___________________|
  61. |██████████__________ 21.12% ___________________|
  62. |████████████________ 24.12% ___________________|
  63. |█████████████_______ 27.12% ___________________|
  64. |██████████████______ 30.12% ___________________|
  65. |████████████████____ 33.12% ___________________|
  66. |█████████████████___ 36.12% ___________________|
  67. |███████████████████_ 39.12% ___________________|
  68. |████████████████████ 42.12% ___________________|
  69. |████████████████████ 45.12% ___________________|
  70. |████████████████████ 48.12% ___________________|
  71. |████████████████████ 51.12% ___________________|
  72. |████████████████████ 54.12% ___________________|
  73. |████████████████████ 57.12% ___________________|
  74. |████████████████████ 60.12% █__________________|
  75. |████████████████████ 63.12% ██_________________|
  76. |████████████████████ 66.12% ████_______________|
  77. |████████████████████ 69.12% █████______________|
  78. |████████████████████ 72.12% ███████____________|
  79. |████████████████████ 75.12% ████████___________|
  80. |████████████████████ 78.12% █████████__________|
  81. |████████████████████ 81.12% ███████████________|
  82. |████████████████████ 84.12% ████████████_______|
  83. |████████████████████ 87.12% ██████████████_____|
  84. |████████████████████ 90.12% ███████████████____|
  85. |████████████████████ 93.12% █████████████████__|
  86. |████████████████████ 96.12% ██████████████████_|
  87. |████████████████████ 99.12% ████████████████████|
  88. |███████████████████ 100.00% ████████████████████|
  89. |███████████████████ 100.00% ████████████████████|
  90. |███████████████████ 100.00% ████████████████████|
  91. [:~/tmp]4 $ cat progressbar2
  92. progress_bar() {
  93.     local progress_exact="$1"
  94.     local progress="${progress_exact%.*}"
  95.     local bar_length=48
  96.  
  97.     # Force progress to 100 if it is >= 100.
  98.     (( progress = progress <= 100 ? progress : 100 ))
  99.  
  100.     # Calculate number of filled characters (rounded)
  101.     local filled empty
  102.     (( filled = progress * bar_length / 100,
  103.        empty = bar_length - filled ))                                                                                                                   21:14:04 [21/1970]
  104.  
  105.     local filled_char="█"
  106.     local empty_char="_"
  107.  
  108.     # Build the underlying bar
  109.     local bar_filled
  110.     printf -v bar_filled '%*s' $filled ""
  111.     bar_filled=${bar_filled// /$filled_char}
  112.  
  113.     local bar_empty
  114.     printf -v bar_empty '%*s' $empty ""
  115.     bar_empty=${bar_empty// /$empty_char}
  116.  
  117.     local bar="${bar_filled}${bar_empty}"
  118.  
  119.     # Format percentage string with 2 decimals and exactly one space before and after.
  120.     local percent_value=$(printf "%.2f%%" "$progress_exact")
  121.     local percent_str=" ${percent_value} "
  122.     local percent_len=${#percent_str}
  123.  
  124.     # Calculate insertion point (index where the percent_str should start within bar)
  125.     local insert_index=$(( (bar_length - percent_len) / 2 ))
  126.  
  127.     # Build final bar by replacing a segment of the underlying bar with percent_str.
  128.     local final_bar="${bar:0:insert_index}${percent_str}${bar:insert_index+percent_len}"
  129.  
  130.     # Enclose with vertical bars.
  131.     echo "${GREEN}|${LIGHT_GREEN}${final_bar}${GREEN}|${NC}"
  132. }
  133.  
  134. for i in 100 {0..110..3}.12; do progress_bar $i; done
  135. [:~/tmp]4 $ bash ./progressbar2
  136. |███████████████████ 100.00% ████████████████████|
  137. |____________________ 0.12% _____________________|
  138. |█___________________ 3.12% _____________________|
  139. |██__________________ 6.12% _____________________|
  140. |████________________ 9.12% _____________________|
  141. |█████_______________ 12.12% ____________________|
  142. |███████_____________ 15.12% ____________________|
  143. |████████____________ 18.12% ____________________|
  144. |██████████__________ 21.12% ____________________|
  145. |███████████_________ 24.12% ____________________|
  146. |████████████________ 27.12% ____________________|
  147. |██████████████______ 30.12% ____________________|
  148. |███████████████_____ 33.12% ____________________|
  149. |█████████████████___ 36.12% ____________________|
  150. |██████████████████__ 39.12% ____________________|
  151. |████████████████████ 42.12% ____________________|
  152. |████████████████████ 45.12% ____________________|
  153. |████████████████████ 48.12% ____________________|
  154. |████████████████████ 51.12% ____________________|
  155. |████████████████████ 54.12% ____________________|
  156. |████████████████████ 57.12% ____________________|
  157. |████████████████████ 60.12% ____________________|
  158. |████████████████████ 63.12% ██__________________|
  159. |████████████████████ 66.12% ███_________________|
  160. |████████████████████ 69.12% █████_______________|
  161. |████████████████████ 72.12% ██████______________|
  162. |████████████████████ 75.12% ████████____________|
  163. |████████████████████ 78.12% █████████___________|
  164. |████████████████████ 81.12% ██████████__________|
  165. |████████████████████ 84.12% ████████████________|
  166. |████████████████████ 87.12% █████████████_______|
  167. |████████████████████ 90.12% ███████████████_____|
  168. |████████████████████ 93.12% ████████████████____|
  169. |████████████████████ 96.12% ██████████████████__|
  170. |████████████████████ 99.12% ███████████████████_|
  171. |███████████████████ 102.12% ████████████████████|
  172. |███████████████████ 105.12% ████████████████████|
  173. |███████████████████ 108.12% ████████████████████|
  174. [:~/tmp]4 $
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement