feat(tui): add resource packs and shaders tabs
This commit is contained in:
1 parent
8749f8361a
commit
794fb4d08c
10 files changed
+877
-211
No files matched your search
@@ -3,9 +3,13 @@ pub mod manager;
|
||||
pub mod loader;
|
||||
pub mod models;
|
||||
pub mod mods;
|
||||
pub mod resource_packs;
|
||||
pub mod shaders;
|
||||
|
||||
pub use launch::LaunchError;
|
||||
pub use loader::{get_installer, GameVersion, ModLoaderInstaller, VanillaInstaller};
|
||||
pub use manager::{InstanceError, InstanceManager};
|
||||
pub use models::{InstanceConfig, ModLoader};
|
||||
pub use mods::{scan_mods, toggle_mod, ModEntry};
|
||||
pub use resource_packs::scan_resource_packs;
|
||||
pub use shaders::scan_shaders;
|
||||
@@ -60,7 +60,7 @@ pub fn scan_mods(instances_dir: &Path, instance_name: &str) -> Vec<ModEntry> {
|
||||
let (name, description, icon_bytes) = read_mod_metadata(&path);
|
||||
let icon_lines = icon_bytes
|
||||
.as_ref()
|
||||
.and_then(|bytes| make_icon_pixels(bytes, 4, 2));
|
||||
.and_then(|bytes| make_icon_pixels(bytes, 6, 3));
|
||||
|
||||
entries.push(ModEntry {
|
||||
file_stem: file_stem.clone(),
|
||||
@@ -116,7 +116,11 @@ fn read_zip_bytes(archive: &mut zip::ZipArchive<std::fs::File>, path: &str) -> O
|
||||
Some(bytes)
|
||||
}
|
||||
|
||||
fn make_icon_pixels(bytes: &[u8], width: u16, height: u16) -> Option<Vec<Vec<IconCell>>> {
|
||||
pub(crate) fn make_icon_pixels(
|
||||
bytes: &[u8],
|
||||
width: u16,
|
||||
height: u16,
|
||||
) -> Option<Vec<Vec<IconCell>>> {
|
||||
let img = image::load_from_memory(bytes).ok()?;
|
||||
let resized = img.resize_exact(
|
||||
u32::from(width),
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::mods::{make_icon_pixels, ModEntry};
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub(crate) struct PackMcMeta {
|
||||
#[serde(default)]
|
||||
pub pack: PackInfo,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub(crate) struct PackInfo {
|
||||
#[serde(default)]
|
||||
pub description: serde_json::Value,
|
||||
}
|
||||
|
||||
pub(crate) fn extract_description(value: &serde_json::Value) -> String {
|
||||
match value {
|
||||
serde_json::Value::String(s) => s.clone(),
|
||||
serde_json::Value::Object(obj) => obj
|
||||
.get("text")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string(),
|
||||
serde_json::Value::Array(arr) => arr
|
||||
.iter()
|
||||
.filter_map(|v| match v {
|
||||
serde_json::Value::String(s) => Some(s.as_str()),
|
||||
serde_json::Value::Object(obj) => obj.get("text").and_then(|v| v.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(""),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scan_resource_packs(instances_dir: &Path, instance_name: &str) -> Vec<ModEntry> {
|
||||
let packs_dir = instances_dir
|
||||
.join(instance_name)
|
||||
.join(".minecraft")
|
||||
.join("resourcepacks");
|
||||
|
||||
let read_dir = match std::fs::read_dir(&packs_dir) {
|
||||
Ok(rd) => rd,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for entry in read_dir.flatten() {
|
||||
let path = entry.path();
|
||||
let file_name = match path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let is_dir = path.is_dir();
|
||||
let (enabled, file_stem) = if is_dir {
|
||||
if file_name.ends_with(".disabled") {
|
||||
(false, file_name.trim_end_matches(".disabled").to_string())
|
||||
} else {
|
||||
(true, file_name.clone())
|
||||
}
|
||||
} else if file_name.ends_with(".zip") {
|
||||
(true, file_name.trim_end_matches(".zip").to_string())
|
||||
} else if file_name.ends_with(".zip.disabled") {
|
||||
(
|
||||
false,
|
||||
file_name.trim_end_matches(".zip.disabled").to_string(),
|
||||
)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let (name, description, icon_bytes) = if is_dir {
|
||||
read_pack_metadata_from_dir(&path)
|
||||
} else {
|
||||
read_pack_metadata_from_zip(&path)
|
||||
};
|
||||
|
||||
let icon_lines = icon_bytes
|
||||
.as_ref()
|
||||
.and_then(|bytes| make_icon_pixels(bytes, 6, 3));
|
||||
|
||||
entries.push(ModEntry {
|
||||
file_stem: file_stem.clone(),
|
||||
name: if name.is_empty() { file_stem } else { name },
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
path,
|
||||
icon_lines,
|
||||
});
|
||||
}
|
||||
|
||||
entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
entries
|
||||
}
|
||||
|
||||
fn read_pack_metadata_from_zip(zip_path: &Path) -> (String, String, Option<Vec<u8>>) {
|
||||
let file = match std::fs::File::open(zip_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return (String::new(), String::new(), None),
|
||||
};
|
||||
|
||||
let mut archive = match zip::ZipArchive::new(file) {
|
||||
Ok(a) => a,
|
||||
Err(_) => return (String::new(), String::new(), None),
|
||||
};
|
||||
|
||||
let description = read_pack_description(&mut archive);
|
||||
|
||||
let icon_bytes = {
|
||||
let mut buf = Vec::new();
|
||||
match archive.by_name("pack.png") {
|
||||
Ok(mut entry) => {
|
||||
if entry.read_to_end(&mut buf).is_ok() {
|
||||
Some(buf)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
};
|
||||
|
||||
(String::new(), description, icon_bytes)
|
||||
}
|
||||
|
||||
fn read_pack_description(archive: &mut zip::ZipArchive<std::fs::File>) -> String {
|
||||
let entry = match archive.by_name("pack.mcmeta") {
|
||||
Ok(e) => e,
|
||||
Err(_) => return String::new(),
|
||||
};
|
||||
|
||||
let meta: PackMcMeta = match serde_json::from_reader(entry) {
|
||||
Ok(m) => m,
|
||||
Err(_) => return String::new(),
|
||||
};
|
||||
|
||||
extract_description(&meta.pack.description)
|
||||
}
|
||||
|
||||
fn read_pack_metadata_from_dir(dir: &Path) -> (String, String, Option<Vec<u8>>) {
|
||||
let actual_dir = if dir
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.is_some_and(|n| n.ends_with(".disabled"))
|
||||
{
|
||||
dir.to_path_buf()
|
||||
} else {
|
||||
dir.to_path_buf()
|
||||
};
|
||||
|
||||
let description = {
|
||||
let mcmeta_path = actual_dir.join("pack.mcmeta");
|
||||
match std::fs::read_to_string(&mcmeta_path) {
|
||||
Ok(content) => match serde_json::from_str::<PackMcMeta>(&content) {
|
||||
Ok(meta) => extract_description(&meta.pack.description),
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
Err(_) => String::new(),
|
||||
}
|
||||
};
|
||||
|
||||
let icon_bytes = {
|
||||
let icon_path = actual_dir.join("pack.png");
|
||||
std::fs::read(&icon_path).ok()
|
||||
};
|
||||
|
||||
(String::new(), description, icon_bytes)
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use super::mods::{make_icon_pixels, ModEntry};
|
||||
use super::resource_packs::{extract_description, PackMcMeta};
|
||||
|
||||
pub fn scan_shaders(instances_dir: &Path, instance_name: &str) -> Vec<ModEntry> {
|
||||
let shaders_dir = instances_dir
|
||||
.join(instance_name)
|
||||
.join(".minecraft")
|
||||
.join("shaderpacks");
|
||||
|
||||
let read_dir = match std::fs::read_dir(&shaders_dir) {
|
||||
Ok(rd) => rd,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
let mut entries = Vec::new();
|
||||
|
||||
for entry in read_dir.flatten() {
|
||||
let path = entry.path();
|
||||
let file_name = match path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n.to_string(),
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let is_dir = path.is_dir();
|
||||
let (enabled, file_stem) = if is_dir {
|
||||
if file_name.ends_with(".disabled") {
|
||||
(false, file_name.trim_end_matches(".disabled").to_string())
|
||||
} else {
|
||||
(true, file_name.clone())
|
||||
}
|
||||
} else if file_name.ends_with(".zip") {
|
||||
(true, file_name.trim_end_matches(".zip").to_string())
|
||||
} else if file_name.ends_with(".zip.disabled") {
|
||||
(
|
||||
false,
|
||||
file_name.trim_end_matches(".zip.disabled").to_string(),
|
||||
)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
let (description, icon_bytes) = if is_dir {
|
||||
read_shader_metadata_from_dir(&path)
|
||||
} else {
|
||||
read_shader_metadata_from_zip(&path)
|
||||
};
|
||||
|
||||
let icon_lines = icon_bytes
|
||||
.as_ref()
|
||||
.and_then(|bytes| make_icon_pixels(bytes, 6, 3));
|
||||
|
||||
entries.push(ModEntry {
|
||||
file_stem: file_stem.clone(),
|
||||
name: file_stem,
|
||||
description,
|
||||
enabled,
|
||||
icon_bytes,
|
||||
path,
|
||||
icon_lines,
|
||||
});
|
||||
}
|
||||
|
||||
entries.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
|
||||
entries
|
||||
}
|
||||
|
||||
fn read_shader_metadata_from_zip(zip_path: &Path) -> (String, Option<Vec<u8>>) {
|
||||
let file = match std::fs::File::open(zip_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return (String::new(), None),
|
||||
};
|
||||
|
||||
let mut archive = match zip::ZipArchive::new(file) {
|
||||
Ok(a) => a,
|
||||
Err(_) => return (String::new(), None),
|
||||
};
|
||||
|
||||
let description = match archive.by_name("pack.mcmeta") {
|
||||
Ok(entry) => match serde_json::from_reader::<_, PackMcMeta>(entry) {
|
||||
Ok(meta) => extract_description(&meta.pack.description),
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
let icon_bytes = {
|
||||
let mut buf = Vec::new();
|
||||
match archive.by_name("pack.png") {
|
||||
Ok(mut entry) => {
|
||||
if entry.read_to_end(&mut buf).is_ok() {
|
||||
Some(buf)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
};
|
||||
|
||||
(description, icon_bytes)
|
||||
}
|
||||
|
||||
fn read_shader_metadata_from_dir(dir: &Path) -> (String, Option<Vec<u8>>) {
|
||||
let description = match std::fs::read_to_string(dir.join("pack.mcmeta")) {
|
||||
Ok(content) => match serde_json::from_str::<PackMcMeta>(&content) {
|
||||
Ok(meta) => extract_description(&meta.pack.description),
|
||||
Err(_) => String::new(),
|
||||
},
|
||||
Err(_) => String::new(),
|
||||
};
|
||||
|
||||
let icon_bytes = std::fs::read(dir.join("pack.png")).ok();
|
||||
|
||||
(description, icon_bytes)
|
||||
}
|
||||
+30
-13
@@ -31,7 +31,9 @@ pub struct App {
|
||||
pre_overlay_focused: FocusedArea,
|
||||
content_tab: widgets::content::ContentTab,
|
||||
profiles_state: profiles::State,
|
||||
mods_state: widgets::mods_list::ModsState,
|
||||
mods_state: widgets::content_list::ContentListState,
|
||||
resource_packs_state: widgets::content_list::ContentListState,
|
||||
shaders_state: widgets::content_list::ContentListState,
|
||||
instance_manager: InstanceManager,
|
||||
log_list_state: tui_logger::TuiWidgetState,
|
||||
throbber_state: throbber_widgets_tui::ThrobberState,
|
||||
@@ -88,7 +90,9 @@ impl Default for App {
|
||||
pre_overlay_focused: FocusedArea::default(),
|
||||
content_tab: widgets::content::ContentTab::default(),
|
||||
profiles_state,
|
||||
mods_state: widgets::mods_list::ModsState::default(),
|
||||
mods_state: widgets::content_list::ContentListState::default(),
|
||||
resource_packs_state: widgets::content_list::ContentListState::default(),
|
||||
shaders_state: widgets::content_list::ContentListState::default(),
|
||||
instance_manager: manager,
|
||||
log_list_state: tui_logger::TuiWidgetState::new()
|
||||
.set_default_display_level(log::LevelFilter::Debug),
|
||||
@@ -110,6 +114,9 @@ impl App {
|
||||
|
||||
self.drain_pending_instances();
|
||||
self.drain_pending_last_played();
|
||||
self.mods_state.drain_pending();
|
||||
self.resource_packs_state.drain_pending();
|
||||
self.shaders_state.drain_pending();
|
||||
self.throbber_tick = self.throbber_tick.wrapping_add(1);
|
||||
if self.throbber_tick % 8 == 0 {
|
||||
self.throbber_state.calc_next();
|
||||
@@ -155,6 +162,8 @@ impl App {
|
||||
self.content_tab,
|
||||
self.profiles_state.selected_instance(),
|
||||
&mut self.mods_state,
|
||||
&mut self.resource_packs_state,
|
||||
&mut self.shaders_state,
|
||||
&self.instance_manager.instances_dir,
|
||||
);
|
||||
|
||||
@@ -279,15 +288,19 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
if self.focused == FocusedArea::Content
|
||||
&& self.content_tab == widgets::content::ContentTab::Mods
|
||||
{
|
||||
if widgets::mods_list::handle_key(
|
||||
&key_event,
|
||||
&mut self.mods_state,
|
||||
&self.instance_manager.instances_dir,
|
||||
) {
|
||||
return Ok(());
|
||||
if self.focused == FocusedArea::Content {
|
||||
let state = match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => Some(&mut self.mods_state),
|
||||
widgets::content::ContentTab::ResourcePacks => {
|
||||
Some(&mut self.resource_packs_state)
|
||||
}
|
||||
widgets::content::ContentTab::Shaders => Some(&mut self.shaders_state),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(state) = state {
|
||||
if widgets::content_list::handle_key(&key_event, state) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,10 +324,14 @@ impl App {
|
||||
self.pre_overlay_focused = self.focused;
|
||||
self.focused = FocusedArea::StatusExpanded;
|
||||
}
|
||||
KeyCode::Tab if self.focused == FocusedArea::Content => {
|
||||
KeyCode::Tab | KeyCode::Char('l') | KeyCode::Right
|
||||
if self.focused == FocusedArea::Content =>
|
||||
{
|
||||
self.content_tab = self.content_tab.next();
|
||||
}
|
||||
KeyCode::BackTab if self.focused == FocusedArea::Content => {
|
||||
KeyCode::BackTab | KeyCode::Char('h') | KeyCode::Left
|
||||
if self.focused == FocusedArea::Content =>
|
||||
{
|
||||
self.content_tab = self.content_tab.previous();
|
||||
}
|
||||
KeyCode::Char('d')
|
||||
|
||||
@@ -68,10 +68,14 @@ pub fn render(
|
||||
focused: FocusedArea,
|
||||
tab: ContentTab,
|
||||
instance: Option<&crate::instance::InstanceConfig>,
|
||||
mods_state: &mut super::mods_list::ModsState,
|
||||
mods_state: &mut super::content_list::ContentListState,
|
||||
resource_packs_state: &mut super::content_list::ContentListState,
|
||||
shaders_state: &mut super::content_list::ContentListState,
|
||||
instances_dir: &std::path::Path,
|
||||
) {
|
||||
let border_color = if focused == FocusedArea::Content {
|
||||
let is_focused = focused == FocusedArea::Content;
|
||||
|
||||
let border_color = if is_focused {
|
||||
THEME.colors.border_focused
|
||||
} else {
|
||||
THEME.colors.border_unfocused
|
||||
@@ -107,9 +111,70 @@ pub fn render(
|
||||
ContentTab::Mods => {
|
||||
if let Some(instance) = instance {
|
||||
if mods_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
mods_state.load_mods(instances_dir, &instance.name);
|
||||
mods_state.start_load(
|
||||
instances_dir,
|
||||
&instance.name,
|
||||
crate::instance::scan_mods,
|
||||
);
|
||||
}
|
||||
super::mods_list::render(frame, content_area, mods_state);
|
||||
super::content_list::render(
|
||||
frame,
|
||||
content_area,
|
||||
mods_state,
|
||||
is_focused,
|
||||
"Loading mods...",
|
||||
"No mods installed.",
|
||||
);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(THEME.colors.text_idle)),
|
||||
content_area,
|
||||
);
|
||||
}
|
||||
}
|
||||
ContentTab::ResourcePacks => {
|
||||
if let Some(instance) = instance {
|
||||
if resource_packs_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
resource_packs_state.start_load(
|
||||
instances_dir,
|
||||
&instance.name,
|
||||
crate::instance::scan_resource_packs,
|
||||
);
|
||||
}
|
||||
super::content_list::render(
|
||||
frame,
|
||||
content_area,
|
||||
resource_packs_state,
|
||||
is_focused,
|
||||
"Loading resource packs...",
|
||||
"No resource packs installed.",
|
||||
);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
.style(Style::default().fg(THEME.colors.text_idle)),
|
||||
content_area,
|
||||
);
|
||||
}
|
||||
}
|
||||
ContentTab::Shaders => {
|
||||
if let Some(instance) = instance {
|
||||
if shaders_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
shaders_state.start_load(
|
||||
instances_dir,
|
||||
&instance.name,
|
||||
crate::instance::scan_shaders,
|
||||
);
|
||||
}
|
||||
super::content_list::render(
|
||||
frame,
|
||||
content_area,
|
||||
shaders_state,
|
||||
is_focused,
|
||||
"Loading shaders...",
|
||||
"No shaders installed.",
|
||||
);
|
||||
} else {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No instance selected.")
|
||||
@@ -139,12 +204,9 @@ pub fn render(
|
||||
}
|
||||
_ => {
|
||||
let body = match tab {
|
||||
ContentTab::Mods => unreachable!(),
|
||||
ContentTab::ResourcePacks => "No resource packs installed.",
|
||||
ContentTab::Shaders => "No shaders installed.",
|
||||
ContentTab::Screenshots => "No screenshots.",
|
||||
ContentTab::Worlds => "No worlds saved.",
|
||||
ContentTab::Logs => unreachable!(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
frame.render_widget(
|
||||
|
||||
@@ -0,0 +1,471 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
style::{Color, Modifier, Style},
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState},
|
||||
Frame,
|
||||
};
|
||||
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
|
||||
|
||||
use crate::instance::mods::ModEntry;
|
||||
use crate::tui::theme::THEME;
|
||||
|
||||
type IconCell = (u8, u8, u8, u8, u8, u8);
|
||||
|
||||
struct CachedList {
|
||||
entries: Vec<ModEntry>,
|
||||
selected: Option<usize>,
|
||||
}
|
||||
|
||||
pub struct ContentListState {
|
||||
pub entries: Vec<ModEntry>,
|
||||
pub list_state: TuiListState,
|
||||
pub scrollbar_state: ScrollbarState,
|
||||
pub loaded_for: Option<String>,
|
||||
pub loading: bool,
|
||||
cache: HashMap<String, CachedList>,
|
||||
pending: Arc<Mutex<Option<(String, Vec<ModEntry>)>>>,
|
||||
}
|
||||
|
||||
impl Default for ContentListState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
entries: Vec::new(),
|
||||
list_state: TuiListState::default(),
|
||||
scrollbar_state: ScrollbarState::default(),
|
||||
loaded_for: None,
|
||||
loading: false,
|
||||
cache: HashMap::new(),
|
||||
pending: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ContentListState {
|
||||
pub fn start_load<F>(&mut self, instances_dir: &Path, instance_name: &str, scan_fn: F)
|
||||
where
|
||||
F: FnOnce(&Path, &str) -> Vec<ModEntry> + Send + 'static,
|
||||
{
|
||||
if let Some(prev) = self.loaded_for.take() {
|
||||
if !self.entries.is_empty() {
|
||||
self.cache.insert(
|
||||
prev,
|
||||
CachedList {
|
||||
entries: std::mem::take(&mut self.entries),
|
||||
selected: self.list_state.selected,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(cached) = self.cache.remove(instance_name) {
|
||||
self.entries = cached.entries;
|
||||
self.list_state.selected = cached.selected;
|
||||
self.loading = false;
|
||||
} else {
|
||||
self.entries.clear();
|
||||
self.list_state = TuiListState::default();
|
||||
self.loading = true;
|
||||
}
|
||||
|
||||
self.loaded_for = Some(instance_name.to_string());
|
||||
self.update_scrollbar();
|
||||
|
||||
let dir = instances_dir.to_path_buf();
|
||||
let tag = instance_name.to_string();
|
||||
let pending = self.pending.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let scan_dir = dir.clone();
|
||||
let scan_name = tag.clone();
|
||||
let entries = tokio::task::spawn_blocking(move || scan_fn(&scan_dir, &scan_name))
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
if let Ok(mut slot) = pending.lock() {
|
||||
*slot = Some((tag, entries));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn drain_pending(&mut self) {
|
||||
let taken = if let Ok(mut slot) = self.pending.lock() {
|
||||
slot.take()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some((instance_name, entries)) = taken {
|
||||
if self.loaded_for.as_deref() == Some(&instance_name) || instance_name.is_empty() {
|
||||
let selected_name = self
|
||||
.list_state
|
||||
.selected
|
||||
.and_then(|i| self.entries.get(i))
|
||||
.map(|e| e.name.clone());
|
||||
|
||||
self.entries = entries;
|
||||
self.loading = false;
|
||||
|
||||
if !self.entries.is_empty() {
|
||||
let new_index = selected_name
|
||||
.and_then(|name| self.entries.iter().position(|e| e.name == name))
|
||||
.or(self.list_state.selected)
|
||||
.map(|i| i.min(self.entries.len().saturating_sub(1)))
|
||||
.unwrap_or(0);
|
||||
self.list_state.selected = Some(new_index);
|
||||
} else {
|
||||
self.list_state.selected = None;
|
||||
}
|
||||
self.update_scrollbar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_scrollbar(&mut self) {
|
||||
let count = self.entries.len();
|
||||
let max = count.saturating_sub(1);
|
||||
let pos = self.list_state.selected.unwrap_or(0);
|
||||
self.scrollbar_state = ScrollbarState::new(max).position(pos);
|
||||
}
|
||||
|
||||
pub fn toggle_selected(&mut self) {
|
||||
let Some(index) = self.list_state.selected else {
|
||||
return;
|
||||
};
|
||||
let Some(entry) = self.entries.get(index) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let new_path = if entry.enabled {
|
||||
let fname = match entry.path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n,
|
||||
None => return,
|
||||
};
|
||||
let mut p = entry.path.clone();
|
||||
p.set_file_name(format!("{fname}.disabled"));
|
||||
p
|
||||
} else {
|
||||
let fname = match entry.path.file_name().and_then(|n| n.to_str()) {
|
||||
Some(n) => n,
|
||||
None => return,
|
||||
};
|
||||
let mut p = entry.path.clone();
|
||||
p.set_file_name(fname.trim_end_matches(".disabled"));
|
||||
p
|
||||
};
|
||||
|
||||
match std::fs::rename(&entry.path, &new_path) {
|
||||
Ok(()) => {
|
||||
let entry = &mut self.entries[index];
|
||||
entry.enabled = !entry.enabled;
|
||||
entry.path = new_path;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to toggle '{}': {}", entry.file_stem, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the key was consumed.
|
||||
pub fn handle_key(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
match key_event.code {
|
||||
KeyCode::Char('j') | KeyCode::Down => {
|
||||
let count = state.entries.len();
|
||||
if count == 0 {
|
||||
return true;
|
||||
}
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some((current + 1).min(count - 1));
|
||||
state.update_scrollbar();
|
||||
true
|
||||
}
|
||||
KeyCode::Char('k') | KeyCode::Up => {
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some(current.saturating_sub(1));
|
||||
state.update_scrollbar();
|
||||
true
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
state.toggle_selected();
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
state: &mut ContentListState,
|
||||
is_focused: bool,
|
||||
loading_text: &str,
|
||||
empty_text: &str,
|
||||
) {
|
||||
if state.loading {
|
||||
frame.render_widget(
|
||||
Paragraph::new(loading_text).style(Style::default().fg(THEME.colors.text_idle)),
|
||||
area,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if state.entries.is_empty() {
|
||||
frame.render_widget(
|
||||
Paragraph::new(empty_text).style(Style::default().fg(THEME.colors.text_idle)),
|
||||
area,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let count = state.entries.len();
|
||||
|
||||
let snapshot: Vec<(String, String, bool, Option<Vec<Vec<IconCell>>>)> = state
|
||||
.entries
|
||||
.iter()
|
||||
.map(|entry| {
|
||||
(
|
||||
entry.name.clone(),
|
||||
entry.description.clone(),
|
||||
entry.enabled,
|
||||
entry.icon_lines.clone(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let builder = ListBuilder::new(move |context| {
|
||||
let (name, description, enabled, icon_pixels) = &snapshot[context.index];
|
||||
let show_selected = is_focused && context.is_selected;
|
||||
let use_mc_colors = *enabled && !show_selected;
|
||||
|
||||
let (name_style, description_style, background) = match (*enabled, show_selected) {
|
||||
(true, true) => (
|
||||
Style::default()
|
||||
.fg(THEME.colors.row_highlight)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.row_highlight),
|
||||
THEME.colors.row_background,
|
||||
),
|
||||
(true, false) => (
|
||||
Style::default()
|
||||
.fg(THEME.colors.foreground)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
THEME.colors.row_alternate_bg,
|
||||
),
|
||||
(false, true) => (
|
||||
Style::default()
|
||||
.fg(THEME.colors.row_highlight)
|
||||
.add_modifier(Modifier::CROSSED_OUT),
|
||||
Style::default().fg(THEME.colors.row_highlight),
|
||||
THEME.colors.row_background,
|
||||
),
|
||||
(false, false) => (
|
||||
Style::default()
|
||||
.fg(THEME.colors.text_idle)
|
||||
.add_modifier(Modifier::CROSSED_OUT),
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
THEME.colors.row_alternate_bg,
|
||||
),
|
||||
};
|
||||
|
||||
let has_icon = icon_pixels.is_some();
|
||||
let stripped_desc = strip_mc_codes(description);
|
||||
let has_description = !stripped_desc.trim().is_empty();
|
||||
let compact = !has_icon && !has_description;
|
||||
|
||||
if compact {
|
||||
let mut line = Vec::new();
|
||||
line.push(Span::raw(" "));
|
||||
if use_mc_colors {
|
||||
line.extend(parse_mc_text(name, name_style));
|
||||
} else {
|
||||
line.push(Span::styled(strip_mc_codes(name), name_style));
|
||||
}
|
||||
|
||||
let item = Text::from(vec![Line::from(line)]).style(Style::default().bg(background));
|
||||
(item, 1)
|
||||
} else if has_icon {
|
||||
let mut line_0 = icon_spans(icon_pixels.as_ref(), 0);
|
||||
line_0.push(Span::raw(" "));
|
||||
if use_mc_colors {
|
||||
line_0.extend(parse_mc_text(name, name_style));
|
||||
} else {
|
||||
line_0.push(Span::styled(strip_mc_codes(name), name_style));
|
||||
}
|
||||
|
||||
let mut line_1 = icon_spans(icon_pixels.as_ref(), 1);
|
||||
line_1.push(Span::raw(" "));
|
||||
if use_mc_colors {
|
||||
line_1.extend(parse_mc_text(description, description_style));
|
||||
} else {
|
||||
line_1.push(Span::styled(stripped_desc, description_style));
|
||||
}
|
||||
|
||||
let line_2 = icon_spans(icon_pixels.as_ref(), 2);
|
||||
|
||||
let item = Text::from(vec![
|
||||
Line::from(line_0),
|
||||
Line::from(line_1),
|
||||
Line::from(line_2),
|
||||
])
|
||||
.style(Style::default().bg(background));
|
||||
(item, 3)
|
||||
} else {
|
||||
let mut line_0 = Vec::new();
|
||||
line_0.push(Span::raw(" "));
|
||||
if use_mc_colors {
|
||||
line_0.extend(parse_mc_text(name, name_style));
|
||||
} else {
|
||||
line_0.push(Span::styled(strip_mc_codes(name), name_style));
|
||||
}
|
||||
|
||||
let mut line_1 = Vec::new();
|
||||
line_1.push(Span::raw(" "));
|
||||
if use_mc_colors {
|
||||
line_1.extend(parse_mc_text(description, description_style));
|
||||
} else {
|
||||
line_1.push(Span::styled(stripped_desc, description_style));
|
||||
}
|
||||
|
||||
let item = Text::from(vec![Line::from(line_0), Line::from(line_1)])
|
||||
.style(Style::default().bg(background));
|
||||
(item, 2)
|
||||
}
|
||||
});
|
||||
|
||||
let list = ListView::new(builder, count);
|
||||
frame.render_stateful_widget(list, area, &mut state.list_state);
|
||||
|
||||
let scrollbar_area = Rect {
|
||||
x: area.x + area.width.saturating_sub(0),
|
||||
y: area.y + 1,
|
||||
width: 1,
|
||||
height: area.height.saturating_sub(2),
|
||||
};
|
||||
frame.render_stateful_widget(
|
||||
Scrollbar::default()
|
||||
.orientation(ScrollbarOrientation::VerticalRight)
|
||||
.begin_symbol(Some("\u{25b2}"))
|
||||
.style(
|
||||
Style::default()
|
||||
.fg(THEME.colors.border_focused)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
)
|
||||
.thumb_symbol("\u{2551}")
|
||||
.track_symbol(Some(""))
|
||||
.end_symbol(Some("\u{25bc}")),
|
||||
scrollbar_area,
|
||||
&mut state.scrollbar_state,
|
||||
);
|
||||
}
|
||||
|
||||
fn mc_color(code: char) -> Option<Color> {
|
||||
match code {
|
||||
'0' => Some(Color::Rgb(0x00, 0x00, 0x00)),
|
||||
'1' => Some(Color::Rgb(0x00, 0x00, 0xAA)),
|
||||
'2' => Some(Color::Rgb(0x00, 0xAA, 0x00)),
|
||||
'3' => Some(Color::Rgb(0x00, 0xAA, 0xAA)),
|
||||
'4' => Some(Color::Rgb(0xAA, 0x00, 0x00)),
|
||||
'5' => Some(Color::Rgb(0xAA, 0x00, 0xAA)),
|
||||
'6' => Some(Color::Rgb(0xFF, 0xAA, 0x00)),
|
||||
'7' => Some(Color::Rgb(0xAA, 0xAA, 0xAA)),
|
||||
'8' => Some(Color::Rgb(0x55, 0x55, 0x55)),
|
||||
'9' => Some(Color::Rgb(0x55, 0x55, 0xFF)),
|
||||
'a' | 'A' => Some(Color::Rgb(0x55, 0xFF, 0x55)),
|
||||
'b' | 'B' => Some(Color::Rgb(0x55, 0xFF, 0xFF)),
|
||||
'c' | 'C' => Some(Color::Rgb(0xFF, 0x55, 0x55)),
|
||||
'd' | 'D' => Some(Color::Rgb(0xFF, 0x55, 0xFF)),
|
||||
'e' | 'E' => Some(Color::Rgb(0xFF, 0xFF, 0x55)),
|
||||
'f' | 'F' => Some(Color::Rgb(0xFF, 0xFF, 0xFF)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_mc_text(text: &str, base_style: Style) -> Vec<Span<'static>> {
|
||||
let mut spans = Vec::new();
|
||||
let mut current_style = base_style;
|
||||
let mut current_text = String::new();
|
||||
let mut chars = text.chars().peekable();
|
||||
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '\u{00A7}' {
|
||||
if let Some(&code) = chars.peek() {
|
||||
if !current_text.is_empty() {
|
||||
spans.push(Span::styled(current_text.clone(), current_style));
|
||||
current_text.clear();
|
||||
}
|
||||
chars.next();
|
||||
|
||||
if let Some(color) = mc_color(code) {
|
||||
current_style = base_style.fg(color);
|
||||
} else {
|
||||
match code {
|
||||
'l' | 'L' => {
|
||||
current_style = current_style.add_modifier(Modifier::BOLD);
|
||||
}
|
||||
'm' | 'M' => {
|
||||
current_style = current_style.add_modifier(Modifier::CROSSED_OUT);
|
||||
}
|
||||
'n' | 'N' => {
|
||||
current_style = current_style.add_modifier(Modifier::UNDERLINED);
|
||||
}
|
||||
'o' | 'O' => {
|
||||
current_style = current_style.add_modifier(Modifier::ITALIC);
|
||||
}
|
||||
'r' | 'R' => {
|
||||
current_style = base_style;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
current_text.push(ch);
|
||||
}
|
||||
|
||||
if !current_text.is_empty() {
|
||||
spans.push(Span::styled(current_text, current_style));
|
||||
}
|
||||
|
||||
spans
|
||||
}
|
||||
|
||||
fn strip_mc_codes(text: &str) -> String {
|
||||
let mut result = String::with_capacity(text.len());
|
||||
let mut chars = text.chars().peekable();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '\u{00A7}' {
|
||||
chars.next();
|
||||
} else {
|
||||
result.push(ch);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn icon_spans(icon_pixels: Option<&Vec<Vec<IconCell>>>, row: usize) -> Vec<Span<'static>> {
|
||||
match icon_pixels.and_then(|rows| rows.get(row)) {
|
||||
Some(cols) => cols
|
||||
.iter()
|
||||
.map(|&(fg_r, fg_g, fg_b, bg_r, bg_g, bg_b)| {
|
||||
Span::styled(
|
||||
"\u{2584}",
|
||||
Style::default()
|
||||
.fg(Color::Rgb(fg_r, fg_g, fg_b))
|
||||
.bg(Color::Rgb(bg_r, bg_g, bg_b)),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
None => vec![Span::styled(
|
||||
" ",
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
)],
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ use crate::tui::theme::THEME;
|
||||
|
||||
pub mod account;
|
||||
pub mod content;
|
||||
pub mod content_list;
|
||||
pub mod details;
|
||||
pub mod mods_list;
|
||||
pub mod popups;
|
||||
pub mod profiles;
|
||||
pub mod search;
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
use std::path::Path;
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
style::{Color, Modifier, Style},
|
||||
text::{Line, Span, Text},
|
||||
widgets::Paragraph,
|
||||
Frame,
|
||||
};
|
||||
use tui_widget_list::{ListBuilder, ListState as TuiListState, ListView};
|
||||
|
||||
use crate::instance::mods::{scan_mods, toggle_mod, ModEntry};
|
||||
use crate::tui::theme::THEME;
|
||||
|
||||
type IconCell = (u8, u8, u8, u8, u8, u8);
|
||||
|
||||
pub struct ModsState {
|
||||
pub mods: Vec<ModEntry>,
|
||||
pub list_state: TuiListState,
|
||||
pub loaded_for: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for ModsState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mods: Vec::new(),
|
||||
list_state: TuiListState::default(),
|
||||
loaded_for: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ModsState {
|
||||
pub fn load_mods(&mut self, instances_dir: &Path, instance_name: &str) {
|
||||
self.mods = scan_mods(instances_dir, instance_name);
|
||||
self.loaded_for = Some(instance_name.to_string());
|
||||
self.list_state = TuiListState::default();
|
||||
if !self.mods.is_empty() {
|
||||
self.list_state.selected = Some(0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_selected(&mut self, instances_dir: &Path) {
|
||||
let Some(index) = self.list_state.selected else {
|
||||
return;
|
||||
};
|
||||
let Some(entry) = self.mods.get(index).cloned() else {
|
||||
return;
|
||||
};
|
||||
|
||||
match toggle_mod(&entry) {
|
||||
Ok(()) => {
|
||||
let loaded_for = self.loaded_for.clone();
|
||||
if let Some(instance_name) = loaded_for.as_deref() {
|
||||
self.load_mods(instances_dir, instance_name);
|
||||
if !self.mods.is_empty() {
|
||||
self.list_state.selected = Some(index.min(self.mods.len() - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::error!("Failed to toggle mod '{}': {}", entry.file_stem, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the key was consumed by the mods list.
|
||||
pub fn handle_key(key_event: &KeyEvent, state: &mut ModsState, instances_dir: &Path) -> bool {
|
||||
match key_event.code {
|
||||
KeyCode::Char('j') | KeyCode::Down => {
|
||||
let count = state.mods.len();
|
||||
if count == 0 {
|
||||
return true;
|
||||
}
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some((current + 1).min(count - 1));
|
||||
true
|
||||
}
|
||||
KeyCode::Char('k') | KeyCode::Up => {
|
||||
let current = state.list_state.selected.unwrap_or(0);
|
||||
state.list_state.selected = Some(current.saturating_sub(1));
|
||||
true
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
state.toggle_selected(instances_dir);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(frame: &mut Frame, area: Rect, state: &mut ModsState) {
|
||||
if state.mods.is_empty() {
|
||||
frame.render_widget(
|
||||
Paragraph::new("No mods installed.").style(Style::default().fg(THEME.colors.text_idle)),
|
||||
area,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let count = state.mods.len();
|
||||
const ITEM_HEIGHT: u16 = 2;
|
||||
|
||||
let mods_snapshot: Vec<(String, String, bool, Option<Vec<Vec<IconCell>>>)> = state
|
||||
.mods
|
||||
.iter()
|
||||
.map(|mod_entry| {
|
||||
(
|
||||
mod_entry.name.clone(),
|
||||
mod_entry.description.clone(),
|
||||
mod_entry.enabled,
|
||||
mod_entry.icon_lines.clone(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let builder = ListBuilder::new(move |context| {
|
||||
let (name, description, enabled, icon_pixels) = &mods_snapshot[context.index];
|
||||
|
||||
let (name_style, description_style, background) = if context.is_selected {
|
||||
(
|
||||
Style::default()
|
||||
.fg(THEME.colors.row_highlight)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.row_highlight),
|
||||
THEME.colors.row_background,
|
||||
)
|
||||
} else {
|
||||
(
|
||||
Style::default()
|
||||
.fg(THEME.colors.foreground)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
THEME.colors.row_alternate_bg,
|
||||
)
|
||||
};
|
||||
|
||||
let (status_text, status_style) = if *enabled {
|
||||
("✓", Style::default().fg(THEME.colors.success))
|
||||
} else {
|
||||
("✗", Style::default().fg(THEME.colors.error))
|
||||
};
|
||||
|
||||
let icon_row_0 = icon_spans(icon_pixels.as_ref(), 0);
|
||||
let icon_row_1 = icon_spans(icon_pixels.as_ref(), 1);
|
||||
|
||||
let mut line_0 = icon_row_0;
|
||||
line_0.push(Span::raw(" "));
|
||||
line_0.push(Span::styled(name.clone(), name_style));
|
||||
line_0.push(Span::raw(" "));
|
||||
line_0.push(Span::styled(status_text, status_style));
|
||||
|
||||
let mut line_1 = icon_row_1;
|
||||
line_1.push(Span::raw(" "));
|
||||
line_1.push(Span::styled(description.clone(), description_style));
|
||||
|
||||
let item = Text::from(vec![Line::from(line_0), Line::from(line_1)])
|
||||
.style(Style::default().bg(background));
|
||||
(item, ITEM_HEIGHT)
|
||||
});
|
||||
|
||||
let list = ListView::new(builder, count);
|
||||
frame.render_stateful_widget(list, area, &mut state.list_state);
|
||||
}
|
||||
|
||||
fn icon_spans(icon_pixels: Option<&Vec<Vec<IconCell>>>, row: usize) -> Vec<Span<'static>> {
|
||||
match icon_pixels.and_then(|rows| rows.get(row)) {
|
||||
Some(cols) => cols
|
||||
.iter()
|
||||
.map(|&(fg_r, fg_g, fg_b, bg_r, bg_g, bg_b)| {
|
||||
Span::styled(
|
||||
"▄",
|
||||
Style::default()
|
||||
.fg(Color::Rgb(fg_r, fg_g, fg_b))
|
||||
.bg(Color::Rgb(bg_r, bg_g, bg_b)),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
None => vec![Span::styled(
|
||||
" ",
|
||||
Style::default().fg(THEME.colors.text_idle),
|
||||
)],
|
||||
}
|
||||
}
|
||||
@@ -275,7 +275,7 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
||||
.fg(THEME.colors.border_focused)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
)
|
||||
.thumb_symbol("\u{2503}")
|
||||
.thumb_symbol("\u{2551}")
|
||||
.track_symbol(Some(""))
|
||||
.end_symbol(Some("\u{25bc}")),
|
||||
scrollbar_area,
|
||||
|
||||
Reference in new issue
Block a user