use std::sync::Arc;
use data_url::DataUrl;
use gpui::{Image, ImageFormat};
const NUMBERED_PREFIXES_1: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const NUMBERED_PREFIXES_2: &str = "abcdefghijklmnopqrstuvwxyz";
const BULLETS: [&str; 5] = ["•", "◦", "▪", "‣", "⁃"];
pub(super) fn list_item_prefix(ix: usize, ordered: bool, depth: usize) -> String {
if ordered {
if depth == 0 {
return format!("{}. ", ix + 1);
}
if depth == 1 {
return format!(
"{}. ",
NUMBERED_PREFIXES_1
.chars()
.nth(ix % NUMBERED_PREFIXES_1.len())
.unwrap()
);
} else {
return format!(
"{}. ",
NUMBERED_PREFIXES_2
.chars()
.nth(ix % NUMBERED_PREFIXES_2.len())
.unwrap()
);
}
} else {
let depth = depth.min(BULLETS.len() - 1);
let bullet = BULLETS[depth];
return format!("{} ", bullet);
}
}
pub(super) fn data_url_image(url: &str) -> Option<Arc<Image>> {
let data_url = DataUrl::process(url).ok()?;
let mime = data_url.mime_type();
let format = ImageFormat::from_mime_type(&format!("{}/{}", mime.type_, mime.subtype))?;
let (bytes, _fragment) = data_url.decode_to_vec().ok()?;
Some(Arc::new(Image::from_bytes(format, bytes)))
}
#[cfg(test)]
mod tests {
use gpui::ImageFormat;
use crate::text::utils::{data_url_image, list_item_prefix};
#[test]
fn test_data_url_image() {
fn image(url: &str) -> (ImageFormat, Vec<u8>) {
let image = data_url_image(url)
.unwrap_or_else(|| panic!("expected an embedded image for {url:?}"));
(image.format(), image.bytes().to_vec())
}
assert_eq!(
image("data:image/png;base64,iVBORw0KGgo="),
(ImageFormat::Png, b"\x89PNG\r\n\x1a\n".to_vec())
);
assert_eq!(
image("data:image/jpg;base64,/9j/4A=="),
(ImageFormat::Jpeg, b"\xff\xd8\xff\xe0".to_vec())
);
assert_eq!(
image("data:image/svg+xml,%3Csvg%3E%3C/svg%3E"),
(ImageFormat::Svg, b"<svg></svg>".to_vec())
);
assert!(data_url_image("https://example.com/logo.png").is_none());
assert!(data_url_image("data:text/plain;base64,aGVsbG8=").is_none());
assert!(data_url_image("data:image/png;base64,not*base64").is_none());
assert!(data_url_image("data:image/png").is_none());
}
#[test]
fn test_list_item_prefix() {
assert_eq!(list_item_prefix(0, true, 0), "1. ");
assert_eq!(list_item_prefix(1, true, 0), "2. ");
assert_eq!(list_item_prefix(2, true, 0), "3. ");
assert_eq!(list_item_prefix(10, true, 0), "11. ");
assert_eq!(list_item_prefix(0, true, 1), "A. ");
assert_eq!(list_item_prefix(1, true, 1), "B. ");
assert_eq!(list_item_prefix(2, true, 1), "C. ");
assert_eq!(list_item_prefix(0, true, 2), "a. ");
assert_eq!(list_item_prefix(1, true, 2), "b. ");
assert_eq!(list_item_prefix(6, true, 2), "g. ");
assert_eq!(list_item_prefix(0, true, 1), "A. ");
assert_eq!(list_item_prefix(0, true, 2), "a. ");
assert_eq!(list_item_prefix(0, false, 0), "• ");
assert_eq!(list_item_prefix(0, false, 1), "◦ ");
assert_eq!(list_item_prefix(0, false, 2), "▪ ");
assert_eq!(list_item_prefix(0, false, 3), "‣ ");
assert_eq!(list_item_prefix(0, false, 4), "⁃ ");
}
}