├── .gitignore ├── LICENSE ├── README.md └── powerline-zsh.py /.gitignore: -------------------------------------------------------------------------------- 1 | .*/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012-2016 Chien-Wei Huang, Shrey Banga and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Powerline style prompt for Zsh 2 | =============================== 3 | 4 | *This is a fork from https://github.com/milkbikis/powerline-bash* 5 | 6 | A [Powerline](https://github.com/Lokaltog/vim-powerline) like prompt for Zsh: 7 | 8 | ![Powerline-Zsh Screenshot](http://i.imgur.com/QfDe3yP.png) 9 | ![Powerline-Zsh Screenshot2](http://i.imgur.com/f1kQcLv.png) 10 | 11 | * Shows some important details about the git branch: 12 | * Displays the current git branch which changes background color when the branch is dirty 13 | * A '+' appears when untracked files are present 14 | * When the local branch differs from the remote, the difference in number of commits is shown along with '⇡' or '⇣' indicating whether a git push or pull is pending 15 | * Changes color if the last command exited with a failure code 16 | * If you're too deep into a directory tree, shortens the displayed path with an ellipsis 17 | * Shows the current Python [virtualenv](http://www.virtualenv.org/) environment 18 | * It's all done in a Python script, so you could go nuts with it 19 | 20 | # Setup 21 | 22 | * This script uses ANSI color codes to display colors in a terminal. These are notoriously non-portable, so may not work for you out of the box, but try setting your $TERM to xterm-256color, because that works for me. 23 | i.e. edit your `.zshrc` file to add: 24 | 25 | ```shell 26 | export TERM='xterm-256color' 27 | ``` 28 | 29 | If you still face problems seeing colors then read this: https://gist.github.com/3749830#file_powerline_zsh_instructions.md 30 | 31 | * Patch the font you use for your terminal. 32 | Download the font from: https://github.com/Lokaltog/vim-powerline/wiki/Patched-fonts 33 | Follow the instructions: https://github.com/Lokaltog/vim-powerline/tree/develop/fontpatcher#font-patching-guide 34 | 35 | 36 | * Clone this repository somewhere: 37 | 38 | ```shell 39 | git clone https://github.com/carlcarl/powerline-zsh 40 | ``` 41 | 42 | * Create a symlink to the python script in your home: 43 | 44 | ```shell 45 | ln -s ~/powerline-zsh.py 46 | ``` 47 | 48 | If you don't want the symlink, just modify the path in the `.zshrc` command below 49 | 50 | * Now add the following to your `.zshrc`: 51 | 52 | ```shell 53 | function _update_ps1() 54 | { 55 | export PROMPT="$(~/powerline-zsh.py $?)" 56 | } 57 | precmd() 58 | { 59 | _update_ps1 60 | } 61 | ``` 62 | 63 | * powerline-zsh.py usage: 64 | 65 | ```shell 66 | -h, --help show this help message and exit 67 | --cwd-only Hide parent directory 68 | --hostname Show hostname at the begin 69 | -m Choose icon font: default, compatible, patched or konsole. 70 | Default is "default" 71 | ``` 72 | 73 | # Python version note 74 | 75 | Most of the distros use `Python2` as default, however, Some distros like `Archlinux` use `Python3`. The earlier version of `powerline-zsh` is not compatible with `Python3`. With such condition, you have two ways to solve this issue. 76 | 77 | 1. Use the newest version of `powerline-zsh`. Just download from the `master` branch. 78 | 2. Modify the `.zshrc` content, use `python2` to execute it. 79 | 80 | ```shell 81 | function _update_ps1() 82 | { 83 | export PROMPT="$(python2 ~/powerline-zsh.py $?)" 84 | } 85 | precmd() 86 | { 87 | _update_ps1 88 | } 89 | ``` 90 | 91 | 92 | # Pypy note 93 | 94 | You can use `pypy` to speed up your script execution, in your `.zshrc`: 95 | 96 | 97 | ```shell 98 | function _update_ps1() 99 | { 100 | error=$? 101 | if [[ -s "/usr/local/bin/pypy" ]]; then 102 | export PROMPT="$(pypy ~/powerline-zsh.py $error)" 103 | else 104 | export PROMPT="$(~/powerline-zsh.py $error)" 105 | fi 106 | } 107 | precmd() 108 | { 109 | _update_ps1 110 | } 111 | ``` 112 | 113 | 114 | # konsole user note 115 | 116 | You may not see the icons when using konsole. To solve this problem, you can use `-m` option: 117 | 118 | 119 | ```shell 120 | function _update_ps1() 121 | { 122 | export PROMPT="$(~/powerline-zsh.py -m konsole $?)" 123 | } 124 | precmd() 125 | { 126 | _update_ps1 127 | } 128 | ``` 129 | 130 | # Authors and License 131 | The ``powerline-zsh`` package is written by Chien-Wei Huang, Shrey Banga and contributors. It’s MIT licensed and freely available. 132 | 133 | Feel free to improve this package and send a pull request to GitHub. 134 | 135 | -------------------------------------------------------------------------------- /powerline-zsh.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from __future__ import print_function 5 | from __future__ import unicode_literals 6 | import os 7 | import subprocess 8 | import sys 9 | import re 10 | import argparse 11 | 12 | 13 | encoding = sys.getdefaultencoding() 14 | 15 | 16 | def warn(msg): 17 | print('[powerline-zsh] ', msg) 18 | 19 | 20 | class Color: 21 | # The following link is a pretty good resources for color values: 22 | # http://www.calmar.ws/vim/color-output.png 23 | 24 | PATH_BG = 237 # dark grey 25 | PATH_FG = 250 # light grey 26 | CWD_FG = 254 # nearly-white grey 27 | SEPARATOR_FG = 244 28 | 29 | REPO_CLEAN_BG = 148 # a light green color 30 | REPO_CLEAN_FG = 0 # black 31 | REPO_DIRTY_BG = 161 # pink/red 32 | REPO_DIRTY_FG = 15 # white 33 | 34 | CMD_PASSED_BG = 236 35 | CMD_PASSED_FG = 15 36 | CMD_FAILED_BG = 161 37 | CMD_FAILED_FG = 15 38 | 39 | SVN_CHANGES_BG = 148 40 | SVN_CHANGES_FG = 22 # dark green 41 | 42 | VIRTUAL_ENV_BG = 35 # a mid-tone green 43 | VIRTUAL_ENV_FG = 22 44 | 45 | 46 | class Powerline: 47 | symbols = { 48 | 'none': { 49 | 'separator': '', 50 | 'separator_thin': '' 51 | }, 52 | 'compatible': { 53 | 'separator': '\u25B6', 54 | 'separator_thin': '\u276F' 55 | }, 56 | 'patched': { 57 | 'separator': '\u2B80', 58 | 'separator_thin': '\u2B81' 59 | }, 60 | 'konsole': { 61 | 'separator': '\ue0b0', 62 | 'separator_thin': '\ue0b1' 63 | }, 64 | 'default': { 65 | 'separator': '⮀', 66 | 'separator_thin': '⮁' 67 | } 68 | } 69 | LSQESCRSQ = '\\[\\e%s\\]' 70 | reset = ' %f%k' 71 | 72 | def __init__(self, mode='default'): 73 | self.separator = Powerline.symbols[mode]['separator'] 74 | self.separator_thin = Powerline.symbols[mode]['separator_thin'] 75 | self.segments = [] 76 | 77 | def color(self, prefix, code): 78 | if prefix == '38': 79 | return '%%F{%s}' % code 80 | elif prefix == '48': 81 | return '%%K{%s}' % code 82 | 83 | def fgcolor(self, code): 84 | return self.color('38', code) 85 | 86 | def bgcolor(self, code): 87 | return self.color('48', code) 88 | 89 | def append(self, segment): 90 | self.segments.append(segment) 91 | 92 | def draw(self): 93 | return (''.join((s[0].draw(self, s[1]) for s in zip(self.segments, self.segments[1:] + [None]))) 94 | + self.reset) 95 | 96 | 97 | class Segment: 98 | def __init__(self, powerline, content, fg, bg, separator=None, separator_fg=None): 99 | self.powerline = powerline 100 | self.content = content 101 | self.fg = fg 102 | self.bg = bg 103 | self.separator = separator or powerline.separator 104 | self.separator_fg = separator_fg or bg 105 | 106 | def draw(self, powerline, next_segment=None): 107 | if next_segment: 108 | separator_bg = powerline.bgcolor(next_segment.bg) 109 | else: 110 | separator_bg = powerline.reset 111 | 112 | return ''.join(( 113 | powerline.fgcolor(self.fg), 114 | powerline.bgcolor(self.bg), 115 | self.content, 116 | separator_bg, 117 | powerline.fgcolor(self.separator_fg), 118 | self.separator)) 119 | 120 | 121 | def add_cwd_segment(powerline, cwd, maxdepth, cwd_only=False, hostname=False): 122 | home = os.getenv('HOME') 123 | cwd = os.getenv('PWD') 124 | 125 | if cwd.find(home) == 0: 126 | cwd = cwd.replace(home, '~', 1) 127 | 128 | if cwd[0] == '/': 129 | cwd = cwd[1:] 130 | 131 | names = cwd.split('/') 132 | if len(names) > maxdepth: 133 | names = names[:2] + ['⋯ '] + names[2 - maxdepth:] 134 | 135 | if hostname: 136 | powerline.append(Segment(powerline, ' %m ' , Color.CWD_FG, Color.PATH_BG, powerline.separator_thin, Color.SEPARATOR_FG)) 137 | 138 | if not cwd_only: 139 | for n in names[:-1]: 140 | powerline.append(Segment(powerline, ' %s ' % n, Color.PATH_FG, Color.PATH_BG, powerline.separator_thin, Color.SEPARATOR_FG)) 141 | powerline.append(Segment(powerline, ' %s ' % names[-1], Color.CWD_FG, Color.PATH_BG)) 142 | 143 | 144 | def get_hg_status(): 145 | has_modified_files = False 146 | has_untracked_files = False 147 | has_missing_files = False 148 | output = subprocess.Popen(['hg', 'status'], stdout=subprocess.PIPE).communicate()[0] 149 | for line in output.split('\n'): 150 | if line == '': 151 | continue 152 | elif line[0] == '?': 153 | has_untracked_files = True 154 | elif line[0] == '!': 155 | has_missing_files = True 156 | else: 157 | has_modified_files = True 158 | return has_modified_files, has_untracked_files, has_missing_files 159 | 160 | 161 | def add_hg_segment(powerline, cwd): 162 | branch = os.popen('hg branch 2> /dev/null').read().rstrip() 163 | if len(branch) == 0: 164 | return False 165 | bg = Color.REPO_CLEAN_BG 166 | fg = Color.REPO_CLEAN_FG 167 | has_modified_files, has_untracked_files, has_missing_files = get_hg_status() 168 | if has_modified_files or has_untracked_files or has_missing_files: 169 | bg = Color.REPO_DIRTY_BG 170 | fg = Color.REPO_DIRTY_FG 171 | extra = '' 172 | if has_untracked_files: 173 | extra += '+' 174 | if has_missing_files: 175 | extra += '!' 176 | branch += (' ' + extra if extra != '' else '') 177 | powerline.append(Segment(powerline, ' %s ' % branch, fg, bg)) 178 | return True 179 | 180 | 181 | def get_git_status(): 182 | has_pending_commits = True 183 | has_untracked_files = False 184 | origin_position = "" 185 | output = subprocess.Popen(['git', 'status', '-unormal'], stdout=subprocess.PIPE).communicate()[0] 186 | for line in output.decode(encoding).split('\n'): 187 | origin_status = re.findall("Your branch is (ahead|behind).*?(\d+) comm", line) 188 | if len(origin_status) > 0: 189 | origin_position = " %d" % int(origin_status[0][1]) 190 | if origin_status[0][0] == 'behind': 191 | origin_position += '⇣' 192 | elif origin_status[0][0] == 'ahead': 193 | origin_position += '⇡' 194 | 195 | if line.find('nothing to commit') >= 0: 196 | has_pending_commits = False 197 | if line.find('Untracked files') >= 0: 198 | has_untracked_files = True 199 | return has_pending_commits, has_untracked_files, origin_position 200 | 201 | 202 | def add_git_segment(powerline, cwd): 203 | p = subprocess.Popen(['git', 'symbolic-ref', '-q', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 204 | out, err = p.communicate() 205 | 206 | if 'not a git repo' in err.decode(encoding).lower(): 207 | return False 208 | 209 | if out: 210 | branch = out[len('refs/heads/'):].rstrip() 211 | else: 212 | branch = b'(Detached)' 213 | 214 | branch = branch.decode(encoding) 215 | has_pending_commits, has_untracked_files, origin_position = get_git_status() 216 | branch += origin_position 217 | if has_untracked_files: 218 | branch += ' +' 219 | 220 | bg = Color.REPO_CLEAN_BG 221 | fg = Color.REPO_CLEAN_FG 222 | 223 | if has_pending_commits: 224 | bg = Color.REPO_DIRTY_BG 225 | fg = Color.REPO_DIRTY_FG 226 | 227 | powerline.append(Segment(powerline, ' %s ' % branch, fg, bg)) 228 | return True 229 | 230 | 231 | def add_svn_segment(powerline, cwd): 232 | if not os.path.exists(os.path.join(cwd, '.svn')): 233 | return 234 | '''svn info: 235 | First column: Says if item was added, deleted, or otherwise changed 236 | ' ' no modifications 237 | 'A' Added 238 | 'C' Conflicted 239 | 'D' Deleted 240 | 'I' Ignored 241 | 'M' Modified 242 | 'R' Replaced 243 | 'X' an unversioned directory created by an externals definition 244 | '?' item is not under version control 245 | '!' item is missing (removed by non-svn command) or incomplete 246 | '~' versioned item obstructed by some item of a different kind 247 | ''' 248 | # TODO: Color segment based on above status codes 249 | try: 250 | p1 = subprocess.Popen(['svn', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 251 | p2 = subprocess.Popen(['grep', '-c', '^[ACDIMRX\\!\\~]'], stdin=p1.stdout, stdout=subprocess.PIPE) 252 | output = p2.communicate()[0].strip() 253 | if len(output) > 0 and int(output) > 0: 254 | changes = output.strip() 255 | powerline.append(Segment(powerline, ' %s ' % changes, Color.SVN_CHANGES_FG, Color.SVN_CHANGES_BG)) 256 | except OSError: 257 | return False 258 | except subprocess.CalledProcessError: 259 | return False 260 | return True 261 | 262 | 263 | def add_repo_segment(powerline, cwd): 264 | for add_repo_segment in [add_git_segment, add_svn_segment, add_hg_segment]: 265 | try: 266 | if add_repo_segment(p, cwd): 267 | return 268 | except subprocess.CalledProcessError: 269 | pass 270 | except OSError: 271 | pass 272 | 273 | 274 | def add_virtual_env_segment(powerline, cwd): 275 | env = os.getenv("VIRTUAL_ENV") 276 | if env is None: 277 | return False 278 | env_name = os.path.basename(env) 279 | bg = Color.VIRTUAL_ENV_BG 280 | fg = Color.VIRTUAL_ENV_FG 281 | powerline.append(Segment(powerline, ' %s ' % env_name, fg, bg)) 282 | return True 283 | 284 | 285 | def add_root_indicator(powerline, error): 286 | bg = Color.CMD_PASSED_BG 287 | fg = Color.CMD_PASSED_FG 288 | if int(error) != 0: 289 | fg = Color.CMD_FAILED_FG 290 | bg = Color.CMD_FAILED_BG 291 | powerline.append(Segment(powerline, ' ❄', fg, bg)) 292 | 293 | 294 | def get_valid_cwd(): 295 | try: 296 | cwd = os.getcwd() 297 | except: 298 | cwd = os.getenv('PWD') # This is where the OS thinks we are 299 | parts = cwd.split(os.sep) 300 | up = cwd 301 | while parts and not os.path.exists(up): 302 | parts.pop() 303 | up = os.sep.join(parts) 304 | try: 305 | os.chdir(up) 306 | except: 307 | warn("Your current directory is invalid.") 308 | sys.exit(1) 309 | warn("Your current directory is invalid. Lowest valid directory: " + up) 310 | return cwd 311 | 312 | if __name__ == '__main__': 313 | arg_parser = argparse.ArgumentParser() 314 | arg_parser.add_argument( 315 | '--cwd-only', 316 | action="store_true", 317 | help=( 318 | 'Hide parent directory' 319 | ), 320 | ) 321 | arg_parser.add_argument( 322 | '--hostname', 323 | action="store_true", 324 | help=( 325 | 'Show hostname at the begin' 326 | ), 327 | ) 328 | arg_parser.add_argument('prev_error', nargs='?', default=0) 329 | arg_parser.add_argument( 330 | '-m', 331 | default='default', 332 | help=( 333 | 'Choose icon font: default, none, compatible, patched or konsole.' 334 | ' Default is "default"' 335 | ), 336 | choices=['default', 'none', 'compatible', 'patched', 'konsole'], 337 | metavar='' 338 | ) 339 | args = arg_parser.parse_args() 340 | 341 | p = Powerline(mode=args.m) 342 | cwd = get_valid_cwd() 343 | add_virtual_env_segment(p, cwd) 344 | add_cwd_segment(p, cwd, 5, args.cwd_only, args.hostname) 345 | add_repo_segment(p, cwd) 346 | add_root_indicator(p, args.prev_error) 347 | if sys.version_info[0] < 3: 348 | sys.stdout.write(p.draw().encode('utf-8')) 349 | else: 350 | sys.stdout.buffer.write(p.draw().encode('utf-8')) 351 | 352 | # vim: set expandtab: 353 | --------------------------------------------------------------------------------