hgext/color.py
changeset 14495 ad6ad51cc0dd
parent 14145 4b7e4b9db8fb
child 14516 842a9179132c
equal deleted inserted replaced
14494:1ffeeb91c55d 14495:ad6ad51cc0dd
   112             'black_background': 40, 'red_background': 41,
   112             'black_background': 40, 'red_background': 41,
   113             'green_background': 42, 'yellow_background': 43,
   113             'green_background': 42, 'yellow_background': 43,
   114             'blue_background': 44, 'purple_background': 45,
   114             'blue_background': 44, 'purple_background': 45,
   115             'cyan_background': 46, 'white_background': 47}
   115             'cyan_background': 46, 'white_background': 47}
   116 
   116 
   117 def _terminfosetup(ui):
   117 def _terminfosetup(ui, mode):
   118     '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
   118     '''Initialize terminfo data and the terminal if we're in terminfo mode.'''
   119 
   119 
   120     global _terminfo_params
   120     global _terminfo_params
   121     # If we failed to load curses, we go ahead and return.
   121     # If we failed to load curses, we go ahead and return.
   122     if not _terminfo_params:
   122     if not _terminfo_params:
   123         return
   123         return
   124     # Otherwise, see what the config file says.
   124     # Otherwise, see what the config file says.
   125     mode = ui.config('color', 'mode', 'auto')
       
   126     if mode not in ('auto', 'terminfo'):
   125     if mode not in ('auto', 'terminfo'):
   127         return
   126         return
   128 
   127 
   129     _terminfo_params.update((key[6:], (False, int(val)))
   128     _terminfo_params.update((key[6:], (False, int(val)))
   130         for key, val in ui.configitems('color')
   129         for key, val in ui.configitems('color')
   146             del _terminfo_params[key]
   145             del _terminfo_params[key]
   147     if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
   146     if not curses.tigetstr('setaf') or not curses.tigetstr('setab'):
   148         ui.warn(_("no terminfo entry for setab/setaf: reverting to "
   147         ui.warn(_("no terminfo entry for setab/setaf: reverting to "
   149           "ECMA-48 color\n"))
   148           "ECMA-48 color\n"))
   150         _terminfo_params = {}
   149         _terminfo_params = {}
       
   150 
       
   151 def _modesetup(ui, opts):
       
   152     global _terminfo_params
       
   153 
       
   154     coloropt = opts['color']
       
   155     auto = coloropt == 'auto'
       
   156     always = not auto and util.parsebool(coloropt)
       
   157     if not always and not auto:
       
   158         return None
       
   159 
       
   160     formatted = always or (os.environ.get('TERM') != 'dumb' and ui.formatted())
       
   161 
       
   162     mode = ui.config('color', 'mode', 'auto')
       
   163     realmode = mode
       
   164     if mode == 'auto':
       
   165         if os.name == 'nt' and 'TERM' not in os.environ:
       
   166             # looks line a cmd.exe console, use win32 API or nothing
       
   167             realmode = 'win32'
       
   168         elif not formatted:
       
   169             realmode = 'ansi'
       
   170         else:
       
   171             realmode = 'terminfo'
       
   172 
       
   173     if realmode == 'win32':
       
   174         if not w32effects and mode == 'win32':
       
   175             # only warn if color.mode is explicitly set to win32
       
   176             ui.warn(_('warning: failed to set color mode to %s\n') % mode)
       
   177             return None
       
   178         _effects.update(w32effects)
       
   179     elif realmode == 'ansi':
       
   180         _terminfo_params = {}
       
   181     elif realmode == 'terminfo':
       
   182         _terminfosetup(ui, mode)
       
   183         if not _terminfo_params:
       
   184             if mode == 'terminfo':
       
   185                 ## FIXME Shouldn't we return None in this case too?
       
   186                 # only warn if color.mode is explicitly set to win32
       
   187                 ui.warn(_('warning: failed to set color mode to %s\n') % mode)
       
   188             realmode = 'ansi'
       
   189     else:
       
   190         return None
       
   191 
       
   192     if always or (auto and formatted):
       
   193         return realmode
       
   194     return None
   151 
   195 
   152 try:
   196 try:
   153     import curses
   197     import curses
   154     # Mapping from effect name to terminfo attribute name or color number.
   198     # Mapping from effect name to terminfo attribute name or color number.
   155     # This will also force-load the curses module.
   199     # This will also force-load the curses module.
   299 
   343 
   300 def uisetup(ui):
   344 def uisetup(ui):
   301     global _terminfo_params
   345     global _terminfo_params
   302     if ui.plain():
   346     if ui.plain():
   303         return
   347         return
   304 
       
   305     formatted = (os.environ.get('TERM') != 'dumb' and ui.formatted())
       
   306     mode = ui.config('color', 'mode', 'auto')
       
   307     if mode == 'auto':
       
   308         if os.name == 'nt' and 'TERM' not in os.environ:
       
   309             # looks line a cmd.exe console, use win32 API or nothing
       
   310             mode = w32effects and 'win32' or 'none'
       
   311         else:
       
   312             if not formatted:
       
   313                 _terminfo_params = False
       
   314             else:
       
   315                 _terminfosetup(ui)
       
   316                 if not _terminfo_params:
       
   317                     mode = 'ansi'
       
   318                 else:
       
   319                     mode = 'terminfo'
       
   320     if mode == 'win32':
       
   321         if w32effects is None:
       
   322             # only warn if color.mode is explicitly set to win32
       
   323             ui.warn(_('warning: failed to set color mode to %s\n') % mode)
       
   324             return
       
   325         _effects.update(w32effects)
       
   326     elif mode == 'ansi':
       
   327         _terminfo_params = {}
       
   328     elif mode == 'terminfo':
       
   329         _terminfosetup(ui)
       
   330     else:
       
   331         return
       
   332     def colorcmd(orig, ui_, opts, cmd, cmdfunc):
   348     def colorcmd(orig, ui_, opts, cmd, cmdfunc):
   333         coloropt = opts['color']
   349         mode = _modesetup(ui_, opts)
   334         auto = coloropt == 'auto'
   350         if mode:
   335         always = util.parsebool(coloropt)
       
   336         if (always or
       
   337             (always is None and auto and formatted)):
       
   338             colorui._colormode = mode
   351             colorui._colormode = mode
   339             colorui.__bases__ = (ui_.__class__,)
   352             colorui.__bases__ = (ui_.__class__,)
   340             ui_.__class__ = colorui
   353             ui_.__class__ = colorui
   341             extstyles()
   354             extstyles()
   342             configstyles(ui_)
   355             configstyles(ui_)