From 59e64c90512f4c67340e4baf37f0a22d41f765cc Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Fri, 21 Apr 2017 00:06:28 +0400 Subject: [PATCH 1/3] host and user segments --- CHANGELOG.md | 5 ++++ README.md | 48 +++++++++++++++++++++++++++++++++++ powerlevel9k.zsh-theme | 57 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cacc88e6..b84e5ac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,11 @@ A new script `debug/font-issues.zsh` was added, so that problems with your font The `ram` segment now shows the available ram instead of free. +### Add new segments `host` and `user` + +The user and host segments allow you to have different icons and colors for both the user and host segments +depending on their state. + ## v0.6.0 - Fixed a bug where the tag display was broken on detached HEADs. diff --git a/README.md b/README.md index 342792f8..2ab722bb 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ The segments that are currently available are: * `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. +* [`host`](#host) - Your current host name * [`ip`](#ip) - Shows the current IP address. * [`public_ip`](#public_ip) - Shows your public IP address. * `load` - Your machine's load averages. @@ -101,6 +102,7 @@ The segments that are currently available are: * [`status`](#status) - The return code of the previous command. * `swap` - Prints the current swap size. * [`time`](#time) - System time. +* [`user`](#user) - Your current username * [`vi_mode`](#vi_mode)- Your prompt's Vi editing mode (NORMAL|INSERT). * `ssh` - Indicates whether or not you are in an SSH session. @@ -354,6 +356,35 @@ The `disk_usage` segment will show the usage level of the partition that your cu |POWERLEVEL9K_DISK_USAGE_WARNING_LEVEL|90|The usage level that triggers a warning state.| |POWERLEVEL9K_DISK_USAGE_CRITICAL_LEVEL|95|The usage level that triggers a critical state.| +##### host + +The `host` segment (host string) will print the hostname. You can customize the `host` segment. +For example, you can make it to print the full hostname by setting +``` +POWERLEVEL9K_HOST_TEMPLATE="`hostname -f`" +``` + +You can also modify the COLOURS for the two states - LOCAL and REMOTE, by setting +``` +POWERLEVEL9K_HOST_LOCAL_BACKGROUND="green" +POWERLEVEL9K_HOST_LOCAL_FOREGROUND="white" +POWERLEVEL9K_HOST_REMOTE_BACKGROUND="red" +POWERLEVEL9K_HOST_REMOTE_FOREGROUND="yellow" +``` + +Currently, LOCAL hosts will show the host icon and remote hosts will show the SSH icon. You can override them by setting +``` +POWERLEVEL9K_HOST_ICON="\uF109 " #  +POWERLEVEL9K_SSH_ICON="\uF489 " #  +``` + +You can set the `POWERLEVEL9K_HOST_TEMPLATE` variable to change how the hostname is displayed. +See (ZSH Manual)[http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Login-information] +for details. The default is set to %m which will show the hostname up to the first ‘.’ +You can set it to %{N}m where N is an integer to show that many segments of system +hostname. Setting N to a negative integer will show that many segments from the +end of the hostname. + ##### ip This segment tries to examine all currently used network interfaces and prints @@ -436,6 +467,23 @@ segment, as well: # Output time, date, and a symbol from the "Awesome Powerline Font" set POWERLEVEL9K_TIME_FORMAT="%D{%H:%M:%S \uE868 %d.%m.%y}" ``` +##### user + +The `user` segment (user string) will print the username. You can customize the `user` segment. +For example, you can modify the COLOURS for the two states - DEFAULT and ROOT, by setting +``` +POWERLEVEL9K_USER_DEFAULT_BACKGROUND="green" +POWERLEVEL9K_USER_DEFAULT_FOREGROUND="white" +POWERLEVEL9K_USER_ROOT_BACKGROUND="red" +POWERLEVEL9K_USER_ROOT_FOREGROUND="yellow" +``` + +You can also override the icons by setting +``` +POWERLEVEL9K_USER_ICON="\uF415" #  +POWERLEVEL9K_ROOT_ICON="#" +``` + ##### vcs By default, the `vcs` segment will provide quite a bit of information. Further diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index bbd1ec44..c791a845 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -567,6 +567,61 @@ prompt_context() { "$1_prompt_segment" "${0}_${current_state}" "$2" "$DEFAULT_COLOR" "${context_states[$current_state]}" "${content}" } +################################################################ +# User: user (who am I) +# Note that if $DEFAULT_USER is not set, this prompt segment will always print +set_default POWERLEVEL9K_USER_TEMPLATE "%n" +prompt_user() { + local current_state="DEFAULT" + typeset -AH user_state + if [[ "$POWERLEVEL9K_ALWAYS_SHOW_CONTEXT" == true ]] || [[ "$USER" != "$DEFAULT_USER" ]] || [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then + if [[ $(print -P "%#") == '#' ]]; then + user_state=( + "STATE" "ROOT" + "CONTENT" "${POWERLEVEL9K_USER_TEMPLATE}" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "yellow" + "VISUAL_IDENTIFIER" "ROOT_ICON" + ) + elif [[ "$POWERLEVEL9K_ALWAYS_SHOW_USER" == true ]]; then + user_state=( + "STATE" "DEFAULT" + "CONTENT" "$USER" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "011" + "VISUAL_IDENTIFIER" "USER_ICON" + ) + fi + fi + "$1_prompt_segment" "${0}_${user_state[STATE]}" "$2" "${user_state[BACKGROUND_COLOR]}" "${user_state[FOREGROUND_COLOR]}" "${user_state[CONTENT]}" "${user_state[VISUAL_IDENTIFIER]}" +} + +################################################################ +# Host: machine (where am I) +set_default POWERLEVEL9K_HOST_TEMPLATE "%m" +prompt_host() { + local current_state="LOCAL" + typeset -AH host_state + if [[ -n "$SSH_CLIENT" ]] || [[ -n "$SSH_TTY" ]]; then + host_state=( + "STATE" "REMOTE" + "CONTENT" "${POWERLEVEL9K_HOST_TEMPLATE}" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "yellow" + "VISUAL_IDENTIFIER" "SSH_ICON" + ) + else + host_state=( + "STATE" "LOCAL" + "CONTENT" "${POWERLEVEL9K_HOST_TEMPLATE}" + "BACKGROUND_COLOR" "${DEFAULT_COLOR}" + "FOREGROUND_COLOR" "011" + "VISUAL_IDENTIFIER" "HOST_ICON" + ) + fi + "$1_prompt_segment" "$0_${host_state[STATE]}" "$2" "${host_state[BACKGROUND_COLOR]}" "${host_state[FOREGROUND_COLOR]}" "${host_state[CONTENT]}" "${host_state[VISUAL_IDENTIFIER]}" +} + # The 'custom` prompt provides a way for users to invoke commands and display # the output in a segment. prompt_custom() { @@ -664,7 +719,7 @@ prompt_dir() { 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 94ff40d2d1823f805f58759b1420e6fe15e28001 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 27 Apr 2017 22:16:20 +0400 Subject: [PATCH 2/3] Removed POWERLEVEL9K_ALWAYS_SHOW_CONTEXT --- powerlevel9k.zsh-theme | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme index c791a845..0ae046d5 100755 --- a/powerlevel9k.zsh-theme +++ b/powerlevel9k.zsh-theme @@ -574,7 +574,7 @@ set_default POWERLEVEL9K_USER_TEMPLATE "%n" prompt_user() { local current_state="DEFAULT" typeset -AH user_state - if [[ "$POWERLEVEL9K_ALWAYS_SHOW_CONTEXT" == true ]] || [[ "$USER" != "$DEFAULT_USER" ]] || [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then + if [[ "$POWERLEVEL9K_ALWAYS_SHOW_USER" == true ]] || [[ "$USER" != "$DEFAULT_USER" ]] || [[ -n "$SSH_CLIENT" || -n "$SSH_TTY" ]]; then if [[ $(print -P "%#") == '#' ]]; then user_state=( "STATE" "ROOT" @@ -583,7 +583,7 @@ prompt_user() { "FOREGROUND_COLOR" "yellow" "VISUAL_IDENTIFIER" "ROOT_ICON" ) - elif [[ "$POWERLEVEL9K_ALWAYS_SHOW_USER" == true ]]; then + else user_state=( "STATE" "DEFAULT" "CONTENT" "$USER" From 61790bf24213f1fb8b98d853e47a37a1a75d3a10 Mon Sep 17 00:00:00 2001 From: Christo Kotze Date: Thu, 27 Apr 2017 22:19:17 +0400 Subject: [PATCH 3/3] Revised User segment in README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 2ab722bb..f62511b1 100644 --- a/README.md +++ b/README.md @@ -484,6 +484,12 @@ POWERLEVEL9K_USER_ICON="\uF415" #  POWERLEVEL9K_ROOT_ICON="#" ``` +| Variable | Default Value | Description | +|----------|---------------|-------------| +|`DEFAULT_USER`|None|Username to consider a "default context" (you can also set `$USER`).| +|`POWERLEVEL9K_ALWAYS_SHOW_USER`|false|Always show the username, but conditionalize the hostname.| +|`POWERLEVEL9K_USER_TEMPLATE`|%n|Default username prompt (username). Refer to the [ZSH Documentation](http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html) for all possible expansions| + ##### vcs By default, the `vcs` segment will provide quite a bit of information. Further