https://github.com/ypares/rescope.nu
Scoped and exception-safe resources for Nushell
https://github.com/ypares/rescope.nu
defer deferred deferred-execution nu nushell resources scoped
Last synced: 8 months ago
JSON representation
Scoped and exception-safe resources for Nushell
- Host: GitHub
- URL: https://github.com/ypares/rescope.nu
- Owner: YPares
- Created: 2025-06-11T08:53:45.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-06-13T07:48:29.000Z (8 months ago)
- Last Synced: 2025-06-29T07:11:33.240Z (8 months ago)
- Topics: defer, deferred, deferred-execution, nu, nushell, resources, scoped
- Language: Nushell
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rescope.nu
Scoped resources and deferred closure executions for Nushell.
Experimental.
## Example
(You can run it from this folder with `nu example.nu`)
```nu
use rescope.nu *
use std log
log set-level 0 # activate debug logs
rescope {|sc1|
# We open a temp folder whose finalization (removal)
# is scheduled at the end of this scope (closure):
let path = mkscoped file -s $sc1 { mktemp --directory }
# We schedule to print "Bye!" at the end of this closure:
"Bye!" | defer -s $sc1 { print $in }
rescope {|sc2|
# Here, no `-s` is given: finalizers are by defaut attached to the
# innermost scope. So here, sc2:
let job1 = mkscoped job { sleep 10sec }
# But we can also target the outer scope:
let job2 = mkscoped job -s $sc1 { sleep 10sec }
} # End of sc2: $job1 gets killed
} # End of sc1: $job2 gets killed, then "Bye!" is printed, then $path gets deleted
# (Resources are always finalized in the order inverse to their creation)
```