export def add [ name: string, # name of the item --comment: string, # comment for the item --sources: list, # a source to acquire the item, typically a URL --importance: int # a number to indicate the importance, choose the scaling yourself, default: 0 ] { let item_id = random uuid { id: $item_id, name: $name, comment: ($comment | default ''), acquired: false, importance: ($importance | default 0) } | into sqlite $env.WISHLIST_DB_PATH print $"Added ($name) successfully to the wishlist." if $sources != null and ($sources | length) > 0 { $sources | each { |source| let link_id = random uuid { id: (random uuid), item_id: $item_id, content: $source } | into sqlite $env.WISHLIST_DB_PATH -t sources } print $"Added sources for ($name) successfully." } show --id $item_id } export def show [ --id: string, # id of the item, identify using `wishlist show --explore` --explore # show in nushell explorer ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist is empty. If it shouldn't be empty, check the WISHLIST_DB_PATH environment variable." return } mut wishlist = open $env.WISHLIST_DB_PATH | get main if $id != null { $wishlist = $wishlist | where id == $id } if ($explore) { return ($wishlist | explore -p) } return $wishlist } export def set-comment [ id: string, # id of the item, identify using `wishlist show --explore` comment: string ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } open $env.WISHLIST_DB_PATH | query db "UPDATE main SET comment = ? WHERE id = ?" -p [$comment, $id] print $"Updated comment for item with id ($id) successfully." show --id $id } export def set-importance [ id: string, # id of the item, identify using `wishlist show --explore` importance: int ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } open $env.WISHLIST_DB_PATH | query db "UPDATE main SET importance = ? WHERE id = ?" -p [$importance, $id] print $"Updated importance for item with id ($id) successfully." show --id $id } export def check [ id: string, # id of the item, identify using `wishlist show --explore` ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } open $env.WISHLIST_DB_PATH | query db "UPDATE main SET acquired = true WHERE id = ?" -p [$id] print $"Checked off item with id ($id) successfully." show --id $id } export def remove [ id: string, # id of the item, identify using `wishlist show --explore` ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } open $env.WISHLIST_DB_PATH | query db "DELETE FROM main WHERE id = ?" -p [$id] print $"Removed item with id ($id) successfully." show } # Get the highest value of importance in the wishlist export def highest-importance [] { show | get importance | reduce { |it, acc| if ($it > $acc) { $it } else { $acc }} } # Get the lowest value of importance in the wishlist export def lowest-importance [] { show | get importance | reduce { |it, acc| if ($it < $acc) { $it } else { $acc }} } export def "sources show" [ --id: string, # id of the source, identify using `wishlist sources show --explore` --item-id: string, # id of the item, identify using `wishlist show --explore` --explore # show in nushell explorer ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } mut sources = open $env.WISHLIST_DB_PATH | get sources if $id != null { $sources = $sources | where id == $id } else if $item_id != null { $sources = $sources | where item_id == $item_id } if ($explore) { return ($sources | explore -p) } return $sources } export def "sources add" [ content: string # content of the source to acquire the item, typically a URL item_id: string, # id of the item, identify using `wishlist show --explore` ] { let source_id = random uuid { id: $source_id, item_id: $item_id, content: $content, } | into sqlite $env.WISHLIST_DB_PATH -t sources print $"Added new source to item with id ($item_id) successfully." sources show --id $source_id } export def "sources update" [ id: string # id of the source, identify using `wishlist sources show --explore` content: string # new content for the given source ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } open $env.WISHLIST_DB_PATH | query db "UPDATE sources SET content = ? WHERE id = ?" -p [$content, $id] print $"Updated source with id ($id) successfully." sources show --id $id } export def "sources remove" [ id: string, # id of the source, identify using `wishlist sources show --explore` ] { if not ($env.WISHLIST_DB_PATH | path exists) { print "Wishlist database was not found. If this shouldn't happen, check the WISHLIST_DB_PATH environment variable." return } open $env.WISHLIST_DB_PATH | query db "DELETE FROM sources WHERE id = ?" -p [$id] print $"Removed source with id ($id) successfully." sources show }