From 1fb482ec31daa64f9087f119c3c67017be1c5722 Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Wed, 3 Jul 2024 22:05:52 +0200 Subject: [PATCH] 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 :relieved: ## 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 :wink: 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: --- nu-hooks/nu-hooks/startup-times.nu | 39 ++++++++++++++++++++++++++++++ nu-hooks/nu-hooks/toolkit.nu | 23 ++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 nu-hooks/nu-hooks/startup-times.nu create mode 100644 nu-hooks/nu-hooks/toolkit.nu diff --git a/nu-hooks/nu-hooks/startup-times.nu b/nu-hooks/nu-hooks/startup-times.nu new file mode 100644 index 0000000..f8a41b9 --- /dev/null +++ b/nu-hooks/nu-hooks/startup-times.nu @@ -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 + } + } +} diff --git a/nu-hooks/nu-hooks/toolkit.nu b/nu-hooks/nu-hooks/toolkit.nu new file mode 100644 index 0000000..71eb16a --- /dev/null +++ b/nu-hooks/nu-hooks/toolkit.nu @@ -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: {|_, 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) + " + } +}