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(crate) 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(crate) 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(crate) 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"))]
83pub(crate) fn search_upward(
84 start: impl AsRef<Path>,
85 end: impl AsRef<Path>,
86 item: impl AsRef<Path>,
87) -> Vec<PathBuf> {
88 start
89 .as_ref()
90 .ancestors()
91 .take_while(|p| p.starts_with(end.as_ref()))
92 .map(|p| p.join(&item))
93 .filter(|p| p.try_exists().is_ok_and(std::convert::identity))
94 .collect()
95}
96
97fn get_multi_extension_from_filename(filename: &str) -> Option<String> {
98 filename
99 .find('.')
100 .map(|pos| filename[pos + 1..].to_string())
101}
102
103#[must_use]
107pub(crate) fn get_multi_extension(path: &Utf8PathBuf) -> Option<String> {
108 if let Some(filename) = path.file_name() {
110 return get_multi_extension_from_filename(filename);
111 }
112 None
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn test_uint_idx_to_alpha_idx_basic_width_1() {
121 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0), 1), "a");
124 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(9), 1), "j");
125 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(15), 1), "p");
126 }
127
128 #[test]
129 fn test_uint_idx_to_alpha_idx_zero_padded_width_2() {
130 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x0), 16), "aa");
132 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x1), 16), "ab");
133 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0xf), 16), "ap");
134 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x10), 16), "ba");
135 assert_eq!(uint_idx_to_alpha_idx(VisibleItemIndex(0x1f), 16), "bp");
136 }
137
138 #[test]
139 fn test_alpha_idx_to_uint_idx_roundtrip() {
140 let cases = [
142 (VisibleItemIndex(0x0), 1),
143 (VisibleItemIndex(0x9), 1),
144 (VisibleItemIndex(0xf), 1),
145 (VisibleItemIndex(0x10), 16),
146 (VisibleItemIndex(0x2a), 256),
147 (VisibleItemIndex(0xabc), 4096),
148 ];
149
150 for (vidx, nvars) in cases {
151 let s = uint_idx_to_alpha_idx(vidx, nvars);
152 let back = alpha_idx_to_uint_idx(&s).expect("should parse back");
153 assert_eq!(back, vidx);
154 }
155 }
156
157 #[test]
158 fn test_alpha_idx_to_uint_idx_invalid_input() {
159 assert!(alpha_idx_to_uint_idx("ar").is_none());
161 assert!(alpha_idx_to_uint_idx("").is_none());
163 assert!(alpha_idx_to_uint_idx("A").is_none());
165 assert!(alpha_idx_to_uint_idx("-").is_none());
166 }
167
168 #[test]
169 fn test_get_multi_extension_from_filename() {
170 assert_eq!(
171 get_multi_extension_from_filename("foo.tar.gz"),
172 Some("tar.gz".to_string())
173 );
174 assert_eq!(
175 get_multi_extension_from_filename("foo.txt"),
176 Some("txt".to_string())
177 );
178 assert_eq!(get_multi_extension_from_filename("foo"), None);
179 assert_eq!(
181 get_multi_extension_from_filename(".bashrc"),
182 Some("bashrc".to_string())
183 );
184 assert_eq!(
186 get_multi_extension_from_filename("foo."),
187 Some(String::new())
188 );
189 }
190
191 #[test]
192 fn test_get_multi_extension_from_path() {
193 let p = Utf8PathBuf::from("/tmp/foo/bar.tar.gz");
194 assert_eq!(get_multi_extension(&p), Some("tar.gz".to_string()));
195 let p = Utf8PathBuf::from("/tmp/foo/bar");
196 assert_eq!(get_multi_extension(&p), None);
197 }
198
199 #[test]
200 fn test_get_multi_extension_with_unicode() {
201 let name = "åäö.archive.tar.gz"; assert_eq!(
205 get_multi_extension_from_filename(name),
206 Some("archive.tar.gz".to_string())
207 );
208
209 let name2 = "ß.";
211 assert_eq!(
212 get_multi_extension_from_filename(name2),
213 Some(String::new())
214 );
215 }
216
217 #[cfg(not(target_arch = "wasm32"))]
218 #[test]
219 fn test_search_upward_finds_closest_first() {
220 use std::fs;
221 use std::io::Write;
222 use std::path::Path;
223
224 let tmp = tempfile::tempdir().expect("tempdir");
226 let root = tmp.path();
227 let a = root.join("a");
228 let b = a.join("b");
229 let c = b.join("c");
230 fs::create_dir_all(&c).expect("dirs");
231
232 let item_name = Path::new("target.txt");
234 let item_c = c.join(item_name);
235 let item_a = a.join(item_name);
236 {
237 let mut f = fs::File::create(&item_c).expect("create c");
238 writeln!(f, "hello").unwrap();
239 }
240 {
241 let mut f = fs::File::create(&item_a).expect("create a");
242 writeln!(f, "world").unwrap();
243 }
244
245 let found = search_upward(&c, root, item_name);
247 assert_eq!(found, vec![item_c, item_a]);
249 }
250}