1use std::fmt::Write as _;
34use std::time::Duration;
35
36use crate::LogChannel;
37
38#[derive(Debug, Clone)]
40pub struct TuiOptions {
41 pub log_lines: usize,
43 pub buffer_lines: usize,
45 pub refresh: Duration,
47 pub bar_width: usize,
49 pub color: bool,
51 channel: Option<LogChannel>,
53}
54
55impl Default for TuiOptions {
56 fn default() -> Self {
57 Self {
58 log_lines: 12,
59 buffer_lines: 500,
60 refresh: Duration::from_millis(100),
61 bar_width: 20,
62 color: false,
63 channel: None,
64 }
65 }
66}
67
68impl TuiOptions {
69 pub fn with_channel(mut self, channel: LogChannel) -> Self {
71 self.channel = Some(channel);
72 self
73 }
74
75 pub(crate) fn take_channel(&mut self) -> LogChannel {
76 self.channel
77 .take()
78 .unwrap_or_else(|| LogChannel::with_capacity(self.buffer_lines))
79 }
80}
81
82pub(crate) fn render_frame(
84 progress_line: &str,
85 channel: &LogChannel,
86 options: &TuiOptions,
87) -> String {
88 let mut frame = String::new();
89 let _ = write!(frame, "{progress_line}\r\n");
90 let _ = write!(frame, "{}\r\n", "─".repeat(options.bar_width + 32));
91
92 let lines: Vec<String> = channel
93 .lines()
94 .into_iter()
95 .rev()
96 .take(options.log_lines)
97 .rev()
98 .collect();
99 for line in lines {
100 let line: String = line.chars().take(240).collect();
101 let _ = write!(frame, "{line}\r\n");
102 }
103
104 frame
105}
106
107impl tracing_subscriber::fmt::MakeWriter<'_> for LogChannel {
110 type Writer = LogChannel;
111
112 fn make_writer(&self) -> Self::Writer {
113 self.clone()
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 fn frame_fixture() -> LogChannel {
122 let channel = LogChannel::with_capacity(20);
123 for i in 0..5 {
124 channel.push_line(format!("log line {i}"));
125 }
126 channel
127 }
128
129 #[test]
130 fn frame_lays_out_progress_and_recent_logs() {
131 let channel = frame_fixture();
132 let frame = render_frame(
133 "[████████░░░░] 50.0% 5/10",
134 &channel,
135 &TuiOptions::default(),
136 );
137
138 assert!(
139 frame.starts_with("[████████░░░░] 50.0% 5/10\r\n"),
140 "{frame:?}"
141 );
142 assert!(frame.contains("─"), "{frame:?}");
143 assert!(frame.contains("log line 0\r\n"), "{frame:?}");
144 assert!(frame.ends_with("log line 4\r\n"), "{frame:?}");
145 }
146
147 #[test]
148 fn frame_keeps_only_recent_lines() {
149 let channel = frame_fixture();
150 let options = TuiOptions {
151 log_lines: 2,
152 ..TuiOptions::default()
153 };
154 let frame = render_frame("progress", &channel, &options);
155
156 assert!(!frame.contains("log line 0"), "{frame:?}");
157 assert!(!frame.contains("log line 2"), "{frame:?}");
158 assert!(frame.ends_with("log line 4\r\n"), "{frame:?}");
159 }
160
161 #[test]
162 fn frame_truncates_overlong_log_lines() {
163 let channel = LogChannel::with_capacity(5);
164 channel.push_line("x".repeat(300));
165 let frame = render_frame("p", &channel, &TuiOptions::default());
166
167 assert!(frame.contains(&"x".repeat(240)), "应截断到 240 字符");
168 assert!(!frame.contains(&"x".repeat(241)), "{frame:?}");
169 }
170
171 #[test]
172 fn make_writer_routes_fmt_lines_into_buffer() {
173 use tracing_subscriber::fmt::MakeWriter as _;
174
175 let channel = LogChannel::with_capacity(10);
176 {
177 let mut writer = channel.clone().make_writer();
178 std::io::Write::write_all(&mut writer, b"hello from fmt\n").unwrap();
179 }
180 assert_eq!(channel.lines(), vec!["hello from fmt"]);
181 }
182}