From a0aa5ccbfcabda100249f710de9297ffa1444fbb Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Thu, 23 Feb 2017 23:30:55 +0100 Subject: [PATCH 1/3] Fix parsing package name from complex package.json files when using `truncate_with_package_name` strategy. --- powerlevel9k.zsh-theme | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 5fab0190..b6fef65a 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -655,12 +655,15 @@ prompt_dir() { subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path - if name=$( cat "$package_path/package.json" 2> /dev/null | grep -m 1 "\"name\""); then - name=$(echo $name | awk -F ':' '{print $2}' | awk -F '"' '{print $2}') - + local pkgFile="${package_path}/package.json" + local packageName=$(jq '.name' ${pkgFile} 2> /dev/null \ + || node -e 'console.log(require(process.argv[1]).name);' ${pkgFile} 2>/dev/null \ + || cat "${pkgFile}" 2> /dev/null | grep -m 1 "\"name\"" | awk -F ':' '{print $2}' | awk -F '"' '{print $2}' 2>/dev/null \ + ) + if [[ -n "${packageName}" ]]; then # Instead of printing out the full path, print out the name of the package # from the package.json and append the current subdirectory - current_path="`echo $name | tr -d '"'`$subdirectory_path" + current_path="`echo $packageName | tr -d '"'`$subdirectory_path" else current_path=$(truncatePathFromRight "$(pwd | sed -e "s,^$HOME,~,")" ) fi From ec044c028ae0780a1579c22c72960411c6b59458 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 24 Feb 2017 01:24:16 +0100 Subject: [PATCH 2/3] Add functionality to truncate via `composer.json` --- README.md | 2 +- powerlevel9k.zsh-theme | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4440ecf6..d2f60dc0 100644 --- a/README.md +++ b/README.md @@ -312,7 +312,7 @@ Customizations available are: |Default|Truncate whole directories from left. How many is defined by `POWERLEVEL9K_SHORTEN_DIR_LENGTH`| |`truncate_middle`|Truncates the middle part of a folder. E.g. you are in a folder named "~/MySuperProjects/AwesomeFiles/BoringOffice", then it will truncated to "~/MyS..cts/Awe..les/BoringOffice", if `POWERLEVEL9K_SHORTEN_DIR_LENGTH=3` is also set (controls the amount of characters to be left).| |`truncate_from_right`|Just leaves the beginning of a folder name untouched. E.g. your folders will be truncated like so: "/ro../Pr../office". How many characters will be untouched is controlled by `POWERLEVEL9K_SHORTEN_DIR_LENGTH`.| -|`truncate_with_package_name`|Use the `package.json` `name` field to abbreviate the directory path.| +|`truncate_with_package_name`|Search for a `package.json` or `composer.json` and prints the `name` field to abbreviate the directory path. The precedence and/or files could be set by `POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json)`| |`truncate_with_folder_marker`|Search for a file that is specified by `POWERLEVEL9K_SHORTEN_FOLDER_MARKER` and truncate everything before that (if found, otherwise stop on $HOME and ROOT).| For example, if you wanted the truncation behavior of the `fish` shell, which diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index b6fef65a..b585911f 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -655,7 +655,16 @@ prompt_dir() { subdirectory_path=$(truncatePathFromRight "${current_dir:${#${(S%%)package_path//$~zero/}}}") # Parse the 'name' from the package.json; if there are any problems, just # print the file path - local pkgFile="${package_path}/package.json" + defined POWERLEVEL9K_DIR_PACKAGE_FILES || POWERLEVEL9K_DIR_PACKAGE_FILES=(package.json composer.json) + + local pkgFile="unknown" + for file in "${POWERLEVEL9K_DIR_PACKAGE_FILES[@]}"; do + if [[ -f "${package_path}/${file}" ]]; then + pkgFile="${package_path}/${file}" + break; + fi + done + local packageName=$(jq '.name' ${pkgFile} 2> /dev/null \ || node -e 'console.log(require(process.argv[1]).name);' ${pkgFile} 2>/dev/null \ || cat "${pkgFile}" 2> /dev/null | grep -m 1 "\"name\"" | awk -F ':' '{print $2}' | awk -F '"' '{print $2}' 2>/dev/null \ From ecd180a285ab5c594dd143674b2c49baf10002da Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 24 Feb 2017 01:38:58 +0100 Subject: [PATCH 3/3] Add changelog entry for truncate with composer.json --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b27a514..d5e9932d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,11 @@ Added an option to configure the path separator. If you want something else than an ordinary slash, you could set `POWERLEVEL9K_DIR_PATH_SEPARATOR` to whatever you want. +#### `truncate_with_package_name` now searches for `composer.json` as well + +Now `composer.json` files are searched as well. By default `package.json` still takes +precedence. If you want to change that, set `POWERLEVEL9K_DIR_PACKAGE_FILES=(composer.json package.json)`. + ### New segment 'command_execution_time' added Shows the duration a command needed to run. By default only durations over 3 seconds