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

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

File diff suppressed because one or more lines are too long

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
import { join } from 'path'
// 农历1900-2100年查询表 // 农历1900-2100年查询表
export const LUNAR_YEARS = [ export const LUNAR_YEARS = [
0x04bd8, // 1900 0x04bd8, // 1900
@ -505,3 +507,5 @@ export const SOLAR_FESTIVALS = {
'12.24': '平安夜', '12.24': '平安夜',
'12.25': '圣诞节' '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 * @date 2021/11/30 13:31:34
*/ */
import fs from 'fs'
import { import {
LUNAR_YEARS, LUNAR_YEARS,
LUNAR_MONTH, LUNAR_MONTH,
@ -14,9 +16,18 @@ import {
FESTIVALS, FESTIVALS,
SOLAR_FESTIVALS, SOLAR_FESTIVALS,
SOLAR_TERMS, SOLAR_TERMS,
SOLAR_TERMS_YEARS SOLAR_TERMS_YEARS,
CACHE_FILE
} from './config.js' } 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 * 传入公历{年月日}, 返回农历信息, 范围支持 1901/01/01 ~ 2100/12/31
@ -28,6 +39,7 @@ export function solar2lunar(year = 1901, month = 0, day = 1) {
var months, leap var months, leap
var result = { short: '', solarTerms: '', festival: '', lunarFestival: '' } var result = { short: '', solarTerms: '', festival: '', lunarFestival: '' }
var solarTermsYear = SOLAR_TERMS_YEARS[year - 1900] var solarTermsYear = SOLAR_TERMS_YEARS[year - 1900]
var customKey = `${year}.${month + 1}.${day}`
if (year < 1901 || year > 2100) { if (year < 1901 || year > 2100) {
return result return result
@ -66,6 +78,9 @@ export function solar2lunar(year = 1901, month = 0, day = 1) {
} }
} }
// 自定义节假日
result.custom = CUSTOM_FESTIVALS[customKey] || ''
// 二十四节气 // 二十四节气
if (solarTermsYear) { if (solarTermsYear) {
let tmp = solarTermsYear.slice(month * 4, (month + 1) * 4) let tmp = solarTermsYear.slice(month * 4, (month + 1) * 4)