Add facts cache

main
trivernis 10 months ago
parent ac259db7a8
commit c3f9f154b8
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,4 +1,34 @@
let fact = (try { http get https://uselessfacts.jsph.pl/api/v2/facts/random | get text } catch { "I wasn't able to provide a fact." });
echo $"Hello (ansi purple_bold)({{#if flags.windows}}$env.USERNAME{{else}}$env.USER{{/if}})(ansi reset),
def print-fact [] {
let username = {{#if flags.windows}}$env.USERNAME{{else}}$env.USER{{/if}}
echo $"Hello (ansi purple_bold)($username)(ansi reset),
it's the (ansi purple_bold)(date now | format date '%d.%m.%Y')(ansi reset) at (ansi purple_bold)(date now | format date '%H:%M:%S')(ansi reset)
Did you know: (ansi purple_bold)($fact)(ansi reset)"
Did you know: (ansi purple_bold)(fact)(ansi reset)"
}
def fact [] {
let cache_file = "{{dirs.data}}/facts.txt"
if not ($cache_file | path exists) {
touch $cache_file
}
mut all_facts = open $cache_file | lines
if ($all_facts | length) < 4 {
try {
$all_facts = (fetch-facts | prepend $all_facts)
} catch {|e|
print -e $"Failed to refresh facts cache ($e)"
}
}
$all_facts | skip 1 | str join "\n" | save -f $cache_file
if ($all_facts | is-empty) {
"I wasn't able to provide a fact."
} else {
$all_facts | first
}
}
def fetch-facts [num_facts: number = 10] {
0..($num_facts) | par-each { http get https://uselessfacts.jsph.pl/api/v2/facts/random | get text }
}
print-fact

Loading…
Cancel
Save