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

refactor: (#418)

* refactor:  move in one commit

Eveything in modules should probably be changed to `exported` defs.
The idea is to move everything first to keep proper history.

* refactor: 📝 add modules readme (wip)

* refactor:  small move

* refactor: 📝 changed nestring, updated modules readme

* refactor: 📝 to document or not to document

* fix: 🐛 themes

replaced the template to use `main` and regenerated them
from lemnos themes.

* Revert "fix: 🐛 themes"

This reverts commit 4918d3633c8d2d81950a0ed0cfd9eb84241bc886.

* refactor:  introduce sourced

- Created a source `root` in which sourcable demos are stored.
  Some might get converted to modules later on.
- Moved some files to bin too.

* fix: 🐛 fehbg.nu

* fix: 🐛 modules/after.nu

* moved some other stuff around

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
Mel Massadian 2023-04-26 00:56:25 +02:00 committed by GitHub
parent 382696cd21
commit c47ccd42b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
128 changed files with 185 additions and 12 deletions

View file

@ -0,0 +1,80 @@
# 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. It will need to be tweaked to actually be used in a
# prompt, but for now it just prints the weather in these 3 ways.
# 1. if it's never been run, it runs the weather command and saves the json cache info
# 2. if the interval has not expired yet, it prints the Cached information
# 3. if the interval has expired, it runs the weather command again and caches the info
# this script is depenedent on get-weather
source get-weather.nu
#command to run at interval
def timed_weather_run [
--command(-c): string # The command to run
--interval(-i): duration # The interval duration
] {
# get the type of system we're on
let system_name = ((sys).host | get name)
if $system_name == "Windows" {
# $"The system is Windows(char nl)"
# generate temp file name
let weather_runtime_file = (($env.TMP) | path join weather_runtime_file.json)
# does the temp file already exist, meaning we've written it previously
if ($weather_runtime_file | path exists) {
# $"Weather path exists [($weather_runtime_file)](char nl)"
# open the file and get the last weather data and run time out of it
let last_runtime_data = (open $weather_runtime_file)
# get the last runtime and add my timezone difference
let last_runtime = ($last_runtime_data | get last_run_time | into datetime)
if $last_runtime + $interval > (date now) {
# $"interval not met. last_runtime: [($last_runtime)](char nl)"
let temp = ($last_runtime_data.Temperature)
let emoji = ($last_runtime_data.Emoji)
{
Temperature: ($temp)
Source: "cache"
Emoji: ($emoji)
}
} else {
# save the run time and run the command
# $"interval met, running command: [($command)](char nl)"
# it would be nice to run a dynamic command here but doesn't appear to be possible
# let weather_table = (do { $command })
let weather_table = (if $command == "get_weather" {(get_weather)})
let temp = ($weather_table.Temperature)
let emoji = ($weather_table.Emoji)
{
Temperature: ($temp)
Source: "expired-cache"
Emoji: ($emoji)
}
$weather_table | upsert last_run_time {(date now | date format '%Y-%m-%d %H:%M:%S %z')} | save $weather_runtime_file
}
} else {
# $"Unable to find [($weather_runtime_file)], creating it(char nl)"
let weather_table = (get_weather)
let temp = ($weather_table.Temperature)
let emoji = ($weather_table.Emoji)
{
Temperature: ($temp)
Source: "initial"
Emoji: ($emoji)
}
$weather_table | upsert last_run_time {(date now | date format '%Y-%m-%d %H:%M:%S %z')} | save $weather_runtime_file
}
} else {
echo "Your command did not run because you are not on Windows..."
# ToDo: refactor the info in the Windows section into another def. The only real difference
# is where the temp file will be located. Mac & Linux probably should be in /tmp I guess.
# everything else is linux or mac
}
}
timed_weather_run --command "get_weather" --interval 1min