nu-scripts/status-notification.nu
2024-03-15 13:51:15 +01:00

67 lines
1.8 KiB
Plaintext
Executable file

#!/usr/bin/env nu
# (ls).name | each {|item| (cat $"($item)/capacity") } | lines
# open uevent | lines | parse "{key}={value}" | transpose -r
# let batteries = ( (ls /sys/class/power_supply).name | each {|item| open $"($item)/uevent" } | each {|item| $item | lines | parse "{key}={value}" | transpose -r })
let dateString = ^date
let discordianDate = ^ddate
def get_batteries [] {
let power_supplies = (
(ls /sys/class/power_supply).name
| each {|item|
open $"($item)/uevent"
}
| each {|item|
$item | lines | parse "{key}={value}"
})
let batteries = $power_supplies | each {||
$in | dfr into-df | dfr get key | dfr contains POWER_SUPPLY_CAPACITY | dfr into-nu | get key | any {|| $in == true }
} | into record | items {|k,v|
if $v == true {
($power_supplies | get ($k | into int) | transpose -r)
}
} | each {|| $in}
$batteries | each {|i|
[ $i.POWER_SUPPLY_MANUFACTURER $i.POWER_SUPPLY_MODEL_NAME $i.POWER_SUPPLY_CAPACITY ]
| each {|j|
$j | first
}
} | each {|str|
$str | str join " "
} | each {|str|
$str | str replace -r '$' '%'
} | str join "\n"
}
# Documentation for notify
def notify [message: string] {
notify-send "Status" $message
}
def calendar [hours: int] {
let events = do { qcal -cron (60 * $hours) } | complete
if $events.stdout == "" {
$"No events in the next ($hours) hours."
} else {
$"Events in the next ($hours) hours:\n($events.stdout)"
}
}
# Documentation for construct_string
def construct_string [] {
[ $dateString
$discordianDate
(calendar 2)
(get_batteries)
] | str join "\n\n"
}
# Documentation for main
def main [] {
notify (construct_string)
}