diff --git a/README.md b/README.md index 6b516c12a..9fbbbd407 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ The segments that are currently available are: * **GoLang Segments:** * `go_version` - Show the current GO version. * **Javascript / Node.js Segments:** - * `node_version` - Show the version number of the installed Node.js. + * [`node_version`](#node_version) - Show the version number of the installed Node.js. * `nodeenv` - [nodeenv](https://github.com/ekalinin/nodeenv) prompt for displaying node version and environment name. * `nvm` - Show the version of Node that is currently active, if it differs from the version used by NVM * **PHP Segments:** @@ -643,6 +643,14 @@ you are using the [ZSH Line Editor](http://zsh.sourceforge.net/Doc/Release/Zsh-L To hide the segment entirely when in `INSERT` mode, set `POWERLEVEL9K_VI_INSERT_MODE_STRING=''` +##### node_version + +This segment shows the _NodeJS_ version, that is currently active. + +| Variable | Default Value | Description | +|----------|---------------|-------------| +|`POWERLEVEL9K_NODE_VERSION_PROJECT_ONLY`|`false`|If `true` this segment only appears inside _NodeJS_ projects described by a `package.json`.| + #### Unit Test Ratios The `symfony2_tests` and `rspec_stats` segments both show a ratio of "real" diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c8ab34bde..72075218c 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -1043,7 +1043,6 @@ prompt_load() { # Replace comma load_avg=${load_avg//,/.} - if [[ "$load_avg" -gt $((${cores} * 0.7)) ]]; then current_state="critical" elif [[ "$load_avg" -gt $((${cores} * 0.5)) ]]; then @@ -1055,13 +1054,37 @@ prompt_load() { "$1_prompt_segment" "${0}_${current_state}" "$2" "${load_states[$current_state]}" "$DEFAULT_COLOR" "$load_avg" 'LOAD_ICON' } -################################################################ -# Segment to diplay Node version +# Node version +set_default POWERLEVEL9K_NODE_VERSION_PROJECT_ONLY=false + prompt_node_version() { - local node_version=$(node -v 2>/dev/null) - [[ -z "${node_version}" ]] && return + if [ "$POWERLEVEL9K_NODE_VERSION_PROJECT_ONLY" = true ] ; then + local foundProject=false # Variable to stop searching if a project has been found. + local currentDir=$(pwd) # Variable to iterate trough the path reverse. + + # Search as long as no project could been found or the root directory has been reached. + while [ "$foundProject" = false -a ! "$currentDir" = "/" ] ; do + # Check if directory contain a project description. + if [[ -a "$currentDir/package.json" ]] ; then + foundProject=true + fi + + # Go to the parent directory. + currentDir="$(dirname "$currentDir")" + done + fi + + # Show version if it should been shown always or if a project has been found. + if [ "$POWERLEVEL9K_NODE_VERSION_PROJECT_ONLY" = false -o "$foundProject" = true ] ; then + # Get the node version. + local node_version=$(node -v 2>/dev/null) - "$1_prompt_segment" "$0" "$2" "green" "white" "${node_version:1}" 'NODE_ICON' + # Return if node is not installed (version output empty). + [[ -z "${node_version}" ]] && return + + # Define the segments itself. + "$1_prompt_segment" "$0" "$2" "green" "white" "${node_version:1}" 'NODE_ICON' + fi } ################################################################