Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Enhancement] adds ...PROJECT_ONLY option to node_version.p9k #1219

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions segments/node_version/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ where you want to show this segment.

## Configuration

### Segment Display

By default, this segment is always shown. You can also choose to have it
displayed only when inside of a Node project by setting
```
P9K_NODE_VERSION_PROJECT_ONLY=true
```
The current directory and its ancestors will be searched for a `project.json`
file, and the segment will only be displayed if one is located before `/`.

### Color Customization

You can change the foreground and background color of this segment by setting
Expand Down
30 changes: 27 additions & 3 deletions segments/node_version/node_version.p9k
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,33 @@
# $2 integer Segment index
# $3 boolean Whether the segment should be joined
##
p9k::set_default P9K_NODE_VERSION_PROJECT_ONLY=false
prompt_node_version() {
local node_version=$(node -v 2>/dev/null)
[[ -z "${node_version}" ]] && return
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" = true ] ; then
local foundProject=false # Variable to stop searching if a project is found
local currentDir=$(pwd) # Variable to iterate through the path ancestry tree

p9k::prepare_segment "$0" "" $1 "$2" $3 "${node_version:1}"
# Search as long as no project could been found or until the root directory
# has been reached
while [ "$foundProject" = false -a ! "$currentDir" = "/" ] ; do
# Check if directory contains a project description
if [[ -e "$currentDir/package.json" ]] ; then
foundProject=true
break
fi
# Go to the parent directory
currentDir="$(dirname "$currentDir")"
done
fi

# Show version if a project has been found, or set to always show
if [ "$P9K_NODE_VERSION_PROJECT_ONLY" != true -o "$foundProject" = true ] ; then
# Get the node version
local node_version=$(node -v 2>/dev/null)

# Return if node is not installed
[[ -z "${node_version}" ]] && return

p9k::prepare_segment "$0" "" $1 "$2" $3 "${node_version:1}"
fi
}
80 changes: 65 additions & 15 deletions segments/node_version/node_version.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,86 @@
setopt shwordsplit
SHUNIT_PARENT=$0

function setUp() {
function oneTimeSetUp() {
export TERM="xterm-256color"
local -a P9K_RIGHT_PROMPT_ELEMENTS
P9K_RIGHT_PROMPT_ELEMENTS=()

OLD_DIR="${PWD}"

TEST_BASE_FOLDER="/tmp/powerlevel9k-test"
NODE_VERSION_TEST_FOLDER="${TEST_BASE_FOLDER}/node_version-test"

OLDPATH="${PATH}"
PATH="${NODE_VERSION_TEST_FOLDER}:${PATH}"

P9K_CUSTOM_WORLD='echo world'

node() {
echo "v1.2.3"
}
}

function setUp() {
# Load Powerlevel9k
source powerlevel9k.zsh-theme
source segments/node_version/node_version.p9k
source "${OLD_DIR}/powerlevel9k.zsh-theme"
source "${OLD_DIR}/segments/node_version/node_version.p9k"

P9K_LEFT_PROMPT_ELEMENTS=(node_version)
P9K_RIGHT_PROMPT_ELEMENTS=()

mkdir -p "${NODE_VERSION_TEST_FOLDER}"
cd "${NODE_VERSION_TEST_FOLDER}"
}

function tearDown() {
cd "${OLD_DIR}"
rm -rf "${NODE_VERSION_TEST_FOLDER}"
}

function oneTimeTearDown() {
PATH="${OLDPATH}"

cd "${OLD_DIR}"
rm -rf "${TEST_BASE_FOLDER}"
}

function testNodeVersionSegmentWorks() {
assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
}

function testNodeVersionSegmentPrintsNothingWithoutNode() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(node_version custom_world)
local P9K_CUSTOM_WORLD='echo world'
P9K_LEFT_PROMPT_ELEMENTS+=(custom_world)

alias node="nonode 2>/dev/null"

assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(__p9k_build_left_prompt)"

unalias node
}

function testNodeVersionSegmentWorks() {
local -a P9K_LEFT_PROMPT_ELEMENTS
P9K_LEFT_PROMPT_ELEMENTS=(node_version)
node() {
echo "v1.2.3"
}
function testNodeVersionSegmentProjectOnlyPrintsNothingOutsideProjectDirectory() {
P9K_LEFT_PROMPT_ELEMENTS+=(custom_world)

P9K_NODE_VERSION_PROJECT_ONLY=true

assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(__p9k_build_left_prompt)"
}

function testNodeVersionSegmentProjectOnlyWorksInsideProjectDirectory() {
P9K_NODE_VERSION_PROJECT_ONLY=true

touch ./package.json

assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
}

function testNodeVersionSegmentProjectOnlyWorksInsideProjectChildDirectory() {
P9K_NODE_VERSION_PROJECT_ONLY=true

unfunction node
touch ./package.json
mkdir test-child
cd test-child

assertEquals "%K{002} %F{015}⬢ %F{015}1.2.3 %k%F{002}%f " "$(__p9k_build_left_prompt)"
}

source shunit2/shunit2