Browse Source

Add bash version of script. Add Support for OSX.

Tyler Cipriani 6 years ago
parent
commit
d55c2aa4fb
1 changed files with 33 additions and 0 deletions
  1. 33
    0
      bash/wthr

+ 33
- 0
bash/wthr View File

@@ -0,0 +1,33 @@
1
+#!/usr/bin/env bash
2
+
3
+if ! command -v bc > /dev/null 2>&1; then
4
+    printf "%s\n" "You must have the 'bc' package installed to run this program"
5
+fi
6
+
7
+snowflakes=(❄ ❅ ❆)
8
+width=$(tput cols)
9
+platform=$(uname -s)
10
+
11
+if [ $platform = 'Linux' ]; then
12
+    cpu_count=$(grep -ic ^processor /proc/cpuinfo);
13
+elif [ $platform = 'Darwin' ]; then
14
+    cpu_count=$(sysctl -n hw.ncpu)
15
+fi
16
+
17
+
18
+while true; do
19
+    if [ $platform = 'Linux' ]; then
20
+        load=$(cut -d ' ' -f 1 /proc/loadavg)
21
+    elif [ $platform = 'Darwin' ]; then
22
+        load=$(sysctl -n vm.loadavg | cut -d ' ' -f 2)
23
+    fi
24
+    heaviness=$(echo "scale=10;$load/$cpu_count" | bc)
25
+    rand_col=$(( (RANDOM % width) + 1 ))
26
+    printf "%${rand_col}s\n" "${snowflakes[$(( (RANDOM % ${#snowflakes[*]}) ))]}"
27
+
28
+    if [ "$(echo "$heaviness > 1" | bc -l)" -eq 1 ]; then
29
+        sleep "$(echo "scale=10;1/$heaviness" | bc)"
30
+    else
31
+        sleep .75
32
+    fi
33
+done

Loading…
Cancel
Save