2021-11-26 17:24:37 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {终端版万年历}
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2021/11/26 17:20:02
|
|
|
|
*/
|
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
import fs from 'iofs'
|
|
|
|
import { getThisYearMonth, drawCalendar } from './calendar.js'
|
2021-11-29 16:05:25 +08:00
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
var { version } = JSON.parse(fs.cat('./package.json'))
|
|
|
|
var argvs = process.argv.slice(2)
|
|
|
|
var action = argvs.shift()
|
2021-11-29 16:05:25 +08:00
|
|
|
var [year, month] = getThisYearMonth()
|
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
function drawOneYear(y) {
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
drawCalendar(y, i)
|
|
|
|
}
|
2021-11-26 17:24:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
function print(...args) {
|
|
|
|
args[0] = args[0].padEnd(20, ' ')
|
|
|
|
if (args.length > 1) {
|
|
|
|
args.splice(1, 0, ' - ')
|
|
|
|
}
|
|
|
|
console.log.apply(null, args)
|
|
|
|
}
|
2021-11-29 16:05:25 +08:00
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
function print_help() {
|
|
|
|
print('='.repeat(64))
|
|
|
|
print(`终端版万年历 v${version}, 作者: 宇天`)
|
|
|
|
print('='.repeat(64))
|
|
|
|
print('用法: cal [command] args...')
|
|
|
|
print('Commands:')
|
|
|
|
print(' -y {year}', '打印指定年份的日历')
|
|
|
|
print(' -m', '打印指定月份的日历')
|
|
|
|
print(' -h', '查看帮助文档')
|
|
|
|
print(' -v', '查看程序的版本')
|
|
|
|
process.exit()
|
|
|
|
}
|
2021-11-29 16:05:25 +08:00
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
switch (action) {
|
|
|
|
case '-y':
|
|
|
|
switch (argvs.length) {
|
|
|
|
// 再无其他参数, 由打印当前年份所有的日历
|
2021-11-29 16:05:25 +08:00
|
|
|
case 0:
|
2021-11-29 19:53:02 +08:00
|
|
|
drawOneYear(year)
|
2021-11-29 16:05:25 +08:00
|
|
|
break
|
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
// 有1~2个参数, cal -y 2020 2, cal -y 2020 5
|
2021-11-29 16:05:25 +08:00
|
|
|
case 1:
|
2021-11-29 19:53:02 +08:00
|
|
|
case 2:
|
|
|
|
year = +argvs.shift()
|
|
|
|
month = +argvs.shift()
|
|
|
|
|
|
|
|
if (year === year) {
|
|
|
|
if (month < 13 && month > 0) {
|
|
|
|
drawCalendar(year, month - 1)
|
2021-11-29 16:05:25 +08:00
|
|
|
} else {
|
2021-11-29 19:53:02 +08:00
|
|
|
drawOneYear(year)
|
2021-11-29 16:05:25 +08:00
|
|
|
}
|
2021-11-29 19:53:02 +08:00
|
|
|
} else {
|
|
|
|
console.log('-y 参数异常')
|
2021-11-29 16:05:25 +08:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
// 3个参数 cal -y 2020 -m 1
|
|
|
|
case 3:
|
|
|
|
action = argvs.shift()
|
|
|
|
month = +argvs.shift()
|
|
|
|
if (action === '-m' && month < 13 && month > 0) {
|
|
|
|
drawCalendar(year, month - 1)
|
2021-11-29 16:05:25 +08:00
|
|
|
} else {
|
2021-11-29 19:53:02 +08:00
|
|
|
console.log('-m 参数异常')
|
2021-11-29 16:05:25 +08:00
|
|
|
}
|
|
|
|
break
|
2021-11-29 19:53:02 +08:00
|
|
|
default:
|
|
|
|
console.log('-y 参数异常')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
case '-m':
|
|
|
|
month = +argvs.shift()
|
|
|
|
if (month < 13 && month > 0) {
|
|
|
|
drawCalendar(year, month - 1)
|
|
|
|
} else {
|
|
|
|
console.log('-m 参数异常')
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case '-v':
|
|
|
|
print(version)
|
|
|
|
process.exit()
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (action) {
|
|
|
|
year = +action
|
|
|
|
month = +argvs.shift()
|
|
|
|
if (year === year) {
|
|
|
|
if (month < 13 && month > 0) {
|
|
|
|
drawCalendar(year, month - 1)
|
|
|
|
} else {
|
|
|
|
drawOneYear(year)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('参数异常')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
drawCalendar(year, month)
|
2021-11-29 16:05:25 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 19:53:02 +08:00
|
|
|
break
|
2021-11-29 16:05:25 +08:00
|
|
|
}
|