mock-json-package
Version:
122 lines (95 loc) • 2.95 kB
JavaScript
import terminalLink from 'terminal-link'
// @see: https://www.codegrepper.com/code-examples/javascript/nodejs+terminal+print+bold
const CLEAR_STYLE = '\x1b[0m'
const BOLD = '\x1b[1m'
const DIM = '\x1b[2m'
const UNDERLINE = '\x1b[4m'
const STRIKETHROUGH = '\x1b[9m'
const REVERSE = '\x1b[7m'
const RED = '\x1b[31m'
const GREEN = '\x1b[32m'
const YELLOW = '\x1b[33m'
const BLUE = '\x1b[34m'
const MAGENTA = '\x1b[35m'
const CYAN = '\x1b[36m'
const DIVIDER = '≖' // ▰ ⊷ ┅ ⋯
// character-width of the current terminal window
export function terminalWidth() {
return process.stdout.columns
}
// prints a section header (will stringify any params passed in)
export function header() {
let s = ` ${stringifyArgs(arguments)}`
console.log(negative(`${s}${' '.repeat(terminalWidth() - s.length)}`))
}
// prints a sectino footer
export function footer() {
console.log()
}
// prints a section divider
export function divider() {
console.log(`${dim(DIVIDER.repeat(terminalWidth()))}`)
}
// prints a line break
export function br() {
console.log()
}
// prints a horizontal rule
export function hr() {
divider()
}
// prints text right-aligned (will stringify any params passed in)
export function right() {
let s = `${stringifyArgs(arguments).trimEnd()} `
console.log(`${' '.repeat(terminalWidth() - s.length)}${s}`)
}
// prints a hyperlink to console - args: (text, uri)
export function link() {
return terminalLink.apply(arguments)
}
// Prints a key-value pair to console with consistent indentation’
export function kvp(k, v, tabTo = 16) {
let s = `${k}`
if (s.length > 0) s = ` ${s}: `
console.log(`${s}${s.length < tabTo ? ' '.repeat(tabTo - s.length) : ''}${v}`)
}
export function bold() {
return `${BOLD}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function dim() {
return `${DIM}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function underline() {
return `${UNDERLINE}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function strikethrough() {
return `${STRIKETHROUGH}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function negative() {
return `${REVERSE}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function red() {
return `${RED}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function green() {
return `${GREEN}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function yellow() {
return `${YELLOW}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function blue() {
return `${BLUE}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function magenta() {
return `${MAGENTA}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
export function cyan() {
return `${CYAN}${stringifyArgs(arguments)}${CLEAR_STYLE}`
}
function stringifyArgs(args) {
let s = ''
for (let i = 0; i < args.length; ++i) {
s = `${s}${args[i]}${i + 1 < args.length ? ' ' : ''}`
}
return s
}