Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nu-complete cache helper #931

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions custom-completions/cache/nu-complete-cache.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def db [] {
[$env.XDG_CACHE_HOME "nu-complete.sqlite"] | path join
}

def init [] {
if (db | path exists) {
stor import --file-name (db)
} else {
(stor create
--table-name "nu_complete"
--columns {
name: str
value: str
timestamp: datetime
}
)
}
}

export def "nu-complete cache" [
--expire: duration
cmd: any
] {
init

let name = view source $cmd

let saved = stor open
| (query db
"SELECT value, timestamp FROM nu_complete WHERE name = :name"
-p { name: $name })
| get -i 0

if $saved == null or (($saved.timestamp | into datetime) + $expire < (date now)) {
let output = do $cmd

if $saved == null {
{ name: $name, value: ($output | to nuon -r), timestamp: (date now) }
| stor insert -t "nu_complete"
} else {
stor open
| query db "UPDATE nu_complete SET value = :value, timestamp = :timestamp WHERE name = :name" -p {
value: ($output | to nuon -r),
timestamp: (date now),
name: $name
}
}

rm -f (db)
stor export --file-name (db)

$output
} else {
rm -f (db)
stor export --file-name (db)

$saved.value | from nuon
}
}