pku-treehole 0.1.2

北大树洞 CLI 客户端
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! 终端格式化输出

use crate::api::{
    ActivityEvent, ClassTime, Comment, CourseRow, Hole, HoleListItem, LabEvent, Message,
    ScoreData, ScheduleItem, UserInfo,
};
use crate::colorize;
use colored::Colorize;

/// 格式化时间戳为人类可读
pub fn fmt_time(ts: i64) -> String {
    chrono::DateTime::from_timestamp(ts, 0)
        .map(|dt| {
            let local = dt.with_timezone(&chrono::Local);
            let now = chrono::Local::now();
            let diff = now.signed_duration_since(local);

            let relative = if diff.num_seconds() < 60 {
                "刚刚".to_string()
            } else if diff.num_minutes() < 60 {
                format!("{}分钟前", diff.num_minutes())
            } else if diff.num_hours() < 24 {
                format!("{}小时前", diff.num_hours())
            } else if diff.num_days() < 7 {
                format!("{}天前", diff.num_days())
            } else {
                local.format("%m-%d %H:%M").to_string()
            };
            relative
        })
        .unwrap_or_else(|| "?".to_string())
}

/// 截断文本到指定宽度(按字符计数,中文算 2 宽度)
fn truncate_display(s: &str, max_width: usize) -> String {
    let mut width = 0;
    let mut result = String::new();
    for ch in s.chars() {
        let w = if ch.is_ascii() { 1 } else { 2 };
        if width + w > max_width {
            result.push_str("...");
            break;
        }
        result.push(ch);
        width += w;
    }
    result
}

/// 图片数量提示
fn media_badge(media_ids: &str) -> String {
    if media_ids.is_empty() {
        return String::new();
    }
    let count = media_ids.split(',').filter(|s| !s.is_empty()).count();
    if count == 1 {
        format!(" {}", "[图片]".magenta())
    } else {
        format!(" {}", format!("[{count}张图片]").magenta())
    }
}

/// 打印帖子列表中的一项
pub fn print_hole_item(item: &HoleListItem) {
    print_hole_item_header(item);

    // 正文预览
    let text = item.text.replace('\n', " ");
    let preview = truncate_display(&text, 80);
    print!("  {}", preview);
    println!("{}", media_badge(&item.media_ids));

    // 评论预览
    for c in item.comment_list.iter().take(3) {
        let tag = format!("[{}]", c.name_tag);
        let ctext = c.text.replace('\n', " ");
        let cpreview = truncate_display(&ctext, 60);
        println!("    {} {}", tag.dimmed(), cpreview.dimmed());
    }
    if item.reply > 3 {
        println!(
            "    {}",
            format!("... 共 {} 条评论", item.reply).dimmed()
        );
    }
    println!();
}

fn print_hole_item_header(h: &HoleListItem) {
    let pid = format!("#{}", h.pid).bold().cyan();
    let time = fmt_time(h.timestamp).dimmed();

    let mut badges = Vec::new();
    if h.is_top == 1 {
        badges.push("[置顶]".red().to_string());
    }
    if h.reward_cost > 0 {
        badges.push(format!("[悬赏{}🍃]", h.reward_cost).yellow().to_string());
    }
    for t in &h.tags_info {
        badges.push(format!("#{}", t.tag_name).blue().to_string());
    }
    if h.is_follow == 1 {
        badges.push("[关注中]".green().to_string());
    }

    let badge_str = if badges.is_empty() {
        String::new()
    } else {
        format!(" {}", badges.join(" "))
    };

    let stats = format!(
        "{}{}{}",
        format!("{}", h.likenum).green(),
        format!("{}", h.tread_num).red(),
        format!(" 💬{}", h.reply).dimmed(),
    );

    println!("{}{} {}{}", pid, badge_str, time, stats);
}

/// 打印帖子头部(PID + 标签 + 时间 + 互动数据)
fn print_hole_header(h: &Hole) {
    let pid = format!("#{}", h.pid).bold().cyan();
    let time = fmt_time(h.timestamp).dimmed();

    let mut badges = Vec::new();
    if h.is_top == 1 {
        badges.push("[置顶]".red().to_string());
    }
    if h.reward_cost > 0 {
        badges.push(format!("[悬赏{}🍃]", h.reward_cost).yellow().to_string());
    }
    for t in &h.tags_info {
        badges.push(format!("#{}", t.tag_name).blue().to_string());
    }
    if h.is_follow == 1 {
        badges.push("[关注中]".green().to_string());
    }

    let badge_str = if badges.is_empty() {
        String::new()
    } else {
        format!(" {}", badges.join(" "))
    };

    let stats = format!(
        "{}{}{}",
        format!("{}", h.likenum).green(),
        format!("{}", h.tread_num).red(),
        format!(" 💬{}", h.reply).dimmed(),
    );

    println!("{}{} {}{}", pid, badge_str, time, stats);
}

/// 打印帖子详情(完整正文 + 全部评论)
pub fn print_hole_detail(h: &Hole, comments: &[Comment], total: Option<i64>) {
    print_hole_header(h);
    println!();

    // 完整正文
    for line in h.text.lines() {
        println!("  {line}");
    }
    if !h.media_ids.is_empty() {
        println!("  {}", media_badge(&h.media_ids).trim());
    }
    println!();

    // 分割线
    let total_count = total.unwrap_or(comments.len() as i64);
    println!(
        "{}",
        format!("── 评论 ({total_count}) ──────────────────────")
            .dimmed()
    );
    println!();

    for c in comments {
        print_comment(c);
    }
}

/// 打印单条评论
pub fn print_comment(c: &Comment) {
    let tag_str = if c.is_lz == 1 {
        format!("[{}]", c.name_tag).bold().yellow().to_string()
    } else {
        format!("[{}]", c.name_tag).bold().to_string()
    };

    let cid = format!("#{}", c.cid).dimmed();
    let time = fmt_time(c.timestamp).dimmed();

    // 引用
    if let Some(q) = c.quote.as_object() {
        if let (Some(qt), Some(qn)) = (
            q.get("text").and_then(|v| v.as_str()),
            q.get("name_tag").and_then(|v| v.as_str()),
        ) {
            let qtext = truncate_display(&qt.replace('\n', " "), 50);
            println!(
                "  {} {}",
                format!("Re {qn}:").dimmed(),
                qtext.dimmed()
            );
        }
    }

    println!("  {} {} {}", tag_str, cid, time);

    for line in c.text.lines() {
        println!("    {line}");
    }
    if !c.media_ids.is_empty() {
        println!("    {}", "[图片]".magenta());
    }
    println!();
}

/// 打印简单帖子列表(搜索/我的帖子)
pub fn print_hole_simple(h: &Hole) {
    print_hole_header(h);
    let text = h.text.replace('\n', " ");
    let preview = truncate_display(&text, 80);
    print!("  {}", preview);
    println!("{}", media_badge(&h.media_ids));
    println!();
}

/// 打印消息
pub fn print_message(m: &Message) {
    let read_mark = if m.is_read == 0 {
        "".red().to_string()
    } else {
        "".dimmed().to_string()
    };

    let pid_str = m
        .pid
        .map(|p| format!(" #{p}").cyan().to_string())
        .unwrap_or_default();

    println!(
        "{} {}{} {}",
        read_mark,
        m.title.bold(),
        pid_str,
        m.created_at.dimmed()
    );
    if !m.content.is_empty() {
        let preview = truncate_display(&m.content.replace('\n', " "), 70);
        println!("  {}", preview.dimmed());
    }
    println!();
}

/// 打印用户信息
pub fn print_user_info(u: &UserInfo) {
    println!("{}", "── 个人信息 ──".bold());
    println!("  UID          {}", u.uid);
    println!("  姓名         {}", u.name);
    println!("  剩余操作次数  {}", u.action_remaining);
    println!("  树叶余额      {}", u.leaf_balance);
    println!("  未读消息      {}", u.newmsgcount);
    if u.is_black == 1 {
        println!("  {}", "⚠ 账号处于黑名单状态".red());
    }
}

// ─── 成绩 ───────────────────────────────────────────────────────

/// 格式化学期名 "25-26-1" → "25-26学年度1学期"
fn fmt_semester(xndxq: &str) -> String {
    let parts: Vec<&str> = xndxq.split('-').collect();
    if parts.len() == 3 {
        format!("{}-{}学年度{}学期", parts[0], parts[1], parts[2])
    } else {
        xndxq.to_string()
    }
}

pub fn print_scores(data: &ScoreData, semester_filter: Option<&str>, no_color: bool) {
    println!(
        "{}  总学分: {}  总GPA: {}",
        "── 成绩查询 ──".bold(),
        data.total_credits.bold(),
        if no_color {
            data.overall_gpa.bold().to_string()
        } else {
            colorize::colorize_gpa(&data.overall_gpa).to_string()
        }
    );
    println!();

    // Group courses by semester (xnd + xq → "25-26-1")
    let mut semesters: Vec<&str> = data
        .semester_gpas
        .iter()
        .map(|g| g.xndxq.as_str())
        .collect();
    // If no semester GPAs, derive from courses
    if semesters.is_empty() {
        let mut seen = std::collections::HashSet::new();
        for c in &data.courses {
            let key = format!("{}-{}", c.xnd, c.xq);
            if seen.insert(key.clone()) {
                semesters.push(Box::leak(key.into_boxed_str()));
            }
        }
    }

    for sem_key in &semesters {
        // Apply filter
        if let Some(filter) = semester_filter {
            if !sem_key.contains(filter) {
                continue;
            }
        }

        let sem_gpa = data
            .semester_gpas
            .iter()
            .find(|g| g.xndxq == *sem_key)
            .map(|g| g.gpa.as_str())
            .unwrap_or("N/A");

        let sem_name = fmt_semester(sem_key);
        let gpa_display = if no_color {
            sem_gpa.bold().to_string()
        } else {
            colorize::colorize_gpa(sem_gpa).to_string()
        };

        println!("  {} {}", sem_name.bold(), gpa_display);
        println!();

        // Filter courses for this semester
        let sem_courses: Vec<_> = data
            .courses
            .iter()
            .filter(|c| {
                let key = format!("{}-{}", c.xnd, c.xq);
                key == *sem_key
            })
            .collect();

        for c in &sem_courses {
            let credits = format!("{}学分", c.xf);
            let category = &c.kclbmc;
            let score_display = if no_color {
                c.xqcj.bold().to_string()
            } else {
                colorize::colorize_score(&c.xqcj).to_string()
            };
            let bar = if no_color {
                String::new()
            } else {
                format!(" {}", colorize::score_bar(&c.xqcj, 10))
            };

            println!(
                "    {} {} {} {}{}",
                credits.dimmed(),
                category.dimmed(),
                c.kcmc,
                score_display,
                bar,
            );
        }
        println!();
    }
}

// ─── 课表 ───────────────────────────────────────────────────────

pub fn print_coursetable(rows: &[CourseRow]) {
    let days = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"];

    println!(
        "  {:>8}  {}",
        "节次".bold(),
        days.iter()
            .map(|d| format!("{:^14}", d.bold()))
            .collect::<Vec<_>>()
            .join("")
    );
    println!("  {}", "".repeat(8 + 14 * 7));

    for row in rows {
        // Check if any slot is non-empty
        let has_content = row.slots.iter().any(|s| s.is_some());
        if !has_content {
            continue;
        }

        let time_label = format!("{:>8}", row.time_num);
        let slots_str: Vec<String> = row
            .slots
            .iter()
            .map(|slot| match slot {
                Some(s) => {
                    let name = truncate_display(&s.course_name, 12);
                    // Color based on the CSS background-color
                    let colored_name = match s.style.as_str() {
                        s if s.contains("aquamarine") => name.on_cyan().black().to_string(),
                        s if s.contains("lightcoral") => name.on_red().white().to_string(),
                        s if s.contains("lightcyan") => name.on_bright_cyan().black().to_string(),
                        s if s.contains("lightpink") => name.on_magenta().white().to_string(),
                        s if s.contains("lightgrey") | s.contains("lightgray") => {
                            name.on_white().black().to_string()
                        }
                        s if s.contains("lightgreen") => name.on_green().black().to_string(),
                        s if s.contains("lightsalmon") => name.on_yellow().black().to_string(),
                        s if s.contains("lightseagreen") => {
                            name.on_bright_green().black().to_string()
                        }
                        _ => name.to_string(),
                    };
                    format!("{:^14}", colored_name)
                }
                None => format!("{:^14}", "·".dimmed()),
            })
            .collect();

        println!("  {}  {}", time_label.dimmed(), slots_str.join(""));
    }
    println!();
}

pub fn print_class_times(times: &[ClassTime]) {
    println!("{}", "── 作息时间 ──".bold());
    for t in times {
        println!(
            "  {:>8}  {}",
            t.name.bold(),
            t.time_period.dimmed()
        );
    }
}

// ─── 学术日历 ───────────────────────────────────────────────────

pub fn print_lab_event(e: &LabEvent) {
    let date = format!("📅 {}", e.start_time).cyan();
    println!("  {}", date);
    println!("    📌 {}", e.title.bold());
    println!(
        "    {}  {}",
        e.dept.blue(),
        e.host.as_deref().unwrap_or("").dimmed()
    );
    if let Some(loc) = &e.location {
        if !loc.is_empty() {
            println!("    📍 {}", loc.dimmed());
        }
    }
    println!();
}

// ─── 活动日历 ───────────────────────────────────────────────────

pub fn print_activity_event(e: &ActivityEvent) {
    let tag = format!("[{}]", e.event_type_name).blue();
    println!("  {} {}", tag, e.event_name.bold());
    println!("    📅 {}", e.event_start_time.cyan());
    if !e.event_location.is_empty() {
        println!("    📍 {}", e.event_location.dimmed());
    }
    if !e.event_organizer.is_empty() {
        println!("    🏫 {}", e.event_organizer.dimmed());
    }
    if !e.event_introduction.is_empty() {
        let preview = truncate_display(&e.event_introduction, 80);
        println!("    {}", preview.dimmed());
    }
    println!();
}

// ─── 日程 ───────────────────────────────────────────────────────

pub fn print_schedule_item(s: &ScheduleItem) {
    println!(
        "  {} {} ~ {}",
        s.title.bold(),
        s.start_time.cyan(),
        s.end_time.cyan()
    );
    if !s.content.is_empty() {
        println!("    {}", s.content.dimmed());
    }
    println!();
}