eww-battery-monitor 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env -S awk -f
  2. # -*- mode: awk -*-
  3. function get_icon(charge) {
  4. if (charge <= 10) {
  5. return "󰂃";
  6. } else if (charge <= 20) {
  7. return "󰁻";
  8. } else if (charge <= 30) {
  9. return "󰁼";
  10. } else if (charge <= 40) {
  11. return "󰁽";
  12. } else if (charge <= 50) {
  13. return "󰁾";
  14. } else if (charge <= 60) {
  15. return "󰁿";
  16. } else if (charge <= 70) {
  17. return "󰂀";
  18. } else if (charge <= 80) {
  19. return "󰂁";
  20. } if (charge < 100) {
  21. return "󰂂";
  22. } else {
  23. return "󰁹"
  24. }
  25. }
  26. function notify_send(title, desc, id) {
  27. if (!id) {
  28. cmd="dunstify -t 0 -p \"" title "\" \"" desc "\""
  29. while ((cmd | getline id) > 0) { }
  30. close(cmd)
  31. } else {
  32. system("dunstify -r " id " \"" title "\" \"" desc "\"")
  33. }
  34. return id
  35. }
  36. function close_notify(id) {
  37. system("dunstify -C " id)
  38. }
  39. function warn_if_low() {
  40. percentage = state_info["percentage"]
  41. id = state_info["id"]
  42. notified = state_info["notified"]
  43. if (percentage <= 10) {
  44. if (!notified) {
  45. state_info["id"] = notify_send("Battery Low", percentage "%", id)
  46. state_info["notified"] = 1
  47. }
  48. } else if (notified) {
  49. close_notify(id)
  50. state_info["notified"] = 0
  51. }
  52. }
  53. function print_state() {
  54. percentage = state_info["percentage"]
  55. printf "%s%s%d%%\n",
  56. get_icon(percentage),
  57. state_info["charging"] ? "󱐋" : "",
  58. percentage
  59. fflush()
  60. }
  61. function parse_record(record, exit_on_absent) {
  62. split(record, fields)
  63. for (i in fields) {
  64. match(fields[i], /^ *([^:]+): *(.+)$/, parts)
  65. if (length(parts) >= 3) {
  66. props[parts[1]] = parts[2]
  67. }
  68. }
  69. name = props["native-path"]
  70. if (name == "") {
  71. return 0
  72. } else if ((! BATTERY && props["power supply"] == "yes" && \
  73. props["native-path"] ~ /BAT[0-9]+/) || name == BATTERY) {
  74. state_info["percentage"] = props["percentage"] + 0
  75. return 1
  76. } else if ((! ADAPTER && props["power supply"] == "yes" && \
  77. props["native-path"] ~ /ADP[0-9]+/) || name == ADAPTER) {
  78. state_info["charging"] = (props["online"] == "yes")
  79. return 1
  80. } else {
  81. return 0
  82. }
  83. }
  84. function print_initial_stats() {
  85. cmd = "upower --dump"
  86. found = 0
  87. while ((cmd | getline record) > 0) {
  88. if (record ~ /Device: \/org\/freedesktop\/UPower\/devices\// \
  89. && parse_record(record)) {
  90. found = 1
  91. }
  92. }
  93. close(cmd)
  94. if (! found) {
  95. # we found no battery adapters
  96. exit 1
  97. }
  98. print_state()
  99. warn_if_low()
  100. }
  101. BEGIN {
  102. RS = ""
  103. FS = "\n"
  104. print_initial_stats()
  105. cmd = "upower --monitor-detail"
  106. while ((cmd | getline record) > 0) {
  107. if (record ~ /device changed/) {
  108. parse_record(record)
  109. print_state()
  110. warn_if_low()
  111. }
  112. }
  113. close(cmd)
  114. }