blivedm_plugins/
terminal_display.rs

1use client::models::BiliMessage;
2use client::scheduler::{EventHandler, EventContext};
3
4/// A plugin that prints BiliMessages to the terminal.
5pub struct TerminalDisplayHandler;
6
7impl EventHandler for TerminalDisplayHandler {
8    fn handle(&self, msg: &BiliMessage, _context: &EventContext) {
9        match msg {
10            BiliMessage::Danmu { user, text } => {
11                println!("[Danmu] {}: {}", user, text);
12            }
13            BiliMessage::Gift { user, gift } => {
14                println!("[Gift] {} sent a gift: {}", user, gift);
15            }
16            BiliMessage::Unsupported => {
17                println!("[Unsupported message type]");
18            }
19        }
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use client::models::BiliMessage;
27    use client::scheduler::EventHandler;
28
29    #[test]
30    fn test_terminal_display_handler_prints_danmu() {
31        let handler = TerminalDisplayHandler;
32        let msg = BiliMessage::Danmu {
33            user: "test_user".to_string(),
34            text: "hello world".to_string(),
35        };
36        // This will print to stdout, but we just want to ensure it doesn't panic
37        let context = EventContext { cookies: None, room_id: 12345 };
38        handler.handle(&msg, &context);
39    }
40
41    #[test]
42    fn test_terminal_display_handler_prints_gift() {
43        let handler = TerminalDisplayHandler;
44        let msg = BiliMessage::Gift {
45            user: "gift_user".to_string(),
46            gift: "rocket".to_string(),
47        };
48        let context = EventContext { cookies: None, room_id: 12345 };
49        handler.handle(&msg, &context);
50    }
51
52    #[test]
53    fn test_terminal_display_handler_prints_unsupported() {
54        let handler = TerminalDisplayHandler;
55        let msg = BiliMessage::Unsupported;
56        let context = EventContext { cookies: None, room_id: 12345 };
57        handler.handle(&msg, &context);
58    }
59}