增加导入自定义节假日的功能

master
宇天 2021-12-13 15:05:05 +08:00
parent 7d2d5841f0
commit 80d3bbe584
7 changed files with 90 additions and 13 deletions

View File

@ -27,6 +27,7 @@ npm i -g bash-calendar
* -m - 打印指定月份的日历
* -h - 查看帮助文档
* -v - 查看程序的版本
* -c - 导入自定义休假日, 必须是标准json格式, 语法看下面的示例
示例:
@ -37,10 +38,18 @@ cal -y 2000 # 打印指定年份的所有月份
cal -y 2021 5 # 打印指定年份, 指定月份
cal -y 2000 -m 3 # 同上
cal -m 5 # 打印当前年份的 指定月份
cal -c {"2021.12.11":"班","2021.12.12":"休"} # 导入自定义休假日
```
## 更新日志
### v1.2.0
* 增加导入自定义节假日的功能
* 修复日历中 1月份和12月份中的 上一个月/下一个月的节日显示
### v1.1.0
* 增加二四十节气的显示
### v1.0.2
* 修复当天农历是3个字时的排版异常
* 周末的农历增加暗红色显示

File diff suppressed because one or more lines are too long

View File

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

View File

@ -56,34 +56,60 @@ export function getCalendarTable(year, month) {
var nd = 0 // 最后一周需要补多少天, 具体看下方的计算
var today = getToday()
var list = []
var lyear, lmonth, nyear, nmonth
// 修正年月日的数值, 以匹配节假日
if (ld < 1) {
lyear = year
lmonth = month - 1
if (lmonth < 0) {
lmonth = 11
lyear--
}
}
for (let i = ld; i <= nums; i++) {
let tmp = {
day: i < 1 ? lnums - -i : (i + '').padStart(2, '0')
}
let lunar
if (i > 0) {
let week = getFirstDay(year, month, i)
let lunar = solar2lunar(year, month, i)
lunar = solar2lunar(year, month, i)
tmp.weekend = week === 0 || week === 6
tmp.picked = !!isPicked({ year, month, day: i }, today)
tmp.lunar = lunar.short
tmp.highlight = !!lunar.festival || !!lunar.solarTerms
} else {
// 从上个月中补齐第1周
lunar = solar2lunar(lyear, lmonth, lnums + i)
tmp.grey = 1
tmp.lunar = solar2lunar(year, month - 1, lnums + i).short
}
tmp.lunar = lunar.short
tmp.custom = lunar.custom
list.push(tmp)
}
nd = list.length % 7
nd = nd > 0 ? 7 - nd : 0
// 修正年月日的数值, 以匹配节假日
if (nd > 0) {
nyear = year
nmonth = month + 1
if (nmonth > 11) {
nmonth = 0
nyear++
}
}
// 最后一行不够1周时, 从下个月的日期中补齐
for (let day = 1; day <= nd; day++) {
let lunar = solar2lunar(nyear, nmonth, day)
list.push({
day: (day + '').padStart(2, '0'),
lunar: solar2lunar(year, month + 1, day).short,
lunar: lunar.short,
custom: lunar.custom,
grey: 1
})
}
@ -136,8 +162,12 @@ function drawTbody(year, month) {
break
case 1:
let right = ' '
if (tmp.custom) {
right = chalk.blue(tmp.custom)
}
if (tmp.picked) {
tr += ' ' + chalk.bgBlue.whiteBright.bold(' ' + tmp.day + ' ') + ' ' + VLINE
tr += ' ' + chalk.bgBlue.whiteBright.bold(' ' + tmp.day + ' ') + right + VLINE
} else {
// 有grey字段的, 优先置灰, 这种为 非本月份的日期
if (tmp.grey) {
@ -149,7 +179,7 @@ function drawTbody(year, month) {
tmp.day = chalk.whiteBright.bold(tmp.day)
}
}
tr += ' '.repeat(4) + tmp.day + ' '.repeat(4) + VLINE
tr += ' '.repeat(4) + tmp.day + ' ' + right + VLINE
}
break

View File

@ -6,7 +6,9 @@
* @date 2021/11/26 17:20:02
*/
import fs from 'fs'
import chalk from 'chalk'
import { CACHE_FILE } from './lunar/config.js'
import { getThisYearMonth, drawCalendar } from './calendar.js'
var version = process.env.APP_VERSION
@ -36,6 +38,7 @@ function print_help() {
print('Commands:')
print(' -y {year}', '打印指定年份的日历')
print(' -m', '打印指定月份的日历')
print(' -c', '导入自定义休假日, 必须是标准json格式, 语法看下面的示例')
print(' -h', '查看帮助文档')
print(' -v', '查看程序的版本\n')
print('示例: ')
@ -45,6 +48,7 @@ function print_help() {
print(' cal -y 2021 5 ' + chalk.grey('# 打印指定年份, 指定月份'))
print(' cal -y 2000 -m 3 ' + chalk.grey('# 同上'))
print(' cal -m 5 ' + chalk.grey('# 打印当前年份的 指定月份'))
print(' cal -c {"2021.12.11":"班","2021.12.12":"休"} ' + chalk.grey('# 导入自定义休假日'))
process.exit()
}
@ -107,6 +111,21 @@ switch (action) {
print_help()
break
case '-c':
let str = argvs.shift()
if (str) {
try {
JSON.parse(str)
fs.writeFileSync(CACHE_FILE, str)
console.log('导入成功')
} catch (e) {
console.log('错误的json数据')
}
} else {
console.log('导入的数据为空')
}
break
default:
if (action) {
year = +action

View File

@ -1,3 +1,5 @@
import { join } from 'path'
// 农历1900-2100年查询表
export const LUNAR_YEARS = [
0x04bd8, // 1900
@ -505,3 +507,5 @@ export const SOLAR_FESTIVALS = {
'12.24': '平安夜',
'12.25': '圣诞节'
}
export const CACHE_FILE = join(process.env.HOME, '.festivals.json')

View File

@ -4,6 +4,8 @@
* @date 2021/11/30 13:31:34
*/
import fs from 'fs'
import {
LUNAR_YEARS,
LUNAR_MONTH,
@ -14,9 +16,18 @@ import {
FESTIVALS,
SOLAR_FESTIVALS,
SOLAR_TERMS,
SOLAR_TERMS_YEARS
SOLAR_TERMS_YEARS,
CACHE_FILE
} from './config.js'
let CUSTOM_FESTIVALS = {}
if (fs.existsSync(CACHE_FILE)) {
try {
CUSTOM_FESTIVALS = JSON.parse(fs.readFileSync(CACHE_FILE))
} catch (e) {}
}
/**
* 公历转农历函数
* 传入公历{年月日}, 返回农历信息, 范围支持 1901/01/01 ~ 2100/12/31
@ -28,6 +39,7 @@ export function solar2lunar(year = 1901, month = 0, day = 1) {
var months, leap
var result = { short: '', solarTerms: '', festival: '', lunarFestival: '' }
var solarTermsYear = SOLAR_TERMS_YEARS[year - 1900]
var customKey = `${year}.${month + 1}.${day}`
if (year < 1901 || year > 2100) {
return result
@ -66,6 +78,9 @@ export function solar2lunar(year = 1901, month = 0, day = 1) {
}
}
// 自定义节假日
result.custom = CUSTOM_FESTIVALS[customKey] || ''
// 二十四节气
if (solarTermsYear) {
let tmp = solarTermsYear.slice(month * 4, (month + 1) * 4)