Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamps to log #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chalk = require('chalk')
const width = require('string-width')

const colorMap = {
info: 'green',
Expand All @@ -9,9 +10,18 @@ const colorMap = {

const print = (type, label, message) => {
const color = colorMap[type]

const fmtDate = chalk.grey(new Date().toLocaleTimeString())
const fmtLabel = chalk.inverse.bold[color](` ${label} `)
const fmtMessage = chalk[color](message)

const widthChars = width(fmtLabel) + width(fmtMessage) + width(fmtDate)

let logSpace = process.stdout.columns - widthChars - 2
if (logSpace <= 0) logSpace = 10

console.log(
chalk.inverse.bold[color](` ${label} `),
chalk[color](message),
`${fmtLabel} ${fmtMessage}${' '.repeat(logSpace)}${fmtDate}`,
'\n'
)
}
Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"repository": "siddharthkp/prettycli",
"homepage": "https://github.com/siddharthkp/prettycli",
"dependencies": {
"chalk": "2.1.0"
"chalk": "2.1.0",
"string-width": "^2.1.1"
}
}
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.