Skip to main content

roder_edit_core/
read.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub struct ReadFormatOptions {
3    pub start_line: usize,
4    pub limit: usize,
5}
6
7impl Default for ReadFormatOptions {
8    fn default() -> Self {
9        Self {
10            start_line: 1,
11            limit: 200,
12        }
13    }
14}
15
16pub fn format_line_numbered_read(text: &str, options: ReadFormatOptions) -> String {
17    let start_line = options.start_line.max(1);
18    let limit = options.limit.max(1);
19    text.lines()
20        .enumerate()
21        .skip(start_line - 1)
22        .take(limit)
23        .map(|(index, line)| format!("{:>5}: {}", index + 1, line))
24        .collect::<Vec<_>>()
25        .join("\n")
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn formats_plain_line_numbers() {
34        let output = format_line_numbered_read(
35            "a\nb\nc",
36            ReadFormatOptions {
37                start_line: 2,
38                limit: 1,
39            },
40        );
41        assert_eq!(output, "    2: b");
42    }
43}