From 500dc3ea6e2989a6a9e8b9960a8e9d7aa44d559d Mon Sep 17 00:00:00 2001 From: moko256 Date: Fri, 7 Jan 2022 06:00:38 +0900 Subject: [PATCH] feat(elvish): last command status (#3403) --- docs/config/README.md | 4 ++-- src/init/starship.elv | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 93d32c89d..4ee5c4639 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -447,7 +447,7 @@ look at [this example](#with-custom-error-shape). ::: warning -`error_symbol` is not supported on elvish and nu shell. +`error_symbol` is not supported on nu shell. ::: @@ -2918,7 +2918,7 @@ To enable it, set `disabled` to `false` in your configuration file. ::: ::: warning -This module is not supported on elvish and nu shell. +This module is not supported on nu shell. ::: ### Options diff --git a/src/init/starship.elv b/src/init/starship.elv index d9552be84..3b0234878 100644 --- a/src/init/starship.elv +++ b/src/init/starship.elv @@ -1,17 +1,33 @@ set-env STARSHIP_SHELL "elvish" set-env STARSHIP_SESSION_KEY (::STARSHIP:: session) +# Define Hooks +var cmd-status-code = 0 + +fn starship-after-command-hook {|m| + var error = $m[error] + if (is $error $nil) { + set cmd-status-code = 0 + } else { + try { + set cmd-status-code = $error[reason][exit-status] + } except { + # The error is from the built-in commands and they have no status code. + set cmd-status-code = 1 + } + } +} + +# Install Hooks +set edit:after-command = [ $@edit:after-command $starship-after-command-hook~ ] + # Install starship set edit:prompt = { - # Note: - # Elvish does not appear to support exit status codes (--status) var cmd-duration = (printf "%.0f" (* $edit:command-duration 1000)) - ::STARSHIP:: prompt --jobs=$num-bg-jobs --cmd-duration=$cmd-duration + ::STARSHIP:: prompt --jobs=$num-bg-jobs --cmd-duration=$cmd-duration --status $cmd-status-code } set edit:rprompt = { - # Note: - # Elvish does not appear to support exit status codes (--status) var cmd-duration = (printf "%.0f" (* $edit:command-duration 1000)) - ::STARSHIP:: prompt --right --jobs=$num-bg-jobs --cmd-duration=$cmd-duration + ::STARSHIP:: prompt --right --jobs=$num-bg-jobs --cmd-duration=$cmd-duration --status $cmd-status-code }