Skip to content

Commit

Permalink
Add nu-complete cache helper
Browse files Browse the repository at this point in the history
  • Loading branch information
steinuil committed Aug 14, 2024
1 parent 7b2ec35 commit bcdffd7
Showing 1 changed file with 60 additions and 0 deletions.
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
}
}

0 comments on commit bcdffd7

Please sign in to comment.