1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
| local ui = {}
local contact = require("contact")
function ui.clear_screen() os.execute("cls || clear") end
function ui.separator(char) char = char or "=" print(string.rep(char, 50)) end
function ui.pause() io.write("按 Enter 键继续...") io.read() end
function ui.input(prompt, default) io.write(prompt) local input = io.read() if input and #input > 0 then return input end return default end
function ui.input_number(prompt, min, max) while true do io.write(prompt) local input = io.read() local num = tonumber(input) if num and num >= min and num <= max then return num end print(string.format("请输入 %d 到 %d 之间的数字", min, max)) end end
local function format_contact(c, index) local idx = index or c.id print(string.format("%-3d | %-10s | %-15s | %-25s | %-8s", idx, c.name or "", c.phone or "", c.email or "", c.group or "" )) end
local function print_table_header() ui.separator("-") print(string.format("%-3s | %-10s | %-15s | %-25s | %-8s", "ID", "姓名", "电话", "邮箱", "分组")) ui.separator("-") end
function ui.print_contacts(contacts, title) ui.clear_screen() print(title or "联系人列表") print_table_header() if #contacts == 0 then print("暂无联系人") else for _, c in ipairs(contacts) do format_contact(c) end end ui.separator("-") print(string.format("共 %d 条记录", #contacts)) end
function ui.show_all_contacts() local contacts = contact.list() ui.print_contacts(contacts, "所有联系人") ui.pause() end
function ui.search_contacts() ui.clear_screen() print("搜索联系人") ui.separator("-") print("1. 按姓名搜索") print("2. 按电话搜索") print("3. 按分组搜索") print("4. 全局搜索") print("0. 返回") ui.separator("-") local choice = ui.input_number("请选择搜索方式: ", 0, 4) if choice == 0 then return end local field_map = {["1"] = "name", ["2"] = "phone", ["3"] = "group", ["4"] = "all"} local keyword = ui.input("请输入搜索关键词: ") if not keyword or #keyword == 0 then print("关键词不能为空") ui.pause() return end local results = contact.search(keyword, field_map[tostring(choice)]) ui.print_contacts(results, "搜索结果") ui.pause() end
function ui.add_contact() ui.clear_screen() print("添加联系人") ui.separator("-") local name = ui.input("姓名: ") if not name or #name == 0 then print("姓名不能为空,取消添加") ui.pause() return end local phone = ui.input("电话: ") if not phone or #phone == 0 then print("电话不能为空,取消添加") ui.pause() return end local email = ui.input("邮箱(可选): ") local group = ui.input("分组(可选): ") local ok, result = contact.add(name, phone, email, group) if ok then print(string.format("添加成功!联系人 ID 为 %d", result.id)) else print("添加失败: " .. result) end ui.pause() end
function ui.edit_contact() local id = tonumber(ui.input("请输入要修改的联系人 ID: ")) if not id then print("无效的 ID") ui.pause() return end local c = contact.find_by_id(id) if not c then print("联系人不存在") ui.pause() return end print("当前信息:") format_contact(c) ui.separator("-") print("留空表示不修改") local name = ui.input(string.format("姓名 [%s]: ", c.name), c.name) local phone = ui.input(string.format("电话 [%s]: ", c.phone), c.phone) local email = ui.input(string.format("邮箱 [%s]: ", c.email), c.email) local group = ui.input(string.format("分组 [%s]: ", c.group), c.group) local ok, result = contact.update(id, { name = name, phone = phone, email = email, group = group }) if ok then print("修改成功!") format_contact(result) else print("修改失败: " .. result) end ui.pause() end
function ui.delete_contact() local id = tonumber(ui.input("请输入要删除的联系人 ID: ")) if not id then print("无效的 ID") ui.pause() return end local c = contact.find_by_id(id) if not c then print("联系人不存在") ui.pause() return end print("即将删除以下联系人:") format_contact(c) local confirm = ui.input("确认删除?(y/n): ") if confirm ~= "y" and confirm ~= "Y" then print("已取消") ui.pause() return end local ok, result = contact.delete(id) if ok then print(string.format("已删除联系人: %s (%s)", result.name, result.phone)) else print("删除失败: " .. result) end ui.pause() end
function ui.show_groups() ui.clear_screen() print("分组信息") ui.separator("-") local groups = contact.get_groups() local stats = contact.get_group_stats() if #groups == 0 then print("暂无分组") else print(string.format("%-15s | %-5s", "分组名称", "人数")) ui.separator("-") table.sort(groups) for _, g in ipairs(groups) do print(string.format("%-15s | %-5d", g, stats[g] or 0)) end ui.separator("-") print(string.format("总计: %d 个分组, %d 位联系人", #groups, contact.count())) end ui.pause() end
function ui.show_contacts_by_group() local groups = contact.get_groups() if #groups == 0 then print("暂无分组") ui.pause() return end ui.clear_screen() print("选择分组") ui.separator("-") for i, g in ipairs(groups) do print(string.format("%d. %s", i, g)) end print("0. 返回") ui.separator("-") local choice = ui.input_number("请选择: ", 0, #groups) if choice == 0 then return end local group_name = groups[choice] local contacts = contact.list(group_name) ui.print_contacts(contacts, "分组: " .. group_name) ui.pause() end
return ui
|