longcipher_leptos_components/components/editor/
line_numbers.rs

1//! Line numbers display
2//!
3//! Provides line number gutter rendering for the editor.
4
5/// Count the number of lines in text.
6#[must_use]
7pub fn count_lines(text: &str) -> usize {
8    if text.is_empty() {
9        1
10    } else {
11        text.chars().filter(|&c| c == '\n').count() + 1
12    }
13}
14
15/// Get the width needed for line number display.
16#[must_use]
17pub fn gutter_width(line_count: usize, font_size: f32) -> f32 {
18    let digit_count = if line_count == 0 {
19        1
20    } else {
21        (line_count as f32).log10().floor() as usize + 1
22    };
23
24    let char_width = font_size * 0.6;
25    let content_width = char_width * digit_count as f32;
26
27    (content_width + 24.0).max(40.0)
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_count_lines() {
36        assert_eq!(count_lines(""), 1);
37        assert_eq!(count_lines("hello"), 1);
38        assert_eq!(count_lines("hello\nworld"), 2);
39        assert_eq!(count_lines("a\nb\nc\n"), 4);
40    }
41
42    #[test]
43    fn test_gutter_width() {
44        let width = gutter_width(100, 14.0);
45        assert!(width >= 40.0);
46    }
47}