Skip to content

Commit

Permalink
feat(cli-table): integrated cli-table to show important option in ter…
Browse files Browse the repository at this point in the history
…minal
  • Loading branch information
weirui88888 committed Dec 13, 2023
1 parent ce3a14b commit 315aacb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"base64-img": "^1.0.4",
"canvas": "^2.11.2",
"cli-progress": "^3.12.0",
"cli-table": "^0.3.4",
"commander": "^11.0.0",
"figlet": "^1.6.0",
"is-image-url": "^1.1.8",
Expand Down
18 changes: 11 additions & 7 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion src/cmd/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ const calculateSeparator = (separator, usedContent) => {
en: 'space',
'zh-cn': 'empty'
}

try {
if (supportSeparator.includes(separator)) return separator
const languages = langdetect.detect(usedContent)
const languageSeparator = separatorMap[languages[0].lang]
return languageSeparator in separatorMap ? languageSeparator : defaultSeparator
return languageSeparator ?? defaultSeparator
} catch (error) {
return defaultSeparator
}
Expand Down
57 changes: 50 additions & 7 deletions src/draw.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { createCanvas, registerFont, loadImage, deregisterAllFonts } = require('canvas')
const { barWatcher, formatDateTime, colorTip, color } = require('./util')
const Table = require('cli-table')
const { barWatcher, formatDateTime, colorTip, color, truncateString } = require('./util')
const HeaderCpu = require('./headerCpu')
const FooterCpu = require('./footerCpu')
const UnderLineCpu = require('./underlineCpu')
Expand Down Expand Up @@ -29,7 +30,6 @@ class Drawer {
from,
underline
} = anyPhotoConfig.canvasSetting
console.log(anyPhotoConfig)
deregisterAllFonts()
if (customFontPath) {
registerFont(customFontPath, {
Expand All @@ -38,7 +38,6 @@ class Drawer {
}
this.content = content
this.anyPhotoConfig = anyPhotoConfig
// separator
this.separator = this.anyPhotoConfig.separator
this.width = width
this.fontWeight = fontWeight
Expand Down Expand Up @@ -78,6 +77,47 @@ class Drawer {
this.underline = underline
}

async setupTable() {
const { separator, content, title, anyPhotoConfig, color, backgroundColor, width } = this
const {
canvasSetting: {
header: { headerAlign },
from: { name },
footer: { slogan }
}
} = anyPhotoConfig
const table = new Table({
style: { head: ['green'] },
head: [
'separator',
'header-align',
'image-width',
'title',
'content',
'background-color',
'content-color',
'from',
'slogan'
],
colWidths: [15, 15, 15, 20, 20, 20, 20, 20, 20]
})
table.push([
separator,
headerAlign,
width,
truncateString(title),
truncateString(content),
backgroundColor,
color,
truncateString(name),
truncateString(slogan)
])

console.log(table.toString())

return this
}

async setupCpu() {
const {
from = {},
Expand Down Expand Up @@ -150,9 +190,11 @@ class Drawer {
if (Array.isArray(backgroundColor)) {
const directionPoint = this.getLinearGradientDirection
gradientController = ctx.createLinearGradient(...directionPoint)
backgroundColor.forEach((color, index) => {
gradientController.addColorStop(index, color)
})
const backgroundColorLength = backgroundColor.length
const intervalSize = 1 / (backgroundColorLength - 1)
for (let i = 0; i < backgroundColorLength; i++) {
gradientController.addColorStop(i * intervalSize, backgroundColor[i])
}
}

ctx.textBaseline = textBaseline
Expand Down Expand Up @@ -756,7 +798,8 @@ const draw = async ({ content, anyPhotoConfig }) => {
const drawer = new Drawer({ content, anyPhotoConfig: handledAnyPhotoConfig })
return (
drawer
.setupCpu()
.setupTable()
.then(drawer => drawer.setupCpu())
.then(drawer => drawer.setupCanvas())
// .then(drawer => drawer.drawBackground())
.then(drawer => drawer.drawAvatar())
Expand Down
13 changes: 10 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ const generateOra = options => ora(options)

const sleep = time => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, time * 1000)
setTimeout(resolve, time * 1000)
})
}

Expand Down Expand Up @@ -72,6 +70,14 @@ const showAnyPhotoFiglet = () => {
console.log(color(figlet.textSync('AnyPhoto'), 'green', 'bold'))
}

const truncateString = (str, length = 7) => {
if (str.length > length) {
return str.substring(0, length) + '...'
} else {
return str
}
}

const barWatcher = new cliProgress.SingleBar({
format: 'Generate Progress ' + color('{bar}', 'green') + ' now status is => ' + color('{step}', 'green'),
barCompleteChar: '\u2588',
Expand All @@ -87,6 +93,7 @@ module.exports = {
colorTip,
checkRemoteFileExists,
showAnyPhotoFiglet,
truncateString,
barWatcher,
formatDateTime
}

0 comments on commit 315aacb

Please sign in to comment.