diff --git a/README.md b/README.md index 760f7a12f..6e6f15cb5 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ The segments that are currently available are: * [`user`](segments/user/README.md) - Your current username * [`vi_mode`](segments/vi_mode/README.md)- Your prompt's Vi editing mode (NORMAL|INSERT). * [`ssh`](segments/ssh/README.md) - Indicates whether or not you are in an SSH session. +* [`variable`](segments/variable/README.md) - Displays the value of a given variable. **Development Environment Segments:** * [`vcs`](segments/vcs/README.md) - Information about this `git` or `hg` repository (if you are in one). diff --git a/segments/variable/README.md b/segments/variable/README.md new file mode 100644 index 000000000..7846849ab --- /dev/null +++ b/segments/variable/README.md @@ -0,0 +1,27 @@ +# Variable + +## Installation + +To use this segment, you need to activate it by adding `variable` to your +`P9K_LEFT_PROMPT_ELEMENTS` or `P9K_RIGHT_PROMPT_ELEMENTS` array, depending +where you want to show this segment. + +## Configuration + +| Variable | Default Value | Description | +|----------|---------------|-------------| +|`P9K_VARIABLE_NAME`|`LANG`|Change this to set the variable to read from| +|`P9K_VARIABLE_SHOW_NAME`|`false`|Set to true if you wish to show the name of the variable| + +### Color Customization + +You can change the foreground and background color of this segment by setting +``` +P9K_VARIABLE_FOREGROUND='red' +P9K_VARIABLE_BACKGROUND='blue' +``` + +### Customize Icon + +The main Icon can be changed by setting `P9K_VARIABLE_ICON="my_icon"`. To change the +icon color only, set `P9K_VARIABLE_ICON_COLOR="red"`. diff --git a/segments/variable/variable.p9k b/segments/variable/variable.p9k new file mode 100644 index 000000000..c143b06f4 --- /dev/null +++ b/segments/variable/variable.p9k @@ -0,0 +1,39 @@ +# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8 +################################################################ +# @title powerlevel9k Segment - Variable +# @source [powerlevel9k](https://github.com/bhilburn/powerlevel9k) +## + +(){ + # Set the right locale to protect special characters + local LC_ALL="" LC_CTYPE="en_US.UTF-8" + ################################################################ + # Register segment + # Parameters: + # segment_name context background foreground Generic Flat/Awesome-Patched Awesome-FontConfig Awesome-Mapped-FontConfig NerdFont + # + p9k::register_segment "VARIABLE" "" "${DEFAULT_COLOR_INVERTED}" "${DEFAULT_COLOR}" '' '' '' '' '' + + p9k::set_default P9K_VARIABLE_NAME "LANG" + p9k::set_default P9K_VARIABLE_SHOW_NAME false +} + +################################################################ +# @description +# Outputs the content of a given variable +## +# @args +# $1 string Alignment - left | right +# $2 integer Segment index +# $3 boolean Whether the segment should be joined +## +prompt_variable() { + local variableContent="$(eval echo \$$P9K_VARIABLE_NAME)" + if [[ -n "$variableContent" ]]; then + if [[ "${P9K_VARIABLE_SHOW_NAME}" == "true" ]]; then + p9k::prepare_segment "$0" "" $1 "$2" $3 "${P9K_VARIABLE_NAME}: ${variableContent}" + else + p9k::prepare_segment "$0" "" $1 "$2" $3 "${variableContent}" + fi + fi +}