mirror of
				https://github.com/flynx/colors.js.git
				synced 2025-10-31 20:10:09 +00:00 
			
		
		
		
	
						commit
						be9b77f96c
					
				
							
								
								
									
										23
									
								
								lib/system/has-flag.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								lib/system/has-flag.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | |||||||
|  | /* | ||||||
|  | MIT License | ||||||
|  | 
 | ||||||
|  | Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) | ||||||
|  | 
 | ||||||
|  | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||||||
|  | 
 | ||||||
|  | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||||||
|  | 
 | ||||||
|  | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  | */ | ||||||
|  | 
 | ||||||
|  | 'use strict'; | ||||||
|  | 
 | ||||||
|  | module.exports = function (flag, argv) { | ||||||
|  | 	argv = argv || process.argv; | ||||||
|  | 
 | ||||||
|  | 	var terminatorPos = argv.indexOf('--'); | ||||||
|  | 	var prefix = /^-{1,2}/.test(flag) ? '' : '--'; | ||||||
|  | 	var pos = argv.indexOf(prefix + flag); | ||||||
|  | 
 | ||||||
|  | 	return pos !== -1 && (terminatorPos === -1 ? true : pos < terminatorPos); | ||||||
|  | }; | ||||||
| @ -23,39 +23,125 @@ THE SOFTWARE. | |||||||
| 
 | 
 | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| var argv = process.argv; | 'use strict'; | ||||||
| 
 | 
 | ||||||
| module.exports = (function () { | var os = require('os'); | ||||||
|   if (argv.indexOf('--no-color') !== -1 || | var hasFlag = require('./has-flag.js'); | ||||||
|     argv.indexOf('--color=false') !== -1) { | 
 | ||||||
|  | var env = process.env; | ||||||
|  | 
 | ||||||
|  | var forceColor = void 0; | ||||||
|  | if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false')) { | ||||||
|  | 	forceColor = false; | ||||||
|  | } else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true') || hasFlag('color=always')) { | ||||||
|  | 	forceColor = true; | ||||||
|  | } | ||||||
|  | if ('FORCE_COLOR' in env) { | ||||||
|  | 	forceColor = env.FORCE_COLOR.length === 0 || parseInt(env.FORCE_COLOR, 10) !== 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function translateLevel(level) { | ||||||
|  | 	if (level === 0) { | ||||||
| 		return false; | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|   if (argv.indexOf('--color') !== -1 || | 	return { | ||||||
|     argv.indexOf('--color=true') !== -1 || | 		level: level, | ||||||
|     argv.indexOf('--color=always') !== -1) { | 		hasBasic: true, | ||||||
|     return true; | 		has256: level >= 2, | ||||||
|  | 		has16m: level >= 3 | ||||||
|  | 	}; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|   if (process.stdout && !process.stdout.isTTY) { | function supportsColor(stream) { | ||||||
|     return false; | 	if (forceColor === false) { | ||||||
|  | 		return 0; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if (hasFlag('color=16m') || hasFlag('color=full') || hasFlag('color=truecolor')) { | ||||||
|  | 		return 3; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (hasFlag('color=256')) { | ||||||
|  | 		return 2; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (stream && !stream.isTTY && forceColor !== true) { | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var min = forceColor ? 1 : 0; | ||||||
|  | 
 | ||||||
| 	if (process.platform === 'win32') { | 	if (process.platform === 'win32') { | ||||||
|     return true; | 		// Node.js 7.5.0 is the first version of Node.js to include a patch to
 | ||||||
|  | 		// libuv that enables 256 color output on Windows. Anything earlier and it
 | ||||||
|  | 		// won't work. However, here we target Node.js 8 at minimum as it is an LTS
 | ||||||
|  | 		// release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
 | ||||||
|  | 		// release that supports 256 colors. Windows 10 build 14931 is the first release
 | ||||||
|  | 		// that supports 16m/TrueColor.
 | ||||||
|  | 		var osRelease = os.release().split('.'); | ||||||
|  | 		if (Number(process.versions.node.split('.')[0]) >= 8 && Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) { | ||||||
|  | 			return Number(osRelease[2]) >= 14931 ? 3 : 2; | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|   if ('COLORTERM' in process.env) { | 		return 1; | ||||||
|     return true; |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|   if (process.env.TERM === 'dumb') { | 	if ('CI' in env) { | ||||||
|     return false; | 		if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(function (sign) { | ||||||
|  | 			return sign in env; | ||||||
|  | 		}) || env.CI_NAME === 'codeship') { | ||||||
|  | 			return 1; | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|   if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)) { | 		return min; | ||||||
|     return true; |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|   return false; | 	if ('TEAMCITY_VERSION' in env) { | ||||||
| })(); | 		return (/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0 | ||||||
|  | 		); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if ('TERM_PROGRAM' in env) { | ||||||
|  | 		var version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10); | ||||||
|  | 
 | ||||||
|  | 		switch (env.TERM_PROGRAM) { | ||||||
|  | 			case 'iTerm.app': | ||||||
|  | 				return version >= 3 ? 3 : 2; | ||||||
|  | 			case 'Hyper': | ||||||
|  | 				return 3; | ||||||
|  | 			case 'Apple_Terminal': | ||||||
|  | 				return 2; | ||||||
|  | 			// No default
 | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (/-256(color)?$/i.test(env.TERM)) { | ||||||
|  | 		return 2; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) { | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if ('COLORTERM' in env) { | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (env.TERM === 'dumb') { | ||||||
|  | 		return min; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return min; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function getSupportLevel(stream) { | ||||||
|  | 	var level = supportsColor(stream); | ||||||
|  | 	return translateLevel(level); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = { | ||||||
|  | 	supportsColor: getSupportLevel, | ||||||
|  | 	stdout: getSupportLevel(process.stdout), | ||||||
|  | 	stderr: getSupportLevel(process.stderr) | ||||||
|  | }; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user