npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bchalk

v1.0.1

Published

⚙ A simple util to add style to console logs

Downloads

2

Readme

自定义控制台输出实用工具

目标

提供一种简易的方式自定义控制台输出,快捷定义项目的输出样式,从而输出高辨识度的控制台输出文本。 提供 Chalk 类似兼容的方式对输出内容进行处理(Browser's Chalk)

理念

遵循开闭原则,提供默认预置样式与样式串联叠加能力,同时开放样式自定义与样式 Alias 的设置。

重要说明 ⚠️

为了保持API调用的浏览器兼容性与输出内容的可读性,此实用工具在非现代浏览器(IE等)上会使用 原始格式 输出,请留意。 为了保持跨浏览器兼容性,您使用的样式描述 CSS 文本串的类型定义最好限定在下述列表内

  • background 与其全写版本。
  • border 与其全写版本。
  • border-radius
  • box-decoration-break
  • box-shadow
  • clear 和 float
  • color
  • cursor
  • display
  • font 与其全写版本。
  • line-height
  • margin
  • outline 与其全写版本。
  • padding
  • text-transform 这类 text-* 属性
  • white-space
  • word-spacing 和 word-break
  • writing-mode

效果演示

bc.log(bc.bgMagenta.white.lighter.large('hello world'))

img1

bc.log(bc.bgBlack.white.bold.larger('hello world'))

img2

bc.log(bc.bgCyan.black.largest.asTag('hello world'))

img3

bc.log(bc.bgMagenta.white.lighter.massive.asTag('hello world'))

img4

bc.log(bc.cyan.bgBlack('hello world'))

img5

bc.log(bc.asWarning.strikethrough('hello world'))

img6

bc.log(bc.asAlert.underline('hello world'))

img7

bc.log(bc.asMarquee('hello world'))

img8

使用方式

先安装 bchalk 包

# 使用 yarn
yarn add bchalk --save
# 使用 npm
npm install bchalk

初始化有两种方式,您可以直接使用我们直接提供的默认 browserChalk 实例

import { browserChalk as bc } from 'bchalk'
// 定制
bc.setupRootStyle('color: red;')
  .setupStyle('asTag', bc.bold.padded.rounded)
  .setupStyle(
    'asMarquee',
    'font-size: 30px; padding: 20px 30px 20px 30px; display: inline-block; border: 3px solid gold;',
  )
// 输出
bc.log(bc.bgCyan.black.largest.asTag('hello world'))
// 覆盖 window.console (不建议)
window.console = bc

或者为了在多处使用,您也可以使用自定义全局单例初始化

import BrowserChalk from 'bchalk'
// 实例化定制
const bc = BrowserChalk.getInstance('color: red;')
    .setupStyle('asTag', bc.bold.padded.rounded)
    .setupStyle(
        'asMarquee',
        'font-size: 30px; padding: 20px 30px 20px 30px; display: inline-block; border: 3px solid gold;',
    )
// 输出
bc.log(bc.bgCyan.black.largest.asTag('hello world'))
// 覆盖 window.console (不建议)
window.console = bc

API

BrowserChalk constructor

interface BrowserChalkInitOptions {
  rewriteOriginalConsole?: boolean
}
/**
 * Creates an instance of BrowserChalk.
 * @param {string} [rootStyle='']
 * @param {BrowserChalkInitOptions} [options]
 * @memberof BrowserChalk
 */
public constructor (rootStyle: string = '', options?: BrowserChalkInitOptions)

BrowserChalk.getInstance

/**
 * getInstance 单例获取器
 * @static
 * @param {string} [rootStyle]
 * @param {BrowserChalkInitOptions} [options]
 * @returns
 * @memberof BrowserChalk
 */
public static getInstance(rootStyle?: string, options?: BrowserChalkInitOptions)

BrowserChalk.setupRootStyle

/**
 * @API
 * 设置根样式(默认样式)
 * @param {string} cssString
 * @memberof BrowserChalk
 */
public setupRootStyle (cssString: string = '')

BrowserChalk.setupStyle

/**
 * @API
 * 添加样式,可以是样式字符串,或者别的样式的引用
 * e.g:
 * 	bc.setupStyle('asWarning', 'backgroundColor: yellow')
 * 	bc.setupStyle('asHint', bc.bgBlack.white.bold.larger)
 * @param {string} styleAlias
 * @param {(string | BrowserChalkStyle)} [styleContent='']
 * @returns
 * @memberof BrowserChalk
 */
public setupStyle (styleAlias: string, styleContent: string | BrowserChalkStyle = '')

预置样式表

BrowserChalk内置了多个预置样式,您可完全不需要定制便能使用,如下所示:

字体颜色
  • black
  • red
  • green
  • blue
  • yellow
  • magenta
  • cyan
  • white
  • gray
  • gold
背景颜色
  • bgBlack
  • bgRed
  • bgGreen
  • bgBlue
  • bgYellow
  • bgMagenta
  • bgCyan
  • bgWhite
  • bgGray
  • bgGold
字形
  • bold
  • lighter
  • italic
  • strikethrough
  • underline
  • large
  • larger
  • largest
  • massive
块样式
  • padded
  • rounded

我们还提供了扩展的浏览器 CSS 颜色预置集,您可单独取用:

import { setupStyle as setupTanStyle } from 'bchalk/dist/extend/tan.esm'
// 注册
const bc = BrowserChalk.getInstance()
setupTanStyle(bc)
// 使用
bc.log(bc.bgTan.black('hello world'))
bc.log(bc.bgWhite.tan('hello world'))