修复参数和颜色

master
yutent 2022-12-28 16:00:59 +08:00
parent bd76a28c61
commit d053f17e4d
4 changed files with 41 additions and 29 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,8 @@
{ {
"type": "module",
"name": "bash-calendar", "name": "bash-calendar",
"description": "终端版万年历", "description": "终端版万年历",
"version": "1.2.4", "version": "1.2.5",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",
"bin": { "bin": {
"calendar": "index.js", "calendar": "index.js",

View File

@ -12,14 +12,16 @@ const CAL_HEAD = ['日', '一', '二', '三', '四', '五', '六'].map((s, i) =>
if (i === 0 || i === 6) { if (i === 0 || i === 6) {
s = chalk.red(s) s = chalk.red(s)
} }
return ' ' + s + ' ' + chalk.grey('|') return ' ' + s + ' ' + chalk.grey('')
}) })
const VLINE = chalk.grey('|') const VLINE = chalk.grey('│')
const DASHED_LINE = chalk.grey('|' + (' '.repeat(10) + '|').repeat(7)) const VLINE2 = chalk.grey('├')
const VLINE3 = chalk.grey('└')
const DASHED_LINE = chalk.grey('│' + (' '.repeat(10) + '│').repeat(7))
// 画间隔线 // 画间隔线
function drawDashedLine(start = '', pipe = ' ') { function drawDashedLine(start = '', pipe = ' ') {
return chalk.grey(start + (pipe.repeat(10) + '|').repeat(7)) return chalk.grey(start + (pipe.repeat(10) + '').repeat(7))
} }
//获取今年的年份/月份,返回的是数组 //获取今年的年份/月份,返回的是数组
@ -121,23 +123,27 @@ function drawThead(year, month) {
var dateStr = `${year}${month + 1}${' '.repeat(10)}${getHSEBYear(year, month)}` var dateStr = `${year}${month + 1}${' '.repeat(10)}${getHSEBYear(year, month)}`
dateStr = dateStr =
chalk.grey('| ') + chalk.cyan(dateStr) + ' '.repeat(71 - dateStr.length - 2) + chalk.grey('|') chalk.grey(' ') + chalk.cyan(dateStr) + ' '.repeat(71 - dateStr.length - 2) + chalk.grey('')
console.log(chalk.grey(' ' + '_'.repeat(76))) console.log(chalk.grey('┌─' + '─'.repeat(75) + '┐'))
console.log(chalk.grey('|' + ' '.repeat(76) + '|')) console.log(chalk.grey('' + ' '.repeat(76) + ''))
console.log(dateStr) console.log(dateStr)
console.log(chalk.grey('|' + '_'.repeat(76) + '|')) console.log(chalk.grey('│' + ' '.repeat(76) + '│'))
console.log(drawDashedLine('|')) console.log(chalk.grey('├' + '──────────┬'.repeat(7).slice(0, -1) + '┤'))
console.log(chalk.grey('|') + CAL_HEAD.join('')) // 以下是星期的表头
console.log(drawDashedLine('|', '_')) console.log(chalk.grey('│') + CAL_HEAD.join(''))
console.log(chalk.grey('├' + '──────────┼'.repeat(7).slice(0, -1) + '┤'))
// ┘└ ┬ ┴ ├ ┤ ┼
} }
// 画日期 // 画日期
function drawTbody(year, month) { function drawTbody(year, month) {
var table = getCalendarTable(year, month) var table = getCalendarTable(year, month)
var line = 0 var line = 0
var maxi = 3 * Math.ceil(table.length / 7)
for (let i = 0; i < 3 * Math.ceil(table.length / 7) + 1; i++) { // i 是 纵坐标, j是横坐标格子
for (let i = 0; i < maxi + 1; i++) {
let tr = '' let tr = ''
for (let j = 0; j < 7; j++) { for (let j = 0; j < 7; j++) {
let tmp = table[line + j] let tmp = table[line + j]
@ -146,15 +152,21 @@ function drawTbody(year, month) {
break break
} }
if (i === 0 && j === 0) {
continue
}
if (j === 0) { if (j === 0) {
tr += VLINE tr += i % 3 === 0 ? (i === maxi ? VLINE3 : VLINE2) : VLINE
} }
switch (i % 3) { switch (i % 3) {
case 0: case 0:
if (i === 0) { if (i === 0) {
tr += chalk.grey(' '.repeat(10) + '|') break
} else { } else {
tr += chalk.grey('-'.repeat(j === 6 ? 10 : 11) + (j === 6 ? '|' : '')) tr += chalk.grey(
'──────────' + (j === 6 ? (i === maxi ? '┘' : '┤') : i === maxi ? '┴' : '┼')
)
if (j === 6) { if (j === 6) {
line += 7 line += 7
} }
@ -167,20 +179,20 @@ function drawTbody(year, month) {
if (tmp.custom === '班') { if (tmp.custom === '班') {
right = chalk.red(tmp.custom) right = chalk.red(tmp.custom)
} else { } else {
right = chalk.green.dim(tmp.custom) right = chalk.green(tmp.custom)
} }
} }
if (tmp.picked) { if (tmp.picked) {
tr += ' ' + chalk.bgBlue.whiteBright.bold(' ' + tmp.day + ' ') + right + VLINE tr += ' ' + chalk.bgBlue.whiteBright(' ' + tmp.day + ' ') + right + VLINE
} else { } else {
// 有grey字段的, 优先置灰, 这种为 非本月份的日期 // 有grey字段的, 优先置灰, 这种为 非本月份的日期
if (tmp.grey) { if (tmp.grey) {
tmp.day = chalk.grey(tmp.day) tmp.day = chalk.grey(tmp.day)
} else { } else {
if (tmp.weekend) { if (tmp.weekend) {
tmp.day = chalk.redBright.bold(tmp.day) tmp.day = chalk.red(tmp.day)
} else { } else {
tmp.day = chalk.whiteBright.bold(tmp.day) tmp.day = chalk.whiteBright(tmp.day)
} }
} }
tr += ' '.repeat(4) + tmp.day + ' ' + right + VLINE tr += ' '.repeat(4) + tmp.day + ' ' + right + VLINE
@ -200,13 +212,13 @@ function drawTbody(year, month) {
padSpace = padSpace.slice(0, pad) padSpace = padSpace.slice(0, pad)
} }
if (tmp.picked) { if (tmp.picked) {
tr += padSpace + chalk.bgBlue.white.bold(space + tmp.lunar + space) + padSpace + VLINE tr += padSpace + chalk.bgBlue.white(space + tmp.lunar + space) + padSpace + VLINE
} else { } else {
if (tmp.lunar) { if (tmp.lunar) {
tmp.lunar = tmp.highlight tmp.lunar = tmp.highlight
? chalk.cyan.dim(tmp.lunar) ? chalk.cyan(tmp.lunar)
: tmp.weekend : tmp.weekend
? chalk.red.dim(tmp.lunar) ? chalk.redBright(tmp.lunar)
: chalk.grey(tmp.lunar) : chalk.grey(tmp.lunar)
} }
tr += ' '.repeat(pad + 2) + tmp.lunar + ' '.repeat(pad + 2) + VLINE tr += ' '.repeat(pad + 2) + tmp.lunar + ' '.repeat(pad + 2) + VLINE
@ -215,7 +227,9 @@ function drawTbody(year, month) {
} }
} }
console.log(tr) if (tr) {
console.log(tr)
}
} }
console.log('') // 再打印一个空行, 用于分隔多个日历 console.log('') // 再打印一个空行, 用于分隔多个日历
} }

3
src/index.js Normal file → Executable file
View File

@ -79,8 +79,9 @@ switch (action) {
// 3个参数 cal -y 2020 -m 1 // 3个参数 cal -y 2020 -m 1
case 3: case 3:
year = +argvs.shift()
action = argvs.shift() action = argvs.shift()
month = +argvs.shift() month = +argvs.pop()
if (action === '-m' && month < 13 && month > 0) { if (action === '-m' && month < 13 && month > 0) {
drawCalendar(year, month - 1) drawCalendar(year, month - 1)
} else { } else {