1use crate::{displayed_item_tree::VisibleItemIndex, wave_data::WaveData};
3use camino::Utf8PathBuf;
4use egui::RichText;
5#[cfg(not(target_arch = "wasm32"))]
6use std::path::{Path, PathBuf};
7
8#[must_use]
13pub fn uint_idx_to_alpha_idx(idx: VisibleItemIndex, nvariables: usize) -> String {
14 let width = usize::try_from(nvariables.ilog(16)).unwrap() + 1;
18 format!("{:0width$x}", idx.0)
19 .chars()
20 .map(|c| match c {
21 '0' => 'a',
22 '1' => 'b',
23 '2' => 'c',
24 '3' => 'd',
25 '4' => 'e',
26 '5' => 'f',
27 '6' => 'g',
28 '7' => 'h',
29 '8' => 'i',
30 '9' => 'j',
31 'a' => 'k',
32 'b' => 'l',
33 'c' => 'm',
34 'd' => 'n',
35 'e' => 'o',
36 'f' => 'p',
37 _ => '?',
38 })
39 .collect()
40}
41
42pub fn alpha_idx_to_uint_idx(idx: &str) -> Option<VisibleItemIndex> {
44 let mapped = idx
45 .chars()
46 .map(|c| match c {
47 'a' => '0',
48 'b' => '1',
49 'c' => '2',
50 'd' => '3',
51 'e' => '4',
52 'f' => '5',
53 'g' => '6',
54 'h' => '7',
55 'i' => '8',
56 'j' => '9',
57 'k' => 'a',
58 'l' => 'b',
59 'm' => 'c',
60 'n' => 'd',
61 'o' => 'e',
62 'p' => 'f',
63 _ => '?',
64 })
65 .collect::<String>();
66 usize::from_str_radix(&mapped, 16)
67 .ok()
68 .map(VisibleItemIndex)
69}
70
71#[must_use]
72pub fn get_alpha_focus_id(vidx: VisibleItemIndex, waves: &WaveData) -> RichText {
73 let alpha_id = uint_idx_to_alpha_idx(vidx, waves.displayed_items.len());
74
75 RichText::new(alpha_id).monospace()
76}
77
78#[cfg(not(target_arch = "wasm32"))]
82pub fn search_upward(
83 start: impl AsRef<Path>,
84 end: impl AsRef<Path>,
85 item: impl AsRef<Path>,
86) -> Vec<PathBuf> {
87 start
88 .as_ref()
89 .ancestors()
90 .take_while(|p| p.starts_with(end.as_ref()))
91 .map(|p| p.join(&item))
92 .filter(|p| p.try_exists().is_ok_and(std::convert::identity))
93 .collect()
94}
95
96fn get_multi_extension_from_filename(filename: &str) -> Option<String> {
97 filename
98 .find('.')
99 .map(|pos| filename[pos + 1..].to_string())
100}
101
102#[must_use]
106pub fn get_multi_extension(path: &Utf8PathBuf) -> Option<String> {
107 if let Some(filename) = path.file_name() {
109 return get_multi_extension_from_filename(filename);
110 }
111 None
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_uint_idx_to_alpha_idx_basic_width_1() {
120 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0), 1), "a");
123 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(9), 1), "j");
124 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(15), 1), "p");
125 }
126
127 #[test]
128 fn test_uint_idx_to_alpha_idx_zero_padded_width_2() {
129 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x0), 16), "aa");
131 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x1), 16), "ab");
132 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0xf), 16), "ap");
133 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x10), 16), "ba");
134 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x1f), 16), "bp");
135 }
136
137 #[test]
138 fn test_alpha_idx_to_uint_idx_roundtrip() {
139 let cases = [
141 (VisibleItemIndex(0x0), 1),
142 (VisibleItemIndex(0x9), 1),
143 (VisibleItemIndex(0xf), 1),
144 (VisibleItemIndex(0x10), 16),
145 (VisibleItemIndex(0x2a), 256),
146 (VisibleItemIndex(0xabc), 4096),
147 ];
148
149 for (vidx, nvars) in cases {
150 let s = uint_idx_to_alpha_idx(vidx, nvars);
151 let back = alpha_idx_to_uint_idx(&s).expect("should parse back");
152 assert_eq!(back, vidx);
153 }
154 }
155
156 #[test]
157 fn test_alpha_idx_to_uint_idx_invalid_input() {
158 assert!(alpha_idx_to_uint_idx("ar").is_none());
160 assert!(alpha_idx_to_uint_idx("").is_none());
162 assert!(alpha_idx_to_uint_idx("A").is_none());
164 assert!(alpha_idx_to_uint_idx("-").is_none());
165 }
166
167 #[test]
168 fn test_get_multi_extension_from_filename() {
169 assert_eq!(
170 get_multi_extension_from_filename("foo.tar.gz"),
171 Some("tar.gz".to_string())
172 );
173 assert_eq!(
174 get_multi_extension_from_filename("foo.txt"),
175 Some("txt".to_string())
176 );
177 assert_eq!(get_multi_extension_from_filename("foo"), None);
178 assert_eq!(
180 get_multi_extension_from_filename(".bashrc"),
181 Some("bashrc".to_string())
182 );
183 assert_eq!(
185 get_multi_extension_from_filename("foo."),
186 Some(String::new())
187 );
188 }
189
190 #[test]
191 fn test_get_multi_extension_from_path() {
192 let p = Utf8PathBuf::from("/tmp/foo/bar.tar.gz");
193 assert_eq!(get_multi_extension(&p), Some("tar.gz".to_string()));
194 let p = Utf8PathBuf::from("/tmp/foo/bar");
195 assert_eq!(get_multi_extension(&p), None);
196 }
197
198 #[test]
199 fn test_get_multi_extension_with_unicode() {
200 let name = "åäö.archive.tar.gz"; assert_eq!(
204 get_multi_extension_from_filename(name),
205 Some("archive.tar.gz".to_string())
206 );
207
208 let name2 = "ß.";
210 assert_eq!(
211 get_multi_extension_from_filename(name2),
212 Some(String::new())
213 );
214 }
215
216 #[cfg(not(target_arch = "wasm32"))]
217 #[test]
218 fn test_search_upward_finds_closest_first() {
219 use std::fs;
220 use std::io::Write;
221 use std::path::Path;
222
223 let tmp = tempfile::tempdir().expect("tempdir");
225 let root = tmp.path();
226 let a = root.join("a");
227 let b = a.join("b");
228 let c = b.join("c");
229 fs::create_dir_all(&c).expect("dirs");
230
231 let item_name = Path::new("target.txt");
233 let item_c = c.join(item_name);
234 let item_a = a.join(item_name);
235 {
236 let mut f = fs::File::create(&item_c).expect("create c");
237 writeln!(f, "hello").unwrap();
238 }
239 {
240 let mut f = fs::File::create(&item_a).expect("create a");
241 writeln!(f, "world").unwrap();
242 }
243
244 let found = search_upward(&c, root, item_name);
246 assert_eq!(found, vec![item_c, item_a]);
248 }
249}