diff --git a/.gitignore b/.gitignore index ce19c5e..476a30b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ node_modules/ package-lock.json - +run.js # Thumbnails ._* diff --git a/Readme.md b/Readme.md index 0d9b9ba..b49c3ca 100644 --- a/Readme.md +++ b/Readme.md @@ -8,6 +8,9 @@ ``` +![demo1.png](./img/demo1.png) + + ## 安装 ```bash npm i -g bash-calendar diff --git a/calendar.js b/calendar.js new file mode 100644 index 0000000..81a8f79 --- /dev/null +++ b/calendar.js @@ -0,0 +1,55 @@ +/** + * {日历} + * @author yutent + * @date 2021/11/29 09:59:26 + */ + +//获取今年的年份/月份,返回的是数组 +export function getThisYearMonth() { + var d = new Date() + return [d.getFullYear(), d.getMonth()] +} + +//根据年份获取指定月份天数 +function getTotalDays(year, month) { + return new Date(year, month + 1, 0).getDate() +} + +//判断指定年月第一天是星期几 +function getFirstDay(year, month, day) { + return new Date(year, month, day || 1).getDay() +} + +//判断指定天数是否被选中 +function isPicked({ year, month, day }, item) { + return item.year === year && item.month === month && item.day === day +} + +function getToday() { + var d = new Date() + return { year: d.getFullYear(), month: d.getMonth(), day: d.getDate() } +} + +//计算日历数组 +export function getCalendarTable(year, month) { + var nums = getTotalDays(year, month) + var numsFixed = 1 - getFirstDay(year, month) + var today = getToday() + var list = [] + + for (let i = numsFixed; i <= nums; i++) { + let day = { + day: i < 1 ? ' ' : (i + '').padStart(2, '0') + } + if (i > 0) { + let week = getFirstDay(year, month, i) + day.weekend = week === 0 || week === 6 + day.picked = !!isPicked({ year, month, day: i }, today) + } + list.push(day) + } + while (list.length % 6 !== 0) { + list.push({ day: ' ' }) + } + return list +} diff --git a/img/demo1.png b/img/demo1.png new file mode 100644 index 0000000..d750087 Binary files /dev/null and b/img/demo1.png differ diff --git a/index.js b/index.js index acfd639..0f83076 100644 --- a/index.js +++ b/index.js @@ -6,12 +6,90 @@ * @date 2021/11/26 17:20:02 */ -function print(...args) { - args[0] = args[0].padEnd(20, ' ') - if (args.length > 1) { - args.splice(1, 0, ' - ') +import chalk from 'chalk' +import figlet from 'figlet' +import { getThisYearMonth, getCalendarTable } from './calendar.js' + +const CAL_HEAD = ['日', '一', '二', '三', '四', '五', '六'].map((s, i) => { + if (i === 0 || i === 6) { + s = chalk.red.bold(s) + } else { + s = chalk.bold(s) } - console.log.apply(null, args) + return ' '.repeat(4) + s + ' '.repeat(4) + chalk.grey('|') +}) +const VLINE = chalk.grey('|') +const DASHED_LINE = chalk.grey('|' + (' '.repeat(10) + '|').repeat(7)) + +var [year, month] = getThisYearMonth() +var table = getCalendarTable(year, month) +var line = 0 +var dateStr = figlet.textSync(`${year} . ${month + 1}`) + +function drawDashedLine(start = '', pipe = ' ') { + return chalk.grey(start + (pipe.repeat(10) + '|').repeat(7)) } -print('Hello calendar!') +dateStr = dateStr + .split('\n') + .map(s => chalk.grey('| ') + chalk.cyan(s) + ' '.repeat(77 - s.length - 2) + chalk.grey('|')) + .slice(0, -1) + .join('\n') + +console.log(chalk.grey(' ' + '_'.repeat(76))) +console.log(dateStr) +console.log(chalk.grey('|' + '_'.repeat(76) + '|')) +console.log(drawDashedLine('|')) +console.log(chalk.grey('|') + CAL_HEAD.join('')) +console.log(drawDashedLine('|', '_')) + +// 渲染日历表格 +for (let i = 0; i < 3 * 5 + 1; i++) { + let tr = '' + for (let j = 0; j < 7; j++) { + let tmp = table[line + j] + + if (!tmp) { + break + } + + if (j === 0) { + tr += VLINE + } + switch (i % 3) { + case 0: + if (i === 0) { + tr += chalk.grey(' '.repeat(10) + '|') + } else { + tr += chalk.grey('-'.repeat(j === 6 ? 10 : 11) + (j === 6 ? '|' : '')) + if (j === 6) { + line += 7 + } + } + break + + case 1: + if (tmp.picked) { + tr += chalk.bgRed.whiteBright.bold(' '.repeat(4) + tmp.day + ' '.repeat(4)) + VLINE + } else { + if (tmp.weekend) { + tmp.day = chalk.redBright(tmp.day) + } else { + tmp.day = chalk.whiteBright(tmp.day) + } + tr += ' '.repeat(4) + tmp.day + ' '.repeat(4) + VLINE + } + break + + case 2: + if (tmp.picked) { + tr += chalk.bgRed(' '.repeat(10)) + VLINE + } else { + tr += ' '.repeat(10) + VLINE + } + break + } + } + + console.log(tr) +} diff --git a/package.json b/package.json index 1febf93..59647f5 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,15 @@ "name": "bash-calendar", "description": "终端版万年历", "version": "0.0.1", + "type": "module", "author": "yutent ", "bin": { "calendar": "index.js", "cal": "index.js" }, "dependencies": { - "chalk": "^4.0.0" + "chalk": "^4.0.0", + "figlet": "^1.5.2" }, "repository": { "type": "git",