Skip to main content

ass_editor/core/document/
section_lines.rs

1//! Append/replace helpers for event and style section lines
2//!
3//! Text-level helpers that insert new dialogue or style lines, creating the
4//! relevant section (with a default format line) when it does not yet exist.
5
6use super::EditorDocument;
7use crate::core::errors::Result;
8use crate::core::position::{Position, Range};
9
10#[cfg(not(feature = "std"))]
11use alloc::format;
12
13impl EditorDocument {
14    /// Add event line to document
15    pub fn add_event_line(&mut self, event_line: &str) -> Result<()> {
16        let content = self.text();
17        if let Some(events_pos) = content.find("[Events]") {
18            // Find end of format line and add after it
19            let format_start = content[events_pos..].find("Format:").unwrap_or(0) + events_pos;
20            let line_end = content[format_start..].find('\n').unwrap_or(0) + format_start + 1;
21
22            let insert_pos = Position::new(line_end);
23            self.insert(insert_pos, &format!("{event_line}\n"))
24        } else {
25            // Add Events section if it doesn't exist
26            let content_len = self.len_bytes();
27            let events_section = format!("\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n{event_line}\n");
28            self.insert(Position::new(content_len), &events_section)
29        }
30    }
31
32    /// Edit style line
33    pub fn edit_style_line(&mut self, style_name: &str, new_style_line: &str) -> Result<()> {
34        let content = self.text();
35        let pattern = format!("Style: {style_name},");
36
37        if let Some(pos) = content.find(&pattern) {
38            // Find end of line
39            let line_end = content[pos..].find('\n').map_or(content.len(), |n| pos + n);
40            let range = Range::new(Position::new(pos), Position::new(line_end));
41            self.replace(range, new_style_line)
42        } else {
43            // Add style if it doesn't exist
44            self.add_style_line(new_style_line)
45        }
46    }
47
48    /// Add style line to document
49    pub fn add_style_line(&mut self, style_line: &str) -> Result<()> {
50        let content = self.text();
51        if let Some(styles_pos) = content
52            .find("[V4+ Styles]")
53            .or_else(|| content.find("[V4 Styles]"))
54        {
55            // Find end of format line and add after it
56            let format_start = content[styles_pos..].find("Format:").unwrap_or(0) + styles_pos;
57            let line_end = content[format_start..].find('\n').unwrap_or(0) + format_start + 1;
58
59            let insert_pos = Position::new(line_end);
60            self.insert(insert_pos, &format!("{style_line}\n"))
61        } else {
62            // Add Styles section if it doesn't exist
63            let script_info_end = content.find("\n[Events]").unwrap_or(content.len());
64            let styles_section = format!("\n[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n{style_line}\n");
65            self.insert(Position::new(script_info_end), &styles_section)
66        }
67    }
68}