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

Uniquely colorize hostname based on a hash` #82

Merged
merged 5 commits into from
Jul 21, 2013
Merged

Conversation

hryanjones
Copy link
Contributor

Many times I find myself sshed into 2+ computers and I sometimes will make mistakes because I don't realize which one I'm on. In order to combat this forgetfulness I wrote a new segment...

 (ideally it should probably be the same as the normal hostname.py segment, 
but I didn't see a quick way to send the module an option)

...which:

  1. gets hostname and passes it through md5sum
  2. taking the end 6 hex digits -> convert to RGB
  3. convert that to HLS and do a bit of magic to find another color which likely won't conflict
  4. lookup the correct terminal code for these two colors and set them as FG and BG

I also did a fair amount of testing (see image below) to make sure the function worked and didn't generate anything super unreadable. I'll probably be using this and committing to it as time goes on and I'd be glad to hear what you think.

Thanks,
Ryan

test_of_many_colors

@oschrenk
Copy link

Awesome idea!

@banga
Copy link
Collaborator

banga commented Jul 21, 2013

Yeah, great idea!
Have you checked the performance impact of this? I've had to turn off powerline on some machines because it was too slow. Really cool nevertheless. Can you also add a line to the readme explaining this feature and how to enable it?

banga added a commit that referenced this pull request Jul 21, 2013
Uniquely colorize hostname based on a hash`
@banga banga merged commit dbaaa09 into b-ryan:master Jul 21, 2013
@BrianEnigma
Copy link
Contributor

My Ubuntu box complains about md5 being deprecated at every prompt refresh. I worked around this by using hashlib instead:

diff --git a/lib/color_compliment.py b/lib/color_compliment.py
index 988684c..47a4dad 100755
--- a/lib/color_compliment.py
+++ b/lib/color_compliment.py
@@ -2,7 +2,7 @@

 from colortrans import *
 from colorsys import hls_to_rgb, rgb_to_hls
-from md5 import md5
+import hashlib
 from sys import argv


@@ -27,7 +27,9 @@ def getOppositeColor(r,g,b):
     return tuple([ int(x) for x in opp])

 def stringToHashToColorAndOpposite(string):
-    string = md5(string).hexdigest()[:6] # get a random color
+    m = hashlib.md5()
+    m.update(string)
+    string = m.hexdigest()[:6] # get a random color
     color1 = rgbstring2tuple(string)
     color2 = getOppositeColor(*color1)
     return color1, color2

@banga
Copy link
Collaborator

banga commented Jul 25, 2013

Do you want to send this as a pull request?

@hryanjones
Copy link
Contributor Author

Please do -- Ref. http://docs.python.org/2/library/md5.html

@hryanjones
Copy link
Contributor Author

Hi Shrey,

I hadn't noticed much performance hit so I did a couple quick runs on the
command line to test. Running the prompt 100 times successively resulted
in about the same amount of time: 12 seconds. Using time they both seemed
to run in about 120 ms. So not much of a performance hit as far as I can
tell.

I'll add something to the README shortly.

DETAILS

ekyuma-kya-okupa /mnt/sdb/CODE_stuff/git/powerline-shell  master + 
$ echo with hostname colorization enabled
with hostname colorization enabled

ekyuma-kya-okupa /mnt/sdb/CODE_stuff/git/powerline-shell  master + 
$ date;for x;for x in (seq 100)
./powerline-shell.py > /dev/null
end;date
Fri Jul 26 22:47:04 PDT 2013
Fri Jul 26 22:47:16 PDT 2013

ekyuma-kya-okupa /mnt/sdb/CODE_stuff/git/powerline-shell  master + 
$ time ./powerline-shell.py > /dev/null
0.07user 0.03system 0:00.12elapsed 80%CPU (0avgtext+0avgdata
28720maxresident)k
0inputs+0outputs (0major+6519minor)pagefaults 0swaps


ekyuma-kya-okupa /mnt/sdb/CODE_stuff/git/powerline-shell  mine + 
$ echo with hostname colorization disabled
with hostname colorization disabled

ekyuma-kya-okupa /mnt/sdb/CODE_stuff/git/powerline-shell  mine + 
$ date;for x;for x in (seq 100)
./powerline-shell.py > /dev/null
end;date
Fri Jul 26 22:48:57 PDT 2013
Fri Jul 26 22:49:09 PDT 2013

ekyuma-kya-okupa /mnt/sdb/CODE_stuff/git/powerline-shell  mine + 
$ time ./powerline-shell.py > /dev/null
0.06user 0.04system 0:00.12elapsed 83%CPU (0avgtext+0avgdata
26096maxresident)k
0inputs+0outputs (0major+6343minor)pagefaults 0swaps


(206) 414-8475
hryanjones.com

On Sun, Jul 21, 2013 at 4:12 AM, Shrey Banga notifications@gh.neting.ccwrote:

Yeah, great idea!
Have you checked the performance impact of this? I've had to turn off
powerline on some machines because it was too slow. Really cool
nevertheless. Can you also add a line to the readme explaining this feature
and how to enable it?


Reply to this email directly or view it on GitHubhttps://github.com//pull/82#issuecomment-21308454
.

@rmbl
Copy link

rmbl commented Aug 1, 2013

I really like the colored hostnames, but I like to have the powerline-shell.py script as a standalone script in my dotfiles repo and the lib module is preventing that. I think it would be better to embed these functions into the segment or use an external module.

@banga
Copy link
Collaborator

banga commented Aug 2, 2013

That's a fair point. Do you want to do this and send a pull request?

@hryanjones
Copy link
Contributor Author

I think it would be preferable to embed them in an external module, because
I can see them being useful beyond just a colorization of the hostname.
For example, I've thought about doing something like a random theme
generator that you can go through until it generates one that you like.


(206) 414-8475
hryanjones.com

On Thu, Aug 1, 2013 at 11:23 PM, Shrey Banga notifications@gh.neting.ccwrote:

That's a fair point. Do you want to do this and send a pull request?


Reply to this email directly or view it on GitHubhttps://github.com//pull/82#issuecomment-21988567
.

amtrivedi91 added a commit to amtrivedi91/powerline-shell that referenced this pull request Aug 31, 2016
Uniquely colorize hostname based on a hash`
amtrivedi91 added a commit to amtrivedi91/powerline-shell that referenced this pull request Aug 31, 2016
Uniquely colorize hostname based on a hash`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants