From bcb0fed873221588bfd5ba101588d7f646c54ca0 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 21 Jan 2017 14:30:19 +0100 Subject: [PATCH 1/8] Avoid error if CLOBBER is not set If the tempfile already exists and CLOBBER is not set, a file exists error occurrs. This commit avoids this problem. --- powerlevel9k.zsh-theme | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..0df17de0 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -469,6 +469,11 @@ prompt_battery() { fi } +# Public IP segment +# Parameters: +# * $1 Alignment: string - left|right +# * $2 Index: integer +# * $3 Joined: bool - If the segment should be joined prompt_public_ip() { # set default values for segment set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300" @@ -531,12 +536,17 @@ prompt_public_ip() { fi # write IP to tmp file or clear tmp file if an IP was not retrieved - [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE > $POWERLEVEL9K_PUBLIC_IP_FILE + # Redirection with `>!`. From the manpage: Same as >, except that the file + # is truncated to zero length if it exists, even if CLOBBER is unset. + # If the file already exists, and a simple `>` redirection and CLOBBER + # unset, ZSH will produce an error. + [[ -n "${fresh_ip}" ]] && echo $fresh_ip >! $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE >! $POWERLEVEL9K_PUBLIC_IP_FILE fi # read public IP saved to tmp file local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) + # Draw the prompt segment if [[ -n $public_ip ]]; then $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON' fi From a576a157599ef3b41b939c4e8dc5df8656c30023 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Wed, 25 Jan 2017 23:47:20 +0100 Subject: [PATCH 2/8] Fix error with truncating package name if repo is symlinked --- powerlevel9k.zsh-theme | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..38c2ceb4 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -587,7 +587,14 @@ prompt_dir() { # Get the path of the Git repo, which should have the package.json file if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == "true" ]]; then - package_path=$(git rev-parse --show-toplevel) + # Get path from the root of the git repository to the current dir + local gitPath=$(git rev-parse --show-prefix) + # Remove trailing slash from git path, so that we can + # remove that git path from the pwd. + gitPath=${gitPath%/} + package_path=${$(pwd)%%$gitPath} + # Remove trailing slash + package_path=${package_path%/} elif [[ $(git rev-parse --is-inside-git-dir 2> /dev/null) == "true" ]]; then package_path=${$(pwd)%%/.git*} fi From 6c352c8d5c90c189166beb07e3f9f7c753f6b79d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 28 Jan 2017 01:29:38 +0100 Subject: [PATCH 3/8] Simplify `public_ip` segment --- powerlevel9k.zsh-theme | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..f1e4314b 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -475,6 +475,7 @@ prompt_public_ip() { set_default POWERLEVEL9K_PUBLIC_IP_NONE "" set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip" set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" + defined POWERLEVEL9K_PUBLIC_IP_METHODS || POWERLEVEL9K_PUBLIC_IP_METHODS=(wget curl dig) # Do we need a fresh IP? local refresh_ip=false @@ -491,51 +492,33 @@ prompt_public_ip() { fi # grab a fresh IP if needed + local fresh_ip if [[ $refresh_ip =~ true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then - # if method specified, don't use fallback methods - if [[ -n $POWERLEVEL9K_PUBLIC_IP_METHOD ]] && [[ $POWERLEVEL9K_PUBLIC_IP_METHOD =~ 'wget|curl|dig' ]]; then - local method=$POWERLEVEL9K_PUBLIC_IP_METHOD - fi - if [[ -n $method ]]; then + for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do case $method in 'dig') - if type -p dig >/dev/null; then - fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" - [[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip - fi + fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" + [[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip ;; 'curl') - if [[ -z "$fresh_ip" ]] && type -p curl >/dev/null; then - fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi + fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" ;; 'wget') - if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then - fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi + fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" ;; esac - else - if type -p dig >/dev/null; then - fresh_ip="$(dig +time=1 +tries=1 +short myip.opendns.com @resolver1.opendns.com 2> /dev/null)" - [[ "$fresh_ip" =~ ^\; ]] && unset fresh_ip + # If we found a fresh IP, break loop. + if [[ -n "${fresh_ip}" ]]; then + break; fi - - if [[ -z "$fresh_ip" ]] && type -p curl >/dev/null; then - fresh_ip="$(curl --max-time 10 -w '\n' "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi - - if [[ -z "$fresh_ip" ]] && type -p wget >/dev/null; then - fresh_ip="$(wget -T 10 -qO- "$POWERLEVEL9K_PUBLIC_IP_HOST" 2> /dev/null)" - fi - fi + done # write IP to tmp file or clear tmp file if an IP was not retrieved [[ -n $fresh_ip ]] && echo $fresh_ip > $POWERLEVEL9K_PUBLIC_IP_FILE || echo $POWERLEVEL9K_PUBLIC_IP_NONE > $POWERLEVEL9K_PUBLIC_IP_FILE fi # read public IP saved to tmp file - local public_ip=$(cat $POWERLEVEL9K_PUBLIC_IP_FILE) + local public_ip="$(cat $POWERLEVEL9K_PUBLIC_IP_FILE)" if [[ -n $public_ip ]]; then $1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" 'PUBLIC_IP_ICON' From 06cb9bb43fd6a5010fafa116fabe8290f87a90f0 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 28 Jan 2017 13:21:40 +0100 Subject: [PATCH 4/8] Change order of public ip methods as it was before --- powerlevel9k.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f1e4314b..61d9f99c 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -475,7 +475,7 @@ prompt_public_ip() { set_default POWERLEVEL9K_PUBLIC_IP_NONE "" set_default POWERLEVEL9K_PUBLIC_IP_FILE "/tmp/p9k_public_ip" set_default POWERLEVEL9K_PUBLIC_IP_HOST "http://ident.me" - defined POWERLEVEL9K_PUBLIC_IP_METHODS || POWERLEVEL9K_PUBLIC_IP_METHODS=(wget curl dig) + defined POWERLEVEL9K_PUBLIC_IP_METHODS || POWERLEVEL9K_PUBLIC_IP_METHODS=(dig curl wget) # Do we need a fresh IP? local refresh_ip=false From bfbfd5c10cc45792f260e76fb919deb26f5d5f2a Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 28 Jan 2017 13:29:08 +0100 Subject: [PATCH 5/8] Fix README entry for renamed variable POWERLEVEL9K_PUBLIC_IP_METHODS --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7a80a09..518cda0c 100644 --- a/README.md +++ b/README.md @@ -348,7 +348,7 @@ segment will not be displayed. |`POWERLEVEL9K_PUBLIC_IP_FILE`|'/tmp/p8k_public_ip'|This is the file your public IP is cached in.| |`POWERLEVEL9K_PUBLIC_IP_HOST`|'http://ident.me'|This is the default host to get your public IP.| |`POWERLEVEL9K_PUBLIC_IP_TIMEOUT`|300|The amount of time in seconds between refreshing your cached IP.| -|`POWERLEVEL9K_PUBLIC_IP_METHOD`|None|You can set this to any of 'dig', 'curl', or 'wget' to only use that method to refresh your IP.| +|`POWERLEVEL9K_PUBLIC_IP_METHODS`|(dig curl wget)| These methods in that order are used to refresh your IP.| |`POWERLEVEL9K_PUBLIC_IP_NONE`|None|The string displayed when an IP was not obtained| ##### rbenv From 52eb3136264e7ea9894a7dc150d320ad47590c3d Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sun, 29 Jan 2017 14:34:32 +0100 Subject: [PATCH 6/8] Fix theme for use with plain "prompt" command ZSH has the ability to preview and set themes directly from the `prompt` builtin. To do so, the theme must be in FPATH and named "prompt_theme_setup". --- prompt_powerlevel9k_setup | 1 + 1 file changed, 1 insertion(+) create mode 120000 prompt_powerlevel9k_setup diff --git a/prompt_powerlevel9k_setup b/prompt_powerlevel9k_setup new file mode 120000 index 00000000..311575f4 --- /dev/null +++ b/prompt_powerlevel9k_setup @@ -0,0 +1 @@ +powerlevel9k.zsh-theme \ No newline at end of file From 6927b0ca94157e2fb9dcd3b12766b4e913d0e44e Mon Sep 17 00:00:00 2001 From: Benoit Averty Date: Tue, 9 Aug 2016 11:22:02 +0200 Subject: [PATCH 7/8] Add directory permission prompt segment --- functions/icons.zsh | 3 +++ powerlevel9k.zsh-theme | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/functions/icons.zsh b/functions/icons.zsh index df92d22a..44d99095 100644 --- a/functions/icons.zsh +++ b/functions/icons.zsh @@ -78,6 +78,7 @@ case $POWERLEVEL9K_MODE in PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' PUBLIC_IP_ICON '' + LOCK_ICON $'\UE138' #  ) ;; 'awesome-fontconfig') @@ -138,6 +139,7 @@ case $POWERLEVEL9K_MODE in PYTHON_ICON $'\U1F40D' # 🐍 SWIFT_ICON '' PUBLIC_IP_ICON '' + LOCK_ICON $'\UE138' #  ) ;; *) @@ -198,6 +200,7 @@ case $POWERLEVEL9K_MODE in PYTHON_ICON '' SWIFT_ICON 'Swift' PUBLIC_IP_ICON '' + LOCK_ICON $'\UE0A2' ) ;; esac diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index f104b789..620a5f7d 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -1114,6 +1114,13 @@ prompt_swift_version() { "$1_prompt_segment" "$0" "$2" "magenta" "white" "${swift_version}" 'SWIFT_ICON' } +# dir_permision: Display information about the user's permission to write in the current directory +prompt_dir_permission() { + if [[ ! -w "$PWD" ]]; then + "$1_prompt_segment" "$0_FORBIDDEN" "$2" "red" "226" "" 'LOCK_ICON' + fi +} + ################################################################ # Prompt processing and drawing ################################################################ @@ -1240,4 +1247,3 @@ prompt_powerlevel9k_setup() { } prompt_powerlevel9k_setup "$@" - From 052eba9c74b2153dbd70eb4ba829f258a596baf2 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 31 Jan 2017 23:57:59 +0100 Subject: [PATCH 8/8] Rename `dir_permissions` segment to `dir_writable` Additionally: - Add changelog entry - Add documentation --- CHANGELOG.md | 4 ++++ README.md | 1 + powerlevel9k.zsh-theme | 4 ++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index defc1dd0..5db1f6e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ 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. +### New segment 'dir_writable' added + +This segment displays a lock icon if your user has no write permissions in the current folder. + ### New segment 'disk_usage' added This segment will show the usage level of your current partition. diff --git a/README.md b/README.md index f7a80a09..8f606f6b 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ The segments that are currently available are: * [`battery`](#battery) - Current battery status. * [`context`](#context) - Your username and host. * [`dir`](#dir) - Your current working directory. +* `dir_writable` - Displays a lock icon, if you do not have write permissions on the current folder. * [`disk_usage`](#disk_usage) - Disk usage of your current partition. * `history` - The command number for the current line. * [`ip`](#ip) - Shows the current IP address. diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index 620a5f7d..b89f390c 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -1114,8 +1114,8 @@ prompt_swift_version() { "$1_prompt_segment" "$0" "$2" "magenta" "white" "${swift_version}" 'SWIFT_ICON' } -# dir_permision: Display information about the user's permission to write in the current directory -prompt_dir_permission() { +# dir_writable: Display information about the user's permission to write in the current directory +prompt_dir_writable() { if [[ ! -w "$PWD" ]]; then "$1_prompt_segment" "$0_FORBIDDEN" "$2" "red" "226" "" 'LOCK_ICON' fi