Skip to content

Commit

Permalink
add "toolkit" and "startup times" hooks (#888)
Browse files Browse the repository at this point in the history
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
:innocent:
  • Loading branch information
amtoine authored Jul 3, 2024
1 parent 1f3dc8b commit 1fb482e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
39 changes: 39 additions & 0 deletions nu-hooks/nu-hooks/startup-times.nu
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions nu-hooks/nu-hooks/toolkit.nu
Original file line number Diff line number Diff line change
@@ -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)
"
}
}

0 comments on commit 1fb482e

Please sign in to comment.