feat(content): refine discovery project metadata
This commit is contained in:
1 parent
19c47cdf7b
commit
725b52579b
9 files changed
+441
-87
No files matched your search
@@ -125,6 +125,8 @@ mod tests {
|
||||
ContentEntry {
|
||||
file_stem: file_stem.to_string(),
|
||||
name: file_stem.to_string(),
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description: String::new(),
|
||||
enabled: true,
|
||||
icon_bytes: None,
|
||||
|
||||
@@ -26,6 +26,8 @@ pub struct IconCell {
|
||||
pub struct ContentEntry {
|
||||
pub file_stem: String,
|
||||
pub name: String,
|
||||
pub title_suffix: Option<String>,
|
||||
pub footer_label: Option<String>,
|
||||
pub description: String,
|
||||
pub enabled: bool,
|
||||
pub icon_bytes: Option<Vec<u8>>,
|
||||
@@ -108,6 +110,8 @@ pub fn scan_one_mod(path: &Path, file_stem: &str, enabled: bool) -> ContentEntry
|
||||
ContentEntry {
|
||||
file_stem: file_stem.to_owned(),
|
||||
name: display_name,
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
@@ -701,6 +705,8 @@ mod tests {
|
||||
let entry = ContentEntry {
|
||||
file_stem: "mymod".to_string(),
|
||||
name: "mymod".to_string(),
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description: String::new(),
|
||||
enabled: false,
|
||||
icon_bytes: None,
|
||||
@@ -972,6 +978,8 @@ description = "A neoforge mod"
|
||||
let entry = ContentEntry {
|
||||
file_stem: "mymod".to_string(),
|
||||
name: "mymod".to_string(),
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description: String::new(),
|
||||
enabled: true,
|
||||
icon_bytes: None,
|
||||
|
||||
@@ -65,6 +65,8 @@ pub fn scan_one_resource_pack(path: &Path, file_stem: &str, enabled: bool) -> Co
|
||||
ContentEntry {
|
||||
file_stem: file_stem.to_owned(),
|
||||
name: display_name,
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
|
||||
@@ -21,6 +21,8 @@ pub fn scan_one_shader(path: &Path, file_stem: &str, enabled: bool) -> ContentEn
|
||||
ContentEntry {
|
||||
file_stem: file_stem.to_owned(),
|
||||
name: file_stem.to_owned(),
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
|
||||
@@ -18,6 +18,8 @@ pub fn scan_one_world(path: &Path, file_stem: &str, enabled: bool) -> ContentEnt
|
||||
ContentEntry {
|
||||
name: file_stem.to_owned(),
|
||||
file_stem: file_stem.to_owned(),
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
|
||||
+26
-3
@@ -551,6 +551,14 @@ impl App {
|
||||
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||
return;
|
||||
};
|
||||
let installed_entries = match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => self.mods_state.entries.clone(),
|
||||
widgets::content::ContentTab::ResourcePacks => {
|
||||
self.resource_packs_state.entries.clone()
|
||||
}
|
||||
widgets::content::ContentTab::Shaders => self.shaders_state.entries.clone(),
|
||||
_ => return,
|
||||
};
|
||||
let state = match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => &mut self.mods_discovery_state,
|
||||
widgets::content::ContentTab::ResourcePacks => &mut self.resource_packs_discovery_state,
|
||||
@@ -560,13 +568,21 @@ impl App {
|
||||
let kind = state.kind;
|
||||
let query = state.search.query.clone();
|
||||
let request = state.begin_search(&instance);
|
||||
Self::spawn_discovery_request(instance, kind, query, request);
|
||||
Self::spawn_discovery_request(instance, kind, query, installed_entries, request);
|
||||
}
|
||||
|
||||
fn spawn_active_discovery_page(&mut self) {
|
||||
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||
return;
|
||||
};
|
||||
let installed_entries = match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => self.mods_state.entries.clone(),
|
||||
widgets::content::ContentTab::ResourcePacks => {
|
||||
self.resource_packs_state.entries.clone()
|
||||
}
|
||||
widgets::content::ContentTab::Shaders => self.shaders_state.entries.clone(),
|
||||
_ => return,
|
||||
};
|
||||
let state = match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => &mut self.mods_discovery_state,
|
||||
widgets::content::ContentTab::ResourcePacks => &mut self.resource_packs_discovery_state,
|
||||
@@ -578,13 +594,14 @@ impl App {
|
||||
let Some(request) = state.begin_next_page() else {
|
||||
return;
|
||||
};
|
||||
Self::spawn_discovery_request(instance, kind, query, request);
|
||||
Self::spawn_discovery_request(instance, kind, query, installed_entries, request);
|
||||
}
|
||||
|
||||
fn spawn_discovery_request(
|
||||
instance: crate::instance::InstanceConfig,
|
||||
kind: crate::net::modrinth::DiscoveryKind,
|
||||
query: String,
|
||||
installed_entries: Vec<crate::instance::ContentEntry>,
|
||||
request: widgets::content::discovery::DiscoveryRequest,
|
||||
) {
|
||||
let widgets::content::discovery::DiscoveryRequest {
|
||||
@@ -616,6 +633,10 @@ impl App {
|
||||
let icon_slots = std::sync::Arc::new(tokio::sync::Semaphore::new(8));
|
||||
for mut project in results.projects {
|
||||
returned_stems.insert(project.id.clone());
|
||||
let installed = widgets::content::discovery::project_is_installed(
|
||||
&project,
|
||||
&installed_entries,
|
||||
);
|
||||
let icon = (!loaded_icon_stems.contains(&project.id))
|
||||
.then(|| project.icon_url.take())
|
||||
.flatten()
|
||||
@@ -626,7 +647,9 @@ impl App {
|
||||
std::path::PathBuf::from(&project.id),
|
||||
)
|
||||
});
|
||||
if !stream.upsert(widgets::content::discovery::project_entry(project)) {
|
||||
if !stream.upsert(widgets::content::discovery::project_entry(
|
||||
project, installed,
|
||||
)) {
|
||||
break;
|
||||
}
|
||||
if let Some((url, file_stem, path)) = icon {
|
||||
|
||||
@@ -34,7 +34,7 @@ impl ContentMode {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::Installed => "Installed",
|
||||
Self::Discover => "Discover",
|
||||
Self::Discover => "Discovery",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -328,16 +328,13 @@ fn loader_slug(loader: ModLoader) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn project_entry(project: DiscoveryProject) -> ContentEntry {
|
||||
let description = format!(
|
||||
"{} • {} downloads",
|
||||
project.description,
|
||||
format_downloads(project.downloads)
|
||||
);
|
||||
pub(crate) fn project_entry(project: DiscoveryProject, installed: bool) -> ContentEntry {
|
||||
ContentEntry {
|
||||
file_stem: project.id.clone(),
|
||||
name: project.title,
|
||||
description,
|
||||
title_suffix: installed.then(|| "Installed".to_owned()),
|
||||
footer_label: Some(format!("{} downloads", format_downloads(project.downloads))),
|
||||
description: project.description,
|
||||
enabled: true,
|
||||
icon_bytes: project.icon_bytes,
|
||||
path: PathBuf::from(project.id),
|
||||
@@ -345,6 +342,39 @@ pub(crate) fn project_entry(project: DiscoveryProject) -> ContentEntry {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn project_is_installed(
|
||||
project: &DiscoveryProject,
|
||||
installed_entries: &[ContentEntry],
|
||||
) -> bool {
|
||||
installed_entries.iter().any(|entry| {
|
||||
identity_matches(&entry.name, &project.title)
|
||||
|| identity_matches(&entry.name, &project.slug)
|
||||
|| filename_matches_project(&entry.file_stem, &project.slug)
|
||||
|| filename_matches_project(&entry.file_stem, &project.title)
|
||||
})
|
||||
}
|
||||
|
||||
fn identity_matches(left: &str, right: &str) -> bool {
|
||||
let normalize = |value: &str| {
|
||||
value
|
||||
.chars()
|
||||
.filter(|character| character.is_alphanumeric())
|
||||
.flat_map(char::to_lowercase)
|
||||
.collect::<String>()
|
||||
};
|
||||
let right = normalize(right);
|
||||
!right.is_empty() && normalize(left) == right
|
||||
}
|
||||
|
||||
fn filename_matches_project(file_stem: &str, project_name: &str) -> bool {
|
||||
let file_stem = file_stem.to_lowercase();
|
||||
let project_name = project_name.to_lowercase().replace(' ', "-");
|
||||
file_stem == project_name
|
||||
|| file_stem
|
||||
.strip_prefix(&project_name)
|
||||
.is_some_and(|suffix| suffix.starts_with(['-', '_', '.']))
|
||||
}
|
||||
|
||||
fn format_downloads(downloads: u64) -> String {
|
||||
match downloads {
|
||||
1_000_000.. => format!("{:.1}M", downloads as f64 / 1_000_000.0),
|
||||
@@ -382,6 +412,53 @@ mod tests {
|
||||
assert_eq!(ContentMode::Discover.toggle(), ContentMode::Installed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_metadata_is_split_between_title_and_footer_badges() {
|
||||
let entry = project_entry(
|
||||
DiscoveryProject {
|
||||
id: "example".to_owned(),
|
||||
slug: "example".to_owned(),
|
||||
title: "Example".to_owned(),
|
||||
description: "Project description".to_owned(),
|
||||
downloads: 1_234,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
assert_eq!(entry.title_suffix.as_deref(), Some("Installed"));
|
||||
assert_eq!(entry.footer_label.as_deref(), Some("1.2K downloads"));
|
||||
assert_eq!(entry.description, "Project description");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installed_projects_match_metadata_names_and_versioned_filenames() {
|
||||
let project = DiscoveryProject {
|
||||
id: "P7dR8mSH".to_owned(),
|
||||
slug: "fabric-api".to_owned(),
|
||||
title: "Fabric API".to_owned(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
};
|
||||
let mut local = project_entry(project.clone(), false);
|
||||
local.name = "Fabric API".to_owned();
|
||||
local.file_stem = "unrelated-file".to_owned();
|
||||
assert!(project_is_installed(&project, &[local]));
|
||||
|
||||
let mut local = project_entry(project.clone(), false);
|
||||
local.name = "Unknown".to_owned();
|
||||
local.file_stem = "fabric-api-0.116.0+1.21.1".to_owned();
|
||||
assert!(project_is_installed(&project, &[local]));
|
||||
|
||||
let mut local = project_entry(project.clone(), false);
|
||||
local.name = "Fabric Language Kotlin".to_owned();
|
||||
local.file_stem = "fabric-language-kotlin".to_owned();
|
||||
assert!(!project_is_installed(&project, &[local]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changing_instance_invalidates_results() {
|
||||
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
|
||||
@@ -393,6 +470,20 @@ mod tests {
|
||||
assert!(state.needs_search(&second));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changing_instance_compatibility_invalidates_results() {
|
||||
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
|
||||
let original = instance("one", "1.21.1");
|
||||
let mut other_version = original.clone();
|
||||
other_version.game_version = "1.20.1".to_owned();
|
||||
let mut other_loader = original.clone();
|
||||
other_loader.loader = ModLoader::NeoForge;
|
||||
let _request = state.begin_search(&original);
|
||||
|
||||
assert!(state.needs_search(&other_version));
|
||||
assert!(state.needs_search(&other_loader));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_search_result_is_ignored() {
|
||||
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
|
||||
@@ -422,15 +513,18 @@ mod tests {
|
||||
let instance = instance("one", "1.21.1");
|
||||
let first = state.begin_search(&instance);
|
||||
for index in 0..PAGE_SIZE {
|
||||
assert!(first.stream.send(project_entry(DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
})));
|
||||
assert!(first.stream.send(project_entry(
|
||||
DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
DiscoveryState::push_result(
|
||||
@@ -456,15 +550,18 @@ mod tests {
|
||||
state.set_viewport_rows(90);
|
||||
let first = state.begin_search(&instance("one", "1.21.1"));
|
||||
for index in 0..PAGE_SIZE {
|
||||
assert!(first.stream.send(project_entry(DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
})));
|
||||
assert!(first.stream.send(project_entry(
|
||||
DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
DiscoveryState::push_result(
|
||||
@@ -486,15 +583,18 @@ mod tests {
|
||||
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
|
||||
let request = state.begin_search(&instance("one", "1.21.1"));
|
||||
for title in ["Sodium", "Lithium"] {
|
||||
assert!(request.stream.send(project_entry(DiscoveryProject {
|
||||
id: title.to_lowercase(),
|
||||
slug: title.to_lowercase(),
|
||||
title: title.to_owned(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
})));
|
||||
assert!(request.stream.send(project_entry(
|
||||
DiscoveryProject {
|
||||
id: title.to_lowercase(),
|
||||
slug: title.to_lowercase(),
|
||||
title: title.to_owned(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
|
||||
@@ -522,15 +622,18 @@ mod tests {
|
||||
let instance = instance("one", "1.21.1");
|
||||
let initial = state.begin_search(&instance);
|
||||
for title in ["Sodium", "Lithium"] {
|
||||
assert!(initial.stream.upsert(project_entry(DiscoveryProject {
|
||||
id: title.to_lowercase(),
|
||||
slug: title.to_lowercase(),
|
||||
title: title.to_owned(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: (title == "Sodium").then(|| vec![1, 2, 3]),
|
||||
})));
|
||||
assert!(initial.stream.upsert(project_entry(
|
||||
DiscoveryProject {
|
||||
id: title.to_lowercase(),
|
||||
slug: title.to_lowercase(),
|
||||
title: title.to_owned(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: (title == "Sodium").then(|| vec![1, 2, 3]),
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
state.search.query = "sodium".to_owned();
|
||||
@@ -551,15 +654,18 @@ mod tests {
|
||||
let instance = instance("one", "1.21.1");
|
||||
let first = state.begin_search(&instance);
|
||||
for index in 0..PAGE_SIZE {
|
||||
assert!(first.stream.upsert(project_entry(DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
})));
|
||||
assert!(first.stream.upsert(project_entry(
|
||||
DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
DiscoveryState::push_result(
|
||||
@@ -576,15 +682,18 @@ mod tests {
|
||||
|
||||
let second = state.begin_next_page().unwrap();
|
||||
for index in PAGE_SIZE..PAGE_SIZE * 2 {
|
||||
assert!(second.stream.upsert(project_entry(DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
})));
|
||||
assert!(second.stream.upsert(project_entry(
|
||||
DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
DiscoveryState::push_result(
|
||||
@@ -608,15 +717,18 @@ mod tests {
|
||||
let mut state = DiscoveryState::new(DiscoveryKind::Mod);
|
||||
let first = state.begin_search(&instance("one", "1.21.1"));
|
||||
for index in 0..PAGE_SIZE {
|
||||
assert!(first.stream.upsert(project_entry(DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
})));
|
||||
assert!(first.stream.upsert(project_entry(
|
||||
DiscoveryProject {
|
||||
id: index.to_string(),
|
||||
slug: index.to_string(),
|
||||
title: index.to_string(),
|
||||
description: String::new(),
|
||||
downloads: 0,
|
||||
icon_url: None,
|
||||
icon_bytes: None,
|
||||
},
|
||||
false
|
||||
)));
|
||||
}
|
||||
state.list.drain_pending();
|
||||
DiscoveryState::push_result(
|
||||
|
||||
@@ -1043,6 +1043,8 @@ pub fn render(
|
||||
let metadata = display_metadata.get(&entry.file_stem);
|
||||
let enabled = entry.enabled;
|
||||
let icon_pixels = &entry.icon_lines;
|
||||
let title_suffix = entry.title_suffix.as_deref();
|
||||
let footer_label = entry.footer_label.as_deref();
|
||||
// Keep rendering the terminal fallback until the asynchronous image
|
||||
// decoder has produced a protocol. Invalid and unsupported images
|
||||
// therefore remain visible as a question mark instead of blank space.
|
||||
@@ -1087,11 +1089,33 @@ pub fn render(
|
||||
stripe_bg,
|
||||
),
|
||||
};
|
||||
let title_suffix_style = Style::default()
|
||||
.fg(theme.background())
|
||||
.bg(theme.success())
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let footer_label_style = Style::default().fg(theme.text());
|
||||
|
||||
let has_icon = icon_pixels.is_some();
|
||||
let stripped_desc = metadata.map_or("", |metadata| metadata.description.as_str());
|
||||
let has_description = metadata.is_some_and(|metadata| metadata.has_description);
|
||||
let compact = !has_icon && !has_description;
|
||||
let rendered_icon_columns = if use_image_protocol && has_image {
|
||||
protocol_columns
|
||||
} else {
|
||||
icon_pixels
|
||||
.as_ref()
|
||||
.and_then(|rows| rows.first())
|
||||
.map_or(0, Vec::len)
|
||||
};
|
||||
let description_width = available_description_width(
|
||||
usize::from(context.cross_axis_size),
|
||||
rendered_icon_columns,
|
||||
has_icon,
|
||||
);
|
||||
let visible_description = ellipsize(
|
||||
stripped_desc,
|
||||
description_text_width(description_width, footer_label, has_description),
|
||||
);
|
||||
let compact = !has_icon && !has_description && footer_label.is_none();
|
||||
|
||||
let selector = if show_selected {
|
||||
Span::styled("\u{258c}", Style::default().fg(theme.accent()))
|
||||
@@ -1103,12 +1127,21 @@ pub fn render(
|
||||
let mut line = Vec::new();
|
||||
line.push(selector.clone());
|
||||
line.extend(searchable_spans(search, name, name_style, use_mc_colors));
|
||||
line.extend(title_suffix_spans(
|
||||
title_suffix,
|
||||
description_style,
|
||||
title_suffix_style,
|
||||
));
|
||||
|
||||
let item = Text::from(vec![Line::from(line)]).style(Style::default().bg(background));
|
||||
(item, 1)
|
||||
} else if has_icon {
|
||||
let icon_row_count = icon_pixels.as_ref().map(|r| r.len()).unwrap_or(0);
|
||||
let text_rows = if has_description { 2 } else { 1 }; // name + optional description
|
||||
let text_rows = if has_description || footer_label.is_some() {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let height = icon_row_count.max(text_rows) as u16;
|
||||
|
||||
let pad = if show_selected {
|
||||
@@ -1126,10 +1159,15 @@ pub fn render(
|
||||
));
|
||||
line_0.push(Span::raw(" "));
|
||||
line_0.extend(searchable_spans(search, name, name_style, use_mc_colors));
|
||||
line_0.extend(title_suffix_spans(
|
||||
title_suffix,
|
||||
description_style,
|
||||
title_suffix_style,
|
||||
));
|
||||
|
||||
let mut lines = vec![Line::from(line_0)];
|
||||
|
||||
if has_description {
|
||||
if has_description || footer_label.is_some() {
|
||||
let mut row = vec![pad.clone()];
|
||||
row.extend(icon_spans(
|
||||
icon_pixels.as_ref(),
|
||||
@@ -1138,12 +1176,19 @@ pub fn render(
|
||||
protocol_columns,
|
||||
));
|
||||
row.push(Span::raw(" "));
|
||||
row.extend(search.highlight_spans(stripped_desc, description_style));
|
||||
if has_description {
|
||||
row.extend(search.highlight_spans(&visible_description, description_style));
|
||||
}
|
||||
if let Some(footer_label) = footer_label {
|
||||
if has_description {
|
||||
row.push(Span::styled(" • ", description_style));
|
||||
}
|
||||
row.push(Span::styled(footer_label.to_owned(), footer_label_style));
|
||||
}
|
||||
lines.push(Line::from(row));
|
||||
}
|
||||
|
||||
let desc_rows = if has_description { 1 } else { 0 };
|
||||
for r in (1 + desc_rows)..icon_row_count {
|
||||
for r in text_rows..icon_row_count {
|
||||
let mut row = vec![pad.clone()];
|
||||
row.extend(icon_spans(
|
||||
icon_pixels.as_ref(),
|
||||
@@ -1160,17 +1205,31 @@ pub fn render(
|
||||
let mut line_0 = Vec::new();
|
||||
line_0.push(selector.clone());
|
||||
line_0.extend(searchable_spans(search, name, name_style, use_mc_colors));
|
||||
line_0.extend(title_suffix_spans(
|
||||
title_suffix,
|
||||
description_style,
|
||||
title_suffix_style,
|
||||
));
|
||||
|
||||
let mut lines = vec![Line::from(line_0)];
|
||||
|
||||
if has_description {
|
||||
if has_description || footer_label.is_some() {
|
||||
let pad = if show_selected {
|
||||
Span::styled("\u{258c}", Style::default().fg(theme.accent()))
|
||||
} else {
|
||||
Span::raw(" ")
|
||||
};
|
||||
let mut description = vec![pad];
|
||||
description.extend(search.highlight_spans(stripped_desc, description_style));
|
||||
if has_description {
|
||||
description
|
||||
.extend(search.highlight_spans(&visible_description, description_style));
|
||||
}
|
||||
if let Some(footer_label) = footer_label {
|
||||
if has_description {
|
||||
description.push(Span::styled(" • ", description_style));
|
||||
}
|
||||
description.push(Span::styled(footer_label.to_owned(), footer_label_style));
|
||||
}
|
||||
lines.push(Line::from(description));
|
||||
}
|
||||
|
||||
@@ -1427,6 +1486,62 @@ fn icon_spans(
|
||||
}
|
||||
}
|
||||
|
||||
fn title_suffix_spans(
|
||||
suffix: Option<&str>,
|
||||
spacing_style: Style,
|
||||
label_style: Style,
|
||||
) -> Vec<Span<'static>> {
|
||||
suffix.map_or_else(Vec::new, |suffix| {
|
||||
vec![
|
||||
Span::styled(" ", spacing_style),
|
||||
Span::styled(format!(" {suffix} "), label_style),
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
fn available_description_width(
|
||||
row_width: usize,
|
||||
rendered_icon_columns: usize,
|
||||
has_icon: bool,
|
||||
) -> usize {
|
||||
let selector_and_scrollbar = 2;
|
||||
let icon_and_gap = has_icon.then_some(rendered_icon_columns + 1).unwrap_or(0);
|
||||
row_width.saturating_sub(selector_and_scrollbar + icon_and_gap)
|
||||
}
|
||||
|
||||
fn description_text_width(
|
||||
available_width: usize,
|
||||
footer_label: Option<&str>,
|
||||
has_description: bool,
|
||||
) -> usize {
|
||||
let footer_width = footer_label.map_or(0, |footer_label| {
|
||||
Span::raw(footer_label).width() + if has_description { 3 } else { 0 }
|
||||
});
|
||||
available_width.saturating_sub(footer_width)
|
||||
}
|
||||
|
||||
fn ellipsize(text: &str, max_width: usize) -> String {
|
||||
if Span::raw(text).width() <= max_width {
|
||||
return text.to_owned();
|
||||
}
|
||||
if max_width <= 3 {
|
||||
return ".".repeat(max_width);
|
||||
}
|
||||
|
||||
let content_width = max_width - 3;
|
||||
let mut visible = String::new();
|
||||
for character in text.chars() {
|
||||
visible.push(character);
|
||||
if Span::raw(visible.as_str()).width() > content_width {
|
||||
visible.pop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
visible = visible.trim_end().to_owned();
|
||||
visible.push_str("...");
|
||||
visible
|
||||
}
|
||||
|
||||
fn protocol_icon_columns(
|
||||
entry: &crate::instance::content::mods::ContentEntry,
|
||||
picker: &ratatui_image::picker::Picker,
|
||||
@@ -1484,13 +1599,25 @@ mod tests {
|
||||
use std::{collections::HashSet, path::PathBuf};
|
||||
|
||||
use crate::instance::content::mods::ContentEntry;
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::Rect,
|
||||
style::{Color, Modifier, Style},
|
||||
text::{Line, Text},
|
||||
widgets::Widget,
|
||||
};
|
||||
|
||||
use super::{ContentListState, square_icon_columns};
|
||||
use super::{
|
||||
ContentListState, available_description_width, description_text_width, ellipsize,
|
||||
square_icon_columns, title_suffix_spans,
|
||||
};
|
||||
|
||||
fn entry(name: &str) -> ContentEntry {
|
||||
ContentEntry {
|
||||
file_stem: name.to_lowercase(),
|
||||
name: name.to_owned(),
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description: String::new(),
|
||||
enabled: true,
|
||||
icon_bytes: None,
|
||||
@@ -1511,6 +1638,70 @@ mod tests {
|
||||
assert_eq!(square_icon_columns(3, (0, 0)), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn title_badge_is_rendered_after_a_small_gap() {
|
||||
let label_style = Style::default()
|
||||
.fg(Color::Black)
|
||||
.bg(Color::Cyan)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let spans = title_suffix_spans(Some("Installed"), Style::default(), label_style);
|
||||
let text = spans
|
||||
.iter()
|
||||
.map(|span| span.content.as_ref())
|
||||
.collect::<Vec<_>>()
|
||||
.concat();
|
||||
|
||||
assert_eq!(text, " Installed ");
|
||||
assert_eq!(spans[1].style, label_style);
|
||||
assert!(title_suffix_spans(None, Style::default(), label_style).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn title_suffix_keeps_label_style_after_the_row_background_is_applied() {
|
||||
let label_style = Style::default()
|
||||
.fg(Color::Black)
|
||||
.bg(Color::Cyan)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let text = Text::from(Line::from(title_suffix_spans(
|
||||
Some("downloads"),
|
||||
Style::default(),
|
||||
label_style,
|
||||
)))
|
||||
.style(Style::default().bg(Color::Black));
|
||||
let area = Rect::new(0, 0, 20, 1);
|
||||
let mut buffer = Buffer::empty(area);
|
||||
|
||||
text.render(area, &mut buffer);
|
||||
|
||||
let label_cell = buffer.cell((5, 0)).unwrap();
|
||||
assert_eq!(label_cell.fg, Color::Black);
|
||||
assert_eq!(label_cell.bg, Color::Cyan);
|
||||
assert!(label_cell.modifier.contains(Modifier::BOLD));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn descriptions_are_ellipsized_to_the_available_cell_width() {
|
||||
assert_eq!(ellipsize("short", 5), "short");
|
||||
assert_eq!(ellipsize("a longer description", 10), "a longe...");
|
||||
assert_eq!(ellipsize("narrow", 3), "...");
|
||||
assert_eq!(ellipsize("narrow", 2), "..");
|
||||
assert_eq!(ellipsize("界界界", 5), "界...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn description_width_reserves_the_row_chrome() {
|
||||
assert_eq!(available_description_width(100, 6, true), 91);
|
||||
assert_eq!(available_description_width(100, 0, false), 98);
|
||||
assert_eq!(available_description_width(4, 6, true), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn description_width_reserves_the_download_metadata() {
|
||||
assert_eq!(description_text_width(40, Some("1.2K downloads"), true), 23);
|
||||
assert_eq!(description_text_width(10, Some("1.2K downloads"), true), 0);
|
||||
assert_eq!(description_text_width(40, None, true), 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn content_stream_inserts_entries_and_icons_incrementally() {
|
||||
let mut state = ContentListState::default();
|
||||
|
||||
@@ -88,6 +88,10 @@ fn visible_tabs(mode: ContentMode) -> &'static [ContentTab] {
|
||||
}
|
||||
}
|
||||
|
||||
fn mode_label(mode: ContentMode) -> String {
|
||||
format!(" {} ", mode.label())
|
||||
}
|
||||
|
||||
fn cycle_tab(current: ContentTab, tabs: &[ContentTab], forward: bool) -> ContentTab {
|
||||
let index = tabs.iter().position(|tab| *tab == current).unwrap_or(0);
|
||||
if forward {
|
||||
@@ -181,16 +185,16 @@ pub fn render(
|
||||
}
|
||||
};
|
||||
|
||||
let mode_color = match mode {
|
||||
let mode_background = match mode {
|
||||
ContentMode::Installed => theme.success(),
|
||||
ContentMode::Discover => theme.accent(),
|
||||
ContentMode::Discover => theme.info(),
|
||||
};
|
||||
let mut content_titles = vec![
|
||||
Span::styled(
|
||||
format!(" {} ", mode.label()),
|
||||
mode_label(mode),
|
||||
Style::default()
|
||||
.fg(theme.background())
|
||||
.bg(mode_color)
|
||||
.bg(mode_background)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::raw(" "),
|
||||
@@ -628,6 +632,14 @@ pub fn title(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn mode_labels_have_the_same_rendered_width() {
|
||||
assert_eq!(
|
||||
mode_label(ContentMode::Installed).chars().count(),
|
||||
mode_label(ContentMode::Discover).chars().count()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discovery_navigation_only_cycles_downloadable_tabs() {
|
||||
assert_eq!(
|
||||
|
||||
Reference in new issue
Block a user