From 3e9c9618272fa6a3d6abce803c8cf412d6fdfd56 Mon Sep 17 00:00:00 2001 From: Bahex <17417311+Bahex@users.noreply.github.com> Date: Mon, 16 Jun 2025 18:41:10 +0300 Subject: [PATCH 1/2] refactor(nu-hooks/startup-times): Better performance and UX - Use attr based examples - Hook removes itself after running, rather than staying loaded. - Switch to tsv as storage format to allow adding new logs without loading the file into memory. --- nu-hooks/nu-hooks/startup-times.nu | 73 ++++++++++++++++-------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/nu-hooks/nu-hooks/startup-times.nu b/nu-hooks/nu-hooks/startup-times.nu index f8a41b930..762a37272 100644 --- a/nu-hooks/nu-hooks/startup-times.nu +++ b/nu-hooks/nu-hooks/startup-times.nu @@ -1,39 +1,44 @@ -# 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 - } +alias startup-times = main - let version = (version) +# A hook for logging startup times +@example "Setting it up" { + $env.config.hooks.pre_prompt ++= (startup-times) +} +@example "Setting it up with a custom path" { + $env.config.hooks.pre_prompt ++= (startup-times "~/startup-times.tsv") +} +export def main [ + file: path = ($nu.data-dir | path join "startup-times.tsv"), # the file to log the startup times +]: nothing -> list { + [ + { + remove: true + code: { + let version = (version) + let times = { + 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 + } - # 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 + if not ($file | path exists) { + mkdir ($file | path dirname) + $times | to tsv | save $file + } else { + $times | to tsv --noheaders | save --append $file + } } - $startup_times | save --force $file } - } + { + # The hook removes itself, making it run just once + # NOTE: We need a separate string hook, modifying `$env.config` in + # a closure hook does not take effect + remove: true + code: '$env.config.hooks.pre_prompt = $env.config.hooks.pre_prompt | where not (try { $it.remove == true } catch { false })' + } + ] } From db40323218c21f051b4bb946d9867821b941ece7 Mon Sep 17 00:00:00 2001 From: Bahex <17417311+Bahex@users.noreply.github.com> Date: Thu, 24 Jul 2025 23:54:13 +0300 Subject: [PATCH 2/2] refactor: The command now sets up the hook by itself, rather than returning it as a record --- nu-hooks/nu-hooks/startup-times.nu | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/nu-hooks/nu-hooks/startup-times.nu b/nu-hooks/nu-hooks/startup-times.nu index 762a37272..e636b57c4 100644 --- a/nu-hooks/nu-hooks/startup-times.nu +++ b/nu-hooks/nu-hooks/startup-times.nu @@ -1,16 +1,14 @@ alias startup-times = main # A hook for logging startup times -@example "Setting it up" { - $env.config.hooks.pre_prompt ++= (startup-times) -} +@example "Setting it up is simple, calling the command adds the hook automatically" { startup-times } @example "Setting it up with a custom path" { $env.config.hooks.pre_prompt ++= (startup-times "~/startup-times.tsv") } -export def main [ +export def --env main [ file: path = ($nu.data-dir | path join "startup-times.tsv"), # the file to log the startup times -]: nothing -> list { - [ +]: nothing -> nothing { + $env.config.hooks.pre_prompt = [ { remove: true code: { @@ -31,14 +29,11 @@ export def main [ } else { $times | to tsv --noheaders | save --append $file } + + # Remove self + $env.config.hooks.pre_prompt = $env.config.hooks.pre_prompt + | where not (try { $it.remove == true } catch { false }) } } - { - # The hook removes itself, making it run just once - # NOTE: We need a separate string hook, modifying `$env.config` in - # a closure hook does not take effect - remove: true - code: '$env.config.hooks.pre_prompt = $env.config.hooks.pre_prompt | where not (try { $it.remove == true } catch { false })' - } ] }