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

[New Segment]: variable segment #1200

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
27 changes: 27 additions & 0 deletions segments/variable/README.md
Original file line number Diff line number Diff line change
@@ -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"`.
39 changes: 39 additions & 0 deletions segments/variable/variable.p9k
Original file line number Diff line number Diff line change
@@ -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
}