mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-07-31 14:17:45 +00:00
add "toolkit" and "startup times" hooks (#888)
i wanted to share some of my hooks, as i don't see equivalent things in the `nu-hooks` package 😌 ## new hooks - `toolkit.nu`: creates a hook that will activate any `toolkit.nu` - `startup-times.nu`: creates a hook that will log the startup times in a log file ## review i think the easiest is to read the docstring and arguments of the two `setup` commands i've added in the two modules defined above 😉 cc @fdncred, i might have missed the latest and hotest features of the "startup times" hook, please tell me if something should be added 😇
This commit is contained in:
parent
1f3dc8b98d
commit
1fb482ec31
2 changed files with 62 additions and 0 deletions
39
nu-hooks/nu-hooks/startup-times.nu
Normal file
39
nu-hooks/nu-hooks/startup-times.nu
Normal file
|
@ -0,0 +1,39 @@
|
|||
# setup a hook that will log startup times
|
||||
#
|
||||
# # Example
|
||||
# ```nushell
|
||||
# $env.config.hooks.env_change.PWD = (
|
||||
# $env.config.hooks.env_change.PWD | append (
|
||||
# use nu-hooks/startup-times.nu;
|
||||
# startup-times setup
|
||||
# )
|
||||
# )
|
||||
# ```
|
||||
export def setup [
|
||||
dir: path = $nu.data-dir, # the path where to store the "startup times" file
|
||||
]: [ nothing -> closure ] {
|
||||
{|before, _|
|
||||
if $before == null {
|
||||
let file = $dir | path join "startup-times.nuon"
|
||||
if not ($file | path exists) {
|
||||
mkdir ($file | path dirname)
|
||||
touch $file
|
||||
}
|
||||
|
||||
let version = (version)
|
||||
|
||||
# NOTE: this binding is required as per
|
||||
# https://github.com/nushell/nushell/pull/12601#issuecomment-2069167555
|
||||
let startup_times = open $file | append {
|
||||
date: (date now)
|
||||
time: $nu.startup-time
|
||||
build: $version.build_rust_channel
|
||||
allocator: $version.allocator
|
||||
version: $version.version
|
||||
commit: $version.commit_hash
|
||||
build_time: $version.build_time
|
||||
}
|
||||
$startup_times | save --force $file
|
||||
}
|
||||
}
|
||||
}
|
23
nu-hooks/nu-hooks/toolkit.nu
Normal file
23
nu-hooks/nu-hooks/toolkit.nu
Normal file
|
@ -0,0 +1,23 @@
|
|||
# setup a hook that will activate `toolkit.nu` when found in a directory
|
||||
#
|
||||
# # Example
|
||||
# ```nushell
|
||||
# $env.config.hooks.env_change.PWD = (
|
||||
# $env.config.hooks.env_change.PWD | append (
|
||||
# use nu-hooks/toolkit.nu;
|
||||
# toolkit setup --name "tk" --color "yellow_bold"
|
||||
# )
|
||||
# )
|
||||
# ```
|
||||
export def setup [
|
||||
--name: string = "toolkit", # the name of the overlay, i.e. the command that will be usable
|
||||
--color: string = "yellow_bold", # the color of the "hook" indicator
|
||||
]: [ nothing -> record<condition: closure, code: string> ] {
|
||||
{
|
||||
condition: {|_, after| $after | path join 'toolkit.nu' | path exists }
|
||||
code: $"
|
||||
print $'[\(ansi ($color)\)nu-hooks toolkit\(ansi reset\)] loading \(ansi purple\)toolkit.nu\(ansi reset\) as an overlay'
|
||||
overlay use --prefix toolkit.nu as ($name)
|
||||
"
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue