Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve hostname colorization #475

Open
wants to merge 4 commits into
base: master
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,14 @@ The options for the `cwd` segment are:
- `full_cwd`: If true, the last directory will not be shortened when
`max_dir_size` is used.

The `hostname` segment provides one option:
The `hostname` segment provides two options:

- `colorize`: If true, the hostname will be colorized based on a hash of
itself.

- `dark`: If true (and colorize=true), a bright color will be used as the
background color

The `vcs` segment provides one option:

- `show_symbol`: If `true`, the version control system segment will start with
Expand Down
20 changes: 5 additions & 15 deletions powerline_shell/color_compliment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,11 @@


def getOppositeColor(r,g,b):
r, g, b = [x/255.0 for x in [r, g, b]] # convert to float before getting hls value
hls = rgb_to_hls(r,g,b)
opp = list(hls[:])
opp[0] = (opp[0]+0.2)%1 # shift hue (a.k.a. color)
if opp[1] > 255/2: # for level you want to make sure they
opp[1] -= 255/2 # are quite different so easily readable
else:
opp[1] += 255/2
if opp[2] > -0.5: # if saturation is low on first color increase second's
opp[2] -= 0.5
opp = hls_to_rgb(*opp)
m = max(opp)
if m > 255: #colorsys module doesn't give caps to their conversions
opp = [ x*254/m for x in opp]
return tuple([ int(x) for x in opp])
"""returns RGB components of complementary color"""
# colorsys functions expect values to be between 0 and 1
hls = rgb_to_hls(r, g, b) # r,g,b are now between 0 and 1
opp = hls_to_rgb(*[ (x+0.5)%1 for x in hls ])
return tuple([ int(x*255) for x in opp ]) # convert back to value range 0-255

def stringToHashToColorAndOpposite(string):
if py3:
Expand Down
5 changes: 5 additions & 0 deletions powerline_shell/segments/hostname.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def add_to_powerline(self):
if powerline.segment_conf("hostname", "colorize"):
hostname = gethostname()
FG, BG = stringToHashToColorAndOpposite(hostname)
# if we operate on a dark background then
# we want the brighter color to always be the
# background color
if powerline.segment_conf("hostname", "dark") and sum(FG) > sum(BG):
FG,BG = BG,FG
FG, BG = (rgb2short(*color) for color in [FG, BG])
host_prompt = " %s " % hostname.split(".")[0]
powerline.append(host_prompt, FG, BG)
Expand Down