第一版demo

master
宇天 2021-11-29 16:05:25 +08:00
parent 57d199b7c0
commit 60d3ccaaaa
6 changed files with 146 additions and 8 deletions

2
.gitignore vendored
View File

@ -6,7 +6,7 @@
node_modules/ node_modules/
package-lock.json package-lock.json
run.js
# Thumbnails # Thumbnails
._* ._*

View File

@ -8,6 +8,9 @@
``` ```
![demo1.png](./img/demo1.png)
## 安装 ## 安装
```bash ```bash
npm i -g bash-calendar npm i -g bash-calendar

55
calendar.js Normal file
View File

@ -0,0 +1,55 @@
/**
* {日历}
* @author yutent<yutent.io@gmail.com>
* @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
}

BIN
img/demo1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -6,12 +6,90 @@
* @date 2021/11/26 17:20:02 * @date 2021/11/26 17:20:02
*/ */
function print(...args) { import chalk from 'chalk'
args[0] = args[0].padEnd(20, ' ') import figlet from 'figlet'
if (args.length > 1) { import { getThisYearMonth, getCalendarTable } from './calendar.js'
args.splice(1, 0, ' - ')
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)
}

View File

@ -2,13 +2,15 @@
"name": "bash-calendar", "name": "bash-calendar",
"description": "终端版万年历", "description": "终端版万年历",
"version": "0.0.1", "version": "0.0.1",
"type": "module",
"author": "yutent <yutent.io@gmail.com>", "author": "yutent <yutent.io@gmail.com>",
"bin": { "bin": {
"calendar": "index.js", "calendar": "index.js",
"cal": "index.js" "cal": "index.js"
}, },
"dependencies": { "dependencies": {
"chalk": "^4.0.0" "chalk": "^4.0.0",
"figlet": "^1.5.2"
}, },
"repository": { "repository": {
"type": "git", "type": "git",