Skip to content

Instantly share code, notes, and snippets.

@Leko
Last active July 19, 2017 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leko/76727c0c4634b57de7231ba4427d5ed1 to your computer and use it in GitHub Desktop.
Save Leko/76727c0c4634b57de7231ba4427d5ed1 to your computer and use it in GitHub Desktop.
node_modules/
const chalk = require('chalk')
console.log(
chalk.red('H')
+ chalk.magenta('e')
+ chalk.yellow('l')
+ chalk.green('l')
+ chalk.blue('o')
+ ' '
+ chalk.cyan('w')
+ chalk.white('o')
+ chalk.yellowBright('r')
+ chalk.greenBright('l')
+ chalk.blueBright('d')
+ chalk.cyanBright('!')
+ chalk.whiteBright('!')
)
{
"name": "cli",
"version": "0.0.1",
"description": "https://blog.leko.jp/post/special-chars-for-cli",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/76727c0c4634b57de7231ba4427d5ed1.git"
},
"author": "Leko <leko.noor@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/76727c0c4634b57de7231ba4427d5ed1"
},
"homepage": "https://gist.github.com/76727c0c4634b57de7231ba4427d5ed1",
"dependencies": {
"chalk": "^2.0.1",
"ink": "^0.3.0"
}
}
const chalk = require('chalk')
console.log(
chalk.white.bgRed(' Hello ')
+ ' '
+ chalk.red.bgBlack('')
+ ' '
+ chalk.bgBlack(' world!! ')
+ ' '
+ chalk.black('')
)
const chalk = require('chalk')
const ucfirst = str => str.slice(0, 1).toUpperCase() + str.slice(1)
const toBgColor = color => `bg${ucfirst(color)}`
const width = () => process.stdout.columns
const eraser = () => `\r${' '.repeat(width())}\r`
class Indicator {
constructor ({ text, icon, color, backgroundColor }) {
this.text = text
this.icon = icon
this.color = color
this.backgroundColor = backgroundColor
}
getColor () {
return this.color
}
getBackgroundColor () {
return this.backgroundColor
}
render () {
let str = this.text
if (this.icon) {
str = `${this.icon} ${str}`
}
return chalk[this.getColor()][toBgColor(this.getBackgroundColor())](` ${str} `)
}
}
class IndicatorGlue {
constructor (componentA, componentB, chr = '') {
this.componentA = componentA
this.componentB = componentB
this.chr = chr
}
render () {
let decorator = chalk[this.componentA.getBackgroundColor()]
if (this.componentB) {
decorator = decorator[toBgColor(this.componentB.getBackgroundColor())]
}
return decorator(this.chr)
}
}
class IndicatorStack {
constructor () {
this.indicators = []
}
getComponents () {
return this.indicators
}
add (indicator) {
this.indicators.push(indicator)
}
render () {
const components = this.indicators.reduce((acc, indicator, i) => acc.concat([
indicator,
new IndicatorGlue(indicator, this.indicators[i + 1])
]), [])
return components.map(component => component.render()).join('')
}
}
class Powerline {
constructor () {
this.stacks = []
}
add (stack) {
this.stacks.push(stack)
}
render () {
return eraser() + this.stacks.map(stack => stack.render()).join('')
}
}
const powerline = new Powerline()
const stack = new IndicatorStack()
stack.add(new Indicator({ text: 'White', color: 'red', backgroundColor: 'white', icon: '✉' }))
stack.add(new Indicator({ text: 'Cyan', color: 'white', backgroundColor: 'cyan', icon: '' }))
stack.add(new Indicator({ text: 'Blue', color: 'white', backgroundColor: 'blue' }))
stack.add(new Indicator({ text: 'Magenta', color: 'white', backgroundColor: 'magenta', icon: '♥' }))
stack.add(new Indicator({ text: 'Red', color: 'white', backgroundColor: 'red', icon: '' }))
stack.add(new Indicator({ text: 'Yellow', color: 'white', backgroundColor: 'yellow', icon: '⚡︎' }))
stack.add(new Indicator({ text: 'Green', color: 'white', backgroundColor: 'green' }))
powerline.add(stack)
console.log('\n\n\n')
console.log(powerline.render())
console.log('\n\n\n')
const chalk = require('chalk')
const FPS = 20
const colors = [
'red',
'magenta',
'yellow',
'green',
'blue',
'cyan',
'white',
'yellowBright',
'greenBright',
'blueBright',
'cyanBright',
'whiteBright',
]
const frames = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'.split('')
let cursor = 0
setInterval(() => {
cursor = (cursor + 1) % frames.length
process.stdout.write(`\r \r${chalk[colors[cursor]](frames[cursor])}`)
}, 1000 / FPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment