@zsc-glitch/habit-tracker 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +146 -18
- package/dist/index.js +7 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,48 +8,176 @@
|
|
|
8
8
|
npm install @zsc-glitch/habit-tracker
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
## 功能
|
|
12
|
+
|
|
13
|
+
### 核心功能
|
|
14
|
+
- ✅ **习惯记录** - 快速打卡,记录完成情况
|
|
15
|
+
- ✅ **连续追踪** - 自动计算 streak,激励坚持
|
|
16
|
+
- ✅ **目标设置** - 设置每周目标次数
|
|
17
|
+
- ✅ **统计报告** - 习惯完成率、趋势分析
|
|
18
|
+
- ✅ **打卡历史** - 日历视图,一目了然
|
|
19
|
+
|
|
20
|
+
### 新增功能 (v2.0)
|
|
21
|
+
- ✏️ **编辑习惯** - 修改名称、类别、频率、目标、图标、颜色
|
|
22
|
+
- 📦 **归档功能** - 暂停追踪但保留数据,随时恢复
|
|
23
|
+
- 🎨 **自定义外观** - 支持自定义颜色和图标
|
|
24
|
+
- 📅 **历史日历** - 可视化查看月度打卡记录
|
|
25
|
+
- 🔥 **改进的 Streak 算法** - 更准确地计算连续天数
|
|
26
|
+
|
|
11
27
|
## 使用
|
|
12
28
|
|
|
13
29
|
### 添加习惯
|
|
14
|
-
|
|
15
|
-
|
|
30
|
+
```
|
|
31
|
+
habit_add(name: string, category?: string, frequency?: string, goal?: number, emoji?: string, color?: string)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
示例:
|
|
35
|
+
- `habit_add({ name: "每天运动" })`
|
|
36
|
+
- `habit_add({ name: "阅读", category: "learning", emoji: "📖", color: "#3498db" })`
|
|
37
|
+
- `habit_add({ name: "冥想", frequency: "daily", goal: 7 })`
|
|
16
38
|
|
|
17
39
|
### 打卡
|
|
18
|
-
|
|
19
|
-
|
|
40
|
+
```
|
|
41
|
+
habit_checkin(name: string, note?: string)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
示例:
|
|
45
|
+
- `habit_checkin({ name: "运动" })`
|
|
46
|
+
- `habit_checkin({ name: "阅读", note: "读了30页" })`
|
|
20
47
|
|
|
21
48
|
### 查看状态
|
|
22
|
-
|
|
23
|
-
|
|
49
|
+
```
|
|
50
|
+
habit_status(name?: string, includeArchived?: boolean)
|
|
51
|
+
```
|
|
24
52
|
|
|
25
|
-
|
|
53
|
+
示例:
|
|
54
|
+
- `habit_status({})` - 查看所有活跃习惯
|
|
55
|
+
- `habit_status({ name: "运动" })` - 查看单个习惯
|
|
56
|
+
- `habit_status({ includeArchived: true })` - 包含已归档习惯
|
|
57
|
+
|
|
58
|
+
### 编辑习惯
|
|
59
|
+
```
|
|
60
|
+
habit_edit(name: string, newName?: string, category?: string, frequency?: string, goal?: number, emoji?: string, color?: string)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
示例:
|
|
64
|
+
- `habit_edit({ name: "运动", newName: "健身" })`
|
|
65
|
+
- `habit_edit({ name: "阅读", goal: 5, emoji: "📚" })`
|
|
66
|
+
|
|
67
|
+
### 查看历史
|
|
68
|
+
```
|
|
69
|
+
habit_history(name: string, year?: number, month?: number)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
示例:
|
|
73
|
+
- `habit_history({ name: "运动" })` - 本月历史
|
|
74
|
+
- `habit_history({ name: "运动", year: 2026, month: 2 })` - 指定月份
|
|
26
75
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
76
|
+
### 归档/恢复
|
|
77
|
+
```
|
|
78
|
+
habit_archive(name: string)
|
|
79
|
+
habit_unarchive(name: string)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
示例:
|
|
83
|
+
- `habit_archive({ name: "运动" })` - 归档习惯
|
|
84
|
+
- `habit_unarchive({ name: "运动" })` - 恢复追踪
|
|
31
85
|
|
|
32
|
-
|
|
86
|
+
### 生成报告
|
|
87
|
+
```
|
|
88
|
+
habit_report(period: "week" | "month" | "year")
|
|
89
|
+
```
|
|
33
90
|
|
|
34
|
-
|
|
35
|
-
- `
|
|
36
|
-
- `
|
|
37
|
-
|
|
38
|
-
|
|
91
|
+
示例:
|
|
92
|
+
- `habit_report({ period: "week" })` - 本周报告
|
|
93
|
+
- `habit_report({ period: "month" })` - 本月报告
|
|
94
|
+
|
|
95
|
+
### 删除习惯
|
|
96
|
+
```
|
|
97
|
+
habit_delete(name: string, confirm?: boolean)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
示例:
|
|
101
|
+
- `habit_delete({ name: "运动" })` - 预览删除
|
|
102
|
+
- `habit_delete({ name: "运动", confirm: true })` - 确认删除
|
|
103
|
+
|
|
104
|
+
## 工具列表
|
|
105
|
+
|
|
106
|
+
| 工具 | 描述 |
|
|
107
|
+
|------|------|
|
|
108
|
+
| `habit_add` | 添加新习惯 |
|
|
109
|
+
| `habit_checkin` | 习惯打卡 |
|
|
110
|
+
| `habit_status` | 查看状态 |
|
|
111
|
+
| `habit_edit` | 编辑习惯 |
|
|
112
|
+
| `habit_history` | 查看打卡历史日历 |
|
|
113
|
+
| `habit_archive` | 归档习惯 |
|
|
114
|
+
| `habit_unarchive` | 恢复习惯 |
|
|
115
|
+
| `habit_report` | 生成报告 |
|
|
116
|
+
| `habit_delete` | 删除习惯 |
|
|
117
|
+
|
|
118
|
+
## 类别
|
|
119
|
+
|
|
120
|
+
| 类别 | ID | 默认图标 | 默认颜色 |
|
|
121
|
+
|------|-----|---------|---------|
|
|
122
|
+
| 健康 | health | 💪 | #4CAF50 |
|
|
123
|
+
| 学习 | learning | 📚 | #2196F3 |
|
|
124
|
+
| 心理 | mindfulness | 🧘 | #9C27B0 |
|
|
125
|
+
| 生活 | life | 🏠 | #FF9800 |
|
|
126
|
+
| 自定义 | custom | ⭐ | #607D8B |
|
|
39
127
|
|
|
40
128
|
## 数据存储
|
|
41
129
|
|
|
130
|
+
习惯数据存储在 `~/.habit-tracker/` 目录:
|
|
131
|
+
|
|
42
132
|
```
|
|
43
133
|
~/.habit-tracker/
|
|
44
134
|
├── habits.json # 习惯配置
|
|
45
135
|
├── checkins/ # 打卡记录
|
|
136
|
+
│ ├── 2026-03-30.json
|
|
137
|
+
│ └── ...
|
|
46
138
|
└── stats.json # 统计数据
|
|
47
139
|
```
|
|
48
140
|
|
|
141
|
+
## Streak 计算规则
|
|
142
|
+
|
|
143
|
+
### 每日习惯
|
|
144
|
+
- 最近一次打卡必须是今天或昨天
|
|
145
|
+
- 从最近一次打卡往前计算连续天数
|
|
146
|
+
- 断开一天,streak 重置为 0
|
|
147
|
+
|
|
148
|
+
### 每周习惯
|
|
149
|
+
- 最近一次打卡必须在最近 7 天内
|
|
150
|
+
- 每次打卡间隔不超过 7 天即视为连续
|
|
151
|
+
|
|
152
|
+
## 更新日志
|
|
153
|
+
|
|
154
|
+
### v2.0.0
|
|
155
|
+
- ✨ 新增 `habit_edit` 编辑习惯功能
|
|
156
|
+
- ✨ 新增 `habit_history` 查看打卡历史日历
|
|
157
|
+
- ✨ 新增 `habit_archive` / `habit_unarchive` 归档功能
|
|
158
|
+
- 🎨 支持自定义颜色和图标
|
|
159
|
+
- 🔥 改进 streak 计算算法,处理边界情况
|
|
160
|
+
- 📊 报告增加日均打卡数据
|
|
161
|
+
- 🐛 修复 streak 计算错误
|
|
162
|
+
- 🐛 修复归档习惯仍可打卡的问题
|
|
163
|
+
|
|
164
|
+
### v1.0.0
|
|
165
|
+
- 🎉 初始版本
|
|
166
|
+
- 基础习惯追踪功能
|
|
167
|
+
- streak 计算和报告生成
|
|
168
|
+
|
|
49
169
|
## License
|
|
50
170
|
|
|
51
171
|
MIT
|
|
52
172
|
|
|
53
173
|
---
|
|
54
174
|
|
|
55
|
-
|
|
175
|
+
## ☕ 支持开发者
|
|
176
|
+
|
|
177
|
+
如果这些工具对你有帮助,欢迎请我喝杯咖啡 ☕
|
|
178
|
+
|
|
179
|
+

|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
Made with 🔥 by [小影](https://github.com/zsc-glitch)
|
package/dist/index.js
CHANGED
|
@@ -105,28 +105,29 @@ async function calculateStreak(dataDir, habitId) {
|
|
|
105
105
|
const today = getTodayDate();
|
|
106
106
|
const yesterday = getYesterdayDate();
|
|
107
107
|
let hasRecentCheckin = false;
|
|
108
|
+
let inCurrentStreak = false;
|
|
108
109
|
for (const date of dates) {
|
|
109
110
|
const checkins = await loadCheckins(dataDir, date);
|
|
110
111
|
const hasCheckin = checkins.some(c => c.habitId === habitId);
|
|
111
112
|
if (hasCheckin) {
|
|
112
113
|
if (date === today || date === yesterday) {
|
|
113
114
|
hasRecentCheckin = true;
|
|
115
|
+
inCurrentStreak = true;
|
|
116
|
+
tempStreak = 1;
|
|
117
|
+
}
|
|
118
|
+
else if (inCurrentStreak) {
|
|
119
|
+
tempStreak++;
|
|
114
120
|
}
|
|
115
|
-
tempStreak++;
|
|
116
121
|
if (tempStreak > longestStreak) {
|
|
117
122
|
longestStreak = tempStreak;
|
|
118
123
|
}
|
|
119
124
|
}
|
|
120
125
|
else {
|
|
121
|
-
|
|
122
|
-
if (tempStreak > 0 && !hasRecentCheckin) {
|
|
123
|
-
// 如果已经断开了,停止计算当前 streak
|
|
126
|
+
if (inCurrentStreak) {
|
|
124
127
|
break;
|
|
125
128
|
}
|
|
126
|
-
tempStreak = 0;
|
|
127
129
|
}
|
|
128
130
|
}
|
|
129
|
-
// 当前 streak 从今天/昨天开始计算
|
|
130
131
|
if (hasRecentCheckin) {
|
|
131
132
|
currentStreak = tempStreak;
|
|
132
133
|
}
|