ass_editor/commands/fonts_graphics_commands/
list.rs1use crate::core::{EditorDocument, Result};
4
5#[cfg(not(feature = "std"))]
6use alloc::{
7 string::{String, ToString},
8 vec::Vec,
9};
10
11#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct ListFontsCommand;
16
17impl ListFontsCommand {
18 pub fn new() -> Self {
20 Self
21 }
22
23 pub fn list(&self, document: &EditorDocument) -> Result<Vec<String>> {
25 let content = document.text();
26
27 let fonts_section = content.find("[Fonts]");
29 if fonts_section.is_none() {
30 return Ok(Vec::new());
31 }
32
33 let section_start = fonts_section.unwrap();
34 let section_end = content[section_start..]
35 .find("\n[")
36 .map(|pos| section_start + pos)
37 .unwrap_or(content.len());
38
39 let section_content = &content[section_start..section_end];
40 let mut fonts = Vec::new();
41
42 for line in section_content.lines() {
43 if let Some(filename) = line.strip_prefix("fontname: ") {
44 fonts.push(filename.trim().to_string());
45 }
46 }
47
48 Ok(fonts)
49 }
50}
51
52impl Default for ListFontsCommand {
53 fn default() -> Self {
54 Self::new()
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct ListGraphicsCommand;
63
64impl ListGraphicsCommand {
65 pub fn new() -> Self {
67 Self
68 }
69
70 pub fn list(&self, document: &EditorDocument) -> Result<Vec<String>> {
72 let content = document.text();
73
74 let graphics_section = content.find("[Graphics]");
76 if graphics_section.is_none() {
77 return Ok(Vec::new());
78 }
79
80 let section_start = graphics_section.unwrap();
81 let section_end = content[section_start..]
82 .find("\n[")
83 .map(|pos| section_start + pos)
84 .unwrap_or(content.len());
85
86 let section_content = &content[section_start..section_end];
87 let mut graphics = Vec::new();
88
89 for line in section_content.lines() {
90 if let Some(filename) = line.strip_prefix("filename: ") {
91 graphics.push(filename.trim().to_string());
92 }
93 }
94
95 Ok(graphics)
96 }
97}
98
99impl Default for ListGraphicsCommand {
100 fn default() -> Self {
101 Self::new()
102 }
103}