1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-02 07:07:46 +00:00

add timed_weather that checks the weather at timed intervals (#349)

This commit is contained in:
Darren Schroeder 2023-01-18 08:54:04 -06:00 committed by GitHub
parent 6471266bd9
commit e478643823
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,44 @@
# This script will run a command, in this case get_weather, at a prescribed interval
# This is meant to be run from your prompt so that it runs every time you cd or
# run a command. Each time it will check if it's been longer than the interval and
# if so, run the command, otherwise it uses the cached weather information.
# I wrote this so I could have weather in my prompt but not pay the price of hitting
# the web for every prompt.
# this script is depenedent on get-weather
use get-weather.nu get_weather
# Create a mutable weather table to hold the weather data
let-env WEATHER = (get_weather | upsert last_run_time { (date now | date format '%Y-%m-%d %H:%M:%S %z')})
#command to run at interval
def-env timed_weather_run [
--interval(-i): duration # The interval duration
] {
# get the last runtime and add my timezone difference
let last_runtime = ($env.WEATHER | get last_run_time | into datetime)
if $last_runtime + $interval > (date now) {
# $"interval not met. last_runtime: [($last_runtime)](char nl)"
let temp = ($env.WEATHER.Temperature)
let emoji = ($env.WEATHER.Emoji)
{
Temperature: ($temp)
Source: "cache"
Emoji: ($emoji)
}
} else {
# $"interval met, running command: [($command)](char nl)"
let-env WEATHER = (get_weather | upsert last_run_time { (date now | date format '%Y-%m-%d %H:%M:%S %z')})
let temp = ($env.WEATHER.Temperature)
let emoji = ($env.WEATHER.Emoji)
{
Temperature: ($temp)
Source: "expired-cache"
Emoji: ($emoji)
}
}
}
timed_weather_run --interval 1min