aboutsummaryrefslogtreecommitdiffstats
path: root/.config/lemonbar-xft/bar.sh
blob: 6aad4b805786135890ac6f5b40c47758604fedd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/sh
set -o errexit

RED="%{F#cd0000}"
YELLOW="%{F#cdcd00}"
RESET="%{F-}"

main() {
  while true; do
    update_bar
    sleep 5
  done
}

update_bar() {
  printf "%%{l} %s | %s | %s | %s | %s %%{r} %s | %s | %s \n" \
    "$(battery)" \
    "$(temp)" \
    "$(cpu)" \
    "$(memory)" \
    "$(brightness)" \
    "$(volume)" \
    "$(wifi)" \
    "$(datetime)"
}

battery() {
  apm_output=$(apm -al)
  percent=$(echo "$apm_output" | sed -n '1p')
  batt_state=$(echo "$apm_output" | sed -n '2p')
  minutes=$(apm -m)

  filled=$((percent / 10))
  bar=""
  i=0
  while [ $i -lt 10 ]; do
    if [ $i -lt $filled ]; then
      if [ "$batt_state" = "1" ]; then
        bar="${bar}+"
      else
        bar="${bar}#"
      fi
    else
      bar="${bar}-"
    fi
    i=$((i + 1))
  done

  if [ "$percent" -lt 30 ] && [ "$batt_state" != "1" ]; then
    bar="${RED}${bar}${RESET}"
  elif [ "$percent" -lt 50 ] && [ "$batt_state" != "1" ]; then
    bar="${YELLOW}${bar}${RESET}"
  fi

  printf "%s%% %s (%s min)" "$percent" "$bar" "$minutes"
}

temp() {
  avg=$(sysctl hw.sensors | awk -F'=' '/temp/ {
        gsub(/degC.*/, "", $2);
        sum += $2;
        count++
    } END {
        printf "%.0f", sum/count
    }')

  if [ "$avg" -ge 85 ]; then
    printf "%s%d°C%s" "$RED" "$avg" "$RESET"
  else
    printf "%d°C" "$avg"
  fi
}

cpu() {
  apm -Pv | awk '{gsub(/\(|\)/, ""); print $5, $6, $4}'
}

memory() {
  top -n | awk '/^Memory:/ {
        split($3, r, "/");
        printf "Used:%s Free:%s", r[2], $6
    }'
}

net() {
  ifconfig egress 2>/dev/null | awk '/inet / {print $2}'
}

brightness() {
  bright=$(xbacklight -get | cut -d. -f1)
  printf "Bri: %d%%" "$bright"
}

volume() {
  vol=$(sndioctl -n output.level)
  percent=$(printf "%.0f" "$(echo "$vol" | awk '{print $1 * 100}')")

  if sndioctl output.mute | grep -q "=1"; then
    printf "Vol: MUTE"
  else
    printf "Vol: %d%%" "$percent"
  fi
}

wifi() {
  wifi_info=$(ifconfig iwx0 2>/dev/null | grep "ieee80211:")
  if [ -n "$wifi_info" ]; then
    ssid=$(echo "$wifi_info" | sed -n 's/.*join \(.*\) chan.*/\1/p' | tr -d '"')
    signal=$(ifconfig iwx0 | awk '/ieee80211:/ {for(i=1;i<=NF;i++) if($i ~ /%/) print $i}' | head -1)
    printf "%s %s %s" "$ssid" "$signal" "$(net)"
  else
    net
  fi
}

datetime() {
  date '+%Y-%m-%d %H:%M'
}

main