Skip to content

Commit

Permalink
feat: align basic and fancy reporter tags
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 9, 2018
1 parent 113780e commit 38a4729
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/reporters/basic.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import util from 'util'
import { isPlainObject, parseStack } from '../utils'
import { isPlainObject, parseStack, align } from '../utils'

export default class BasicReporter {
constructor (options) {
this.options = Object.assign({
stream: process.stdout
stream: process.stdout,
tagAlignment: 'left'
}, options)
}

Expand Down Expand Up @@ -71,12 +72,12 @@ export default class BasicReporter {

// Print date
if (fields.type.length) {
this.write((`[${fields.date}] `))
this.write((`[${align(this.options.tagAlignment, fields.date, 8)}] `))
}

// Print type
if (fields.type.length) {
this.write((`[${fields.type.toUpperCase()}] `))
this.write((`[${align(this.options.tagAlignment, fields.type.toUpperCase(), 7)}] `))
}

// Print tag
Expand Down
7 changes: 4 additions & 3 deletions src/reporters/fancy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import figures from 'figures'
import chalk from 'chalk'
import BasicReporter from './basic'
import { parseStack } from '../utils'
import { parseStack, align } from '../utils'

function chalkColor (name) {
if (name[0] === '#') {
Expand Down Expand Up @@ -50,11 +50,12 @@ export default class FancyReporter extends BasicReporter {
}

// Print type
const type = align(this.options.tagAlignment, fields.type.toUpperCase(), 7)
if (logObj.badge) {
this.write('\n' + chalkBgColor(logObj.color).black(` ${fields.type.toUpperCase()} `) + ' ')
this.write('\n' + chalkBgColor(logObj.color).black(` ${type} `) + ' ')
} else if (fields.type !== 'log') {
const icon = logObj.icon || ICONS[fields.type] || ICONS.default
this.write(chalkColor(logObj.color)(`${icon} ${fields.type.toUpperCase()} `))
this.write(chalkColor(logObj.color)(`${icon} ${type} `))
}

// Print tag
Expand Down
42 changes: 42 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,45 @@ export function parseStack (stack) {

return lines
}

export function centerALign (str, len, space = ' ') {
const free = len - str.length
if (free <= 0) {
return str
}
const freeLeft = Math.floor(free / 2)
let _str = ''
for (let i = 0; i < len; i++) {
_str += (i < freeLeft || i >= freeLeft + str.length) ? space : str[i - freeLeft]
}
return _str
}

export function rightALign (str, len, space = ' ') {
const free = len - str.length
if (free <= 0) {
return str
}
let _str = ''
for (let i = 0; i < len; i++) {
_str += i < free ? space : str[i - free]
}
return _str
}

export function leftALign (str, len, space = ' ') {
let _str = ''
for (let i = 0; i < len; i++) {
_str += i < str.length ? str[i] : space
}
return _str
}

export function align (alignment, str, len, space = ' ') {
switch (alignment) {
case 'left': return leftALign(str, len, space)
case 'right': return rightALign(str, len, space)
case 'center': return centerALign(str, len, space)
default: return str
}
}

0 comments on commit 38a4729

Please sign in to comment.