Skip to content

Commit

Permalink
support before and env_highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
d2iq-shadowbq committed Jan 22, 2020
1 parent 96823f9 commit 9bad1cc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
59 changes: 46 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
[![Supported Python Versions](https://img.shields.io/pypi/pyversions/powerline-inject.svg)](https://pypi.python.org/pypi/powerline-inject/)
[![License](https://img.shields.io/pypi/l/powerline-inject.svg)](https://pypi.python.org/pypi/powerline-inject/)

powerline-inject is a [Powerline](https://github.com/powerline/powerline) segment that shows injections
- the list of envs but with some **extra knobs**.
`powerline-inject` is a [Powerline](https://github.com/powerline/powerline) status segment addon for showing ENV with extra knobs.

This can be natively done with `powerline.segments.common.env.environment` if knobs aren't needed:

```json
{
"segments":
"segments":
{
"left":
"left":
[
{ "function":
{ "function":
"powerline.segments.common.env.environment",
"name": "aws",
"priority": 20,
Expand All @@ -29,14 +28,15 @@ This can be natively done with `powerline.segments.common.env.environment` if kn
}
```

Two other nifty features that it has are:
Couple knobs featured that `powerline-inject` has are:

1. The ability to toggle on or off the powerline-inject segment using an environment variable which can easily be mapped to a function in your `~/.bash_profile`.
2. The ability to run additional commands prior to loading
2. The ability to show only a symbol when the variables holds value. (SECRET KEYS, TOKENS, etc.)
3. Multiple different highlighter profiles for your ENV lists.

The screenshot below demonstrates this functionality:

<img src="usage_screenshot.png" width="800">
[![screenshot](usage_screenshot.png)](https://pypi.org/project/powerline-inject/)

## Installation

Expand All @@ -62,8 +62,10 @@ Within our user config, we'll need to add the powerline-inject segment to our sh
{
"function": "powerline_inject.context",
"priority": 30,
"args": {"show_env": true,
"env_list": ["AWS_PROFILE", "TOKEN_X"]}
"args": {
"show_env": true,
"env_list": ["AWS_PROFILE", "TOKEN_X"]
}
}
```

Expand All @@ -73,13 +75,13 @@ Next we'll add the highlighting colors we'll use to our `~/.config/powerline/col
{
"name": "Default",
"groups": {
"powerline_inject": { "fg": "white", "bg": "brightred", "attrs": [] },
"powerline_inject": { "fg": "white", "bg": "red", "attrs": [] },
"powerline_inject_bold": { "fg": "white", "bg": "brightred", "attrs": [] },
}
}
```


4. You may need to reload powerline with `powerline-daemon --replace` to load the new settings. That's it!
4. You will need to reload powerline with `powerline-daemon --replace` to load the new settings. That's it!

5. (Optional) By default powerline-inject will render the environment variable if `RENDER_POWERLINE_INJECTS` is either set to `YES` or is not set at all. Rather than setting this variable manually, you can create a simple `powerline-inject-toggle` function by placing the following in your `~/.bash_profile`:

Expand All @@ -93,6 +95,37 @@ Next we'll add the highlighting colors we'll use to our `~/.config/powerline/col
}
```

## Confidential ENV use

You may find you want to know when you have **SECRETS** loaded into your **ENVIRONMENT**. This will show only a symbol `` when the `SECRET_TOKEN` or `AWS_PROFILE` is loaded.

```json
{
"function": "powerline_inject.context",
"priority": 30,
"args": {
"show_env": false,
"env_list": ["SECRET_TOKEN", "AWS_PROFILE"],
"before": ""
}
},
```

You can further add a second call to `powerline_inject.context` with a different `before` symbol and/or `env_highlighter` in args like the `powerline_inject_bold` defined above.

```json
{
"function": "powerline_inject.context",
"priority": 30,
"name": "second_injection",
"args": {
"show_env": false,
"env_list": ["SECRET_TOKEN", "AWS_PROFILE"],
"env_highlighter": "powerline_inject_bold"
}
},
```

## Used with `PROMPT_COMMAND` for ENV refresh

You may want to use this with a PROMPT COMMAND that updates the ENVs being checked everytime. As an example `aws-test-authentication` sets `AWS_EPOCH` and `AWS_PROFILE`.
Expand Down
48 changes: 23 additions & 25 deletions powerline_inject/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from powerline.theme import requires_segment_info
from pathlib import Path

_BEGINNING_OF_LIST=0

_BEGINNING_OF_LIST = 0
_INJ_SYMBOL = u'\U0001F489 '
highlight_group_default = 'powerline_inject'

@requires_segment_info
def context(pl, segment_info, show_env=True, env_list=()):
def context(pl, segment_info, before=_INJ_SYMBOL, show_env=True, env_list=(), env_highlighter=highlight_group_default):
'''
Return the current items of envs as separate segments. Uses the 'powerline_inject'
Return the current items of ENV as separate segments. Uses the 'powerline_inject'
highlight groups.
By default powerline-inject will render the environment variable if `RENDER_POWERLINE_INJECTS` is
Expand All @@ -29,32 +32,27 @@ def context(pl, segment_info, show_env=True, env_list=()):

render_injects = segment_info['environ'].get('RENDER_POWERLINE_INJECTS', 'YES')
if render_injects != 'YES':
Path('/var/tmp/RI_NOT_YES').touch()
return []

segments_list = []
highlight_group_default = 'powerline_inject'

env_has_value = False

for item in env_list:
highlight_group = env_highlighter
content = segment_info['environ'].get(item)
if content:
env_has_value = True
if show_env:
segments_list.append( {'contents': content + " ",'highlight_groups': [highlight_group] })

if show_env:
for item in env_list:
highlight_group = 'powerline_inject'
segments_list.append(
{'contents': segment_info['environ'].get(item) + " ",
'highlight_groups': [highlight_group],
}
)
first_highlight_group = segments_list[0]['highlight_groups']
else:
first_highlight_group = env_highlighter

# Add the inject symbol UNICODE :syringe: before the first segment
first_highlight_group = segments_list[0]['highlight_groups']

if not first_highlight_group:
first_highlight_group = highlight_group_default

segments_list.insert(
_BEGINNING_OF_LIST,
{
'contents': _INJ_SYMBOL,
'highlight_groups': first_highlight_group
}
)

if env_has_value:
segments_list.insert(_BEGINNING_OF_LIST, { 'contents': before, 'highlight_groups': [first_highlight_group] })

return segments_list
2 changes: 1 addition & 1 deletion version.meta
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.5
0.1.6

0 comments on commit 9bad1cc

Please sign in to comment.