ass_editor/commands/fonts_graphics_commands/
clear.rs1use crate::commands::{CommandResult, EditorCommand};
4use crate::core::{EditorDocument, Position, Range, Result};
5
6#[cfg(not(feature = "std"))]
7use alloc::string::ToString;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct ClearFontsCommand;
14
15impl ClearFontsCommand {
16 pub fn new() -> Self {
18 Self
19 }
20}
21
22impl Default for ClearFontsCommand {
23 fn default() -> Self {
24 Self::new()
25 }
26}
27
28impl EditorCommand for ClearFontsCommand {
29 fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
30 let content = document.text().to_string();
31
32 if let Some(fonts_section) = content.find("[Fonts]") {
34 let header_end = content[fonts_section..]
35 .find('\n')
36 .map(|pos| fonts_section + pos + 1)
37 .unwrap_or(fonts_section + "[Fonts]".len());
38
39 let section_end = content[fonts_section..]
40 .find("\n[")
41 .map(|pos| fonts_section + pos)
42 .unwrap_or(content.len());
43
44 if section_end > header_end {
45 let range = Range::new(Position::new(header_end), Position::new(section_end));
46 document.delete(range)?;
47
48 return Ok(CommandResult::success_with_change(
49 Range::new(Position::new(header_end), Position::new(header_end)),
50 Position::new(header_end),
51 ));
52 }
53 }
54
55 Ok(CommandResult::success())
56 }
57
58 fn description(&self) -> &str {
59 "Clear all fonts"
60 }
61
62 fn memory_usage(&self) -> usize {
63 core::mem::size_of::<Self>()
64 }
65}
66
67#[derive(Debug, Clone, PartialEq, Eq)]
71pub struct ClearGraphicsCommand;
72
73impl ClearGraphicsCommand {
74 pub fn new() -> Self {
76 Self
77 }
78}
79
80impl Default for ClearGraphicsCommand {
81 fn default() -> Self {
82 Self::new()
83 }
84}
85
86impl EditorCommand for ClearGraphicsCommand {
87 fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
88 let content = document.text().to_string();
89
90 if let Some(graphics_section) = content.find("[Graphics]") {
92 let header_end = content[graphics_section..]
93 .find('\n')
94 .map(|pos| graphics_section + pos + 1)
95 .unwrap_or(graphics_section + "[Graphics]".len());
96
97 let section_end = content[graphics_section..]
98 .find("\n[")
99 .map(|pos| graphics_section + pos)
100 .unwrap_or(content.len());
101
102 if section_end > header_end {
103 let range = Range::new(Position::new(header_end), Position::new(section_end));
104 document.delete(range)?;
105
106 return Ok(CommandResult::success_with_change(
107 Range::new(Position::new(header_end), Position::new(header_end)),
108 Position::new(header_end),
109 ));
110 }
111 }
112
113 Ok(CommandResult::success())
114 }
115
116 fn description(&self) -> &str {
117 "Clear all graphics"
118 }
119
120 fn memory_usage(&self) -> usize {
121 core::mem::size_of::<Self>()
122 }
123}