/** * 第 10 章 · 实战项目 — Todo 数据模型 */ import kotlinx.serialization.Serializable @Serializable enum class Priority { LOW, MID, HIGH } @Serializable data class Todo( val id: Int, val title: String, val priority: Priority = Priority.MID, val due: String? = null, val done: Boolean = false, val createdAt: Long = System.currentTimeMillis(), ) /** * 美化打印一个 todo。 * - [✓] 表示已完成(带删除线) * - [ ] 表示未完成 * - 优先级用方括号 * - 截止日期可选 */ fun Todo.format(): String { val mark = if (done) "✓" else " " val prio = "[${priority.name.padEnd(4)}]" val displayTitle = if (done) "\u001B[9m${title}\u001B[0m" else title val dueStr = due?.let { " (截止 $it)" } ?: "" return "[$mark] #$id $prio $displayTitle$dueStr" } fun List.summary(): String { val total = size val doneCount = count { it.done } val highPrio = count { it.priority == Priority.HIGH && !it.done } val pct = if (total > 0) doneCount * 100.0 / total else 0.0 return "总数: $total | 已完成: $doneCount (${"%.1f".format(pct)}%) | 高优待办: $highPrio" }