Skip to content

Commit

Permalink
Refactor coloring
Browse files Browse the repository at this point in the history
Use colorama rather than manually adding ANSI codes. This also means
Windows users should get properly colored output.

Fixes #28
  • Loading branch information
Marcus Rosenow authored and Marcus Rosenow committed Nov 18, 2015
1 parent 33ebdfe commit 1013243
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
16 changes: 7 additions & 9 deletions bin/commands/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
from collections import OrderedDict
from subprocess import call, check_output, PIPE, Popen

import colorama

from . import settings
from stateextensions import branches, log, reflog, stashes, status
from utils import directories
from utils.messages import error


class Colors:
green = '\x1B[0;32m'
no_color = '\x1B[0m'


def _print_section(title, accent=None, text=None, format='compact', show_empty=False):
"""Print a section."""

if not show_empty and not text:
return ""

if accent:
section = '# {}{} {}{}'.format(Colors.green, title, accent, Colors.no_color) + '\n'
section = '# {}{} {}{}'.format(colorama.Fore.GREEN, title, accent, colorama.Fore.RESET) + '\n'
else:
section = '# {}{}{}'.format(Colors.green, title, Colors.no_color) + '\n'
section = '# {}{}{}'.format(colorama.Fore.GREEN, title, colorama.Fore.RESET) + '\n'

if format == 'pretty' and text is not None and len(text) > 0:
# pretty print
Expand Down Expand Up @@ -65,10 +62,11 @@ def state(**kwargs):
show_color = kwargs.get('show_color').lower()
if show_color == 'never' or (show_color == 'auto' and not sys.stdout.isatty()):
show_color = 'never'
Colors.green = ''
Colors.no_color = ''
colorama.init(strip=True)
elif show_color == 'auto' and sys.stdout.isatty():
show_color = 'always'
colorama.init()

kwargs['show_color'] = show_color
kwargs['show_clean_message'] = settings.get(
'git-state.status.show-clean-message',
Expand Down
15 changes: 3 additions & 12 deletions bin/commands/stateextensions/status.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from ast import literal_eval
from subprocess import call, check_output, Popen, PIPE


class Colors:
green = '\x1B[0;32m'
red='\x1B[0;31m'
no_color = '\x1B[0m'
from colorama import Fore


def _set_color_status(show_color):
Expand Down Expand Up @@ -39,17 +35,12 @@ def accent(**kwargs):
new_repository = kwargs.get('new_repository', False)
show_color = kwargs.get('show_color', 'always')

if show_color == 'never':
Colors.no_color = ''
Colors.green = Colors.no_color
Colors.red = Colors.no_color

if new_repository:
title = '{no_color}({green}master{no_color})'.format(no_color=Colors.no_color, green=Colors.green)
title = '{no_color}({green}master{no_color})'.format(no_color=Fore.RESET, green=Fore.GREEN)
else:
original_color_status = _set_color_status(show_color)
title = check_output('git status --branch --short'.split()).splitlines()[0].lstrip('# ')
title = '{}({})'.format(Colors.no_color, title)
title = '{}({})'.format(Fore.RESET, title)
_reset_color_status(original_color_status)

return title
Expand Down

0 comments on commit 1013243

Please sign in to comment.