git-off

git off handles large files in git repos
git clone https://noulin.net/git/git-off.git
Log | Files | Refs | README

colors.js (4825B)


      1 /*
      2 
      3 The MIT License (MIT)
      4 
      5 Original Library 
      6   - Copyright (c) Marak Squires
      7 
      8 Additional functionality
      9  - Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
     10 
     11 Permission is hereby granted, free of charge, to any person obtaining a copy
     12 of this software and associated documentation files (the "Software"), to deal
     13 in the Software without restriction, including without limitation the rights
     14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     15 copies of the Software, and to permit persons to whom the Software is
     16 furnished to do so, subject to the following conditions:
     17 
     18 The above copyright notice and this permission notice shall be included in
     19 all copies or substantial portions of the Software.
     20 
     21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     27 THE SOFTWARE.
     28 
     29 */
     30 
     31 var colors = {};
     32 module['exports'] = colors;
     33 
     34 colors.themes = {};
     35 
     36 var ansiStyles = colors.styles = require('./styles');
     37 var defineProps = Object.defineProperties;
     38 
     39 colors.supportsColor = require('./system/supports-colors');
     40 
     41 if (typeof colors.enabled === "undefined") {
     42   colors.enabled = colors.supportsColor;
     43 }
     44 
     45 colors.stripColors = colors.strip = function(str){
     46   return ("" + str).replace(/\x1B\[\d+m/g, '');
     47 };
     48 
     49 
     50 var stylize = colors.stylize = function stylize (str, style) {
     51   if (!colors.enabled) {
     52     return str+'';
     53   }
     54 
     55   return ansiStyles[style].open + str + ansiStyles[style].close;
     56 }
     57 
     58 var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
     59 var escapeStringRegexp = function (str) {
     60   if (typeof str !== 'string') {
     61     throw new TypeError('Expected a string');
     62   }
     63   return str.replace(matchOperatorsRe,  '\\$&');
     64 }
     65 
     66 function build(_styles) {
     67   var builder = function builder() {
     68     return applyStyle.apply(builder, arguments);
     69   };
     70   builder._styles = _styles;
     71   // __proto__ is used because we must return a function, but there is
     72   // no way to create a function with a different prototype.
     73   builder.__proto__ = proto;
     74   return builder;
     75 }
     76 
     77 var styles = (function () {
     78   var ret = {};
     79   ansiStyles.grey = ansiStyles.gray;
     80   Object.keys(ansiStyles).forEach(function (key) {
     81     ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
     82     ret[key] = {
     83       get: function () {
     84         return build(this._styles.concat(key));
     85       }
     86     };
     87   });
     88   return ret;
     89 })();
     90 
     91 var proto = defineProps(function colors() {}, styles);
     92 
     93 function applyStyle() {
     94   var args = arguments;
     95   var argsLen = args.length;
     96   var str = argsLen !== 0 && String(arguments[0]);
     97   if (argsLen > 1) {
     98     for (var a = 1; a < argsLen; a++) {
     99       str += ' ' + args[a];
    100     }
    101   }
    102 
    103   if (!colors.enabled || !str) {
    104     return str;
    105   }
    106 
    107   var nestedStyles = this._styles;
    108 
    109   var i = nestedStyles.length;
    110   while (i--) {
    111     var code = ansiStyles[nestedStyles[i]];
    112     str = code.open + str.replace(code.closeRe, code.open) + code.close;
    113   }
    114 
    115   return str;
    116 }
    117 
    118 function applyTheme (theme) {
    119   for (var style in theme) {
    120     (function(style){
    121       colors[style] = function(str){
    122         if (typeof theme[style] === 'object'){
    123           var out = str;
    124           for (var i in theme[style]){
    125             out = colors[theme[style][i]](out);
    126           }
    127           return out;
    128         }
    129         return colors[theme[style]](str);
    130       };
    131     })(style)
    132   }
    133 }
    134 
    135 colors.setTheme = function (theme) {
    136   if (typeof theme === 'string') {
    137     try {
    138       colors.themes[theme] = require(theme);
    139       applyTheme(colors.themes[theme]);
    140       return colors.themes[theme];
    141     } catch (err) {
    142       console.log(err);
    143       return err;
    144     }
    145   } else {
    146     applyTheme(theme);
    147   }
    148 };
    149 
    150 function init() {
    151   var ret = {};
    152   Object.keys(styles).forEach(function (name) {
    153     ret[name] = {
    154       get: function () {
    155         return build([name]);
    156       }
    157     };
    158   });
    159   return ret;
    160 }
    161 
    162 var sequencer = function sequencer (map, str) {
    163   var exploded = str.split(""), i = 0;
    164   exploded = exploded.map(map);
    165   return exploded.join("");
    166 };
    167 
    168 // custom formatter methods
    169 colors.trap = require('./custom/trap');
    170 colors.zalgo = require('./custom/zalgo');
    171 
    172 // maps
    173 colors.maps = {};
    174 colors.maps.america = require('./maps/america');
    175 colors.maps.zebra = require('./maps/zebra');
    176 colors.maps.rainbow = require('./maps/rainbow');
    177 colors.maps.random = require('./maps/random')
    178 
    179 for (var map in colors.maps) {
    180   (function(map){
    181     colors[map] = function (str) {
    182       return sequencer(colors.maps[map], str);
    183     }
    184   })(map)
    185 }
    186 
    187 defineProps(colors, init());