Skip to content

Instantly share code, notes, and snippets.

@Lokaltog
Created May 21, 2013 08:03
Show Gist options
  • Save Lokaltog/5618224 to your computer and use it in GitHub Desktop.
Save Lokaltog/5618224 to your computer and use it in GitHub Desktop.
Powerline fish prompt renderer test
# vim:fileencoding=utf-8:noet
from powerline.renderer import Renderer
from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE
def int_to_rgb(num):
r = (num >> 16) & 0xff
g = (num >> 8) & 0xff
b = num & 0xff
return r, g, b
class FishRenderer(Renderer):
'''Powerline shell segment renderer.'''
escape_hl_start = ''
escape_hl_end = ''
term_truecolor = False
tmux_escape = False
screen_escape = False
def hlstyle(self, fg=None, bg=None, attr=None):
'''Highlight a segment.
If an argument is None, the argument is ignored. If an argument is
False, the argument is reset to the terminal defaults. If an argument
is a valid color or attribute, it's added to the ANSI escape code.
'''
ret = ''
ansi = []
r = '\033[{0}m'.format(';'.join(str(attr) for attr in ansi))
ret += r
ansi = []
if fg is not None:
if fg is False or fg[0] is False:
ansi += [39]
else:
if self.term_truecolor:
ansi += [38, 2] + list(int_to_rgb(fg[1]))
else:
ansi += [38, 5, fg[0]]
if ansi:
r = '\033[{0}m'.format(';'.join(str(attr) for attr in ansi))
ret += r
ansi = []
if bg is not None:
if bg is False or bg[0] is False:
ansi += [49]
else:
if self.term_truecolor:
ansi += [48, 2] + list(int_to_rgb(bg[1]))
else:
ansi += [48, 5, bg[0]]
if ansi:
r = '\033[{0}m'.format(';'.join(str(attr) for attr in ansi))
ret += r
ansi = []
if attr is not None:
if attr is False:
ansi += [22]
else:
if attr & ATTR_BOLD:
ansi += [1]
elif attr & ATTR_ITALIC:
# Note: is likely not to work or even be inverse in place of
# italic. Omit using this in colorschemes.
ansi += [3]
elif attr & ATTR_UNDERLINE:
ansi += [4]
if ansi:
r = '\033[{0}m'.format(';'.join(str(attr) for attr in ansi))
ret += r
ansi = []
return ret
r = '\033[{0}m'.format(';'.join(str(attr) for attr in ansi))
if self.tmux_escape:
r = '\033Ptmux;' + r.replace('\033', '\033\033') + '\033\\'
elif self.screen_escape:
r = '\033P' + r.replace('\033', '\033\033') + '\033\\'
return self.escape_hl_start + r + self.escape_hl_end
@staticmethod
def escape(string):
return string.replace('\\', '\\\\')
renderer = FishRenderer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment