Add interface languages and improve themes and workspace controls for 0.1.36
This commit is contained in:
@@ -142,6 +142,22 @@ pub fn workspace_recent(app: AppHandle) -> WsResult<Vec<String>> {
|
||||
Ok(load_workspace_history(&last_workspace_file(&app)?))
|
||||
}
|
||||
|
||||
fn forget_workspace(path: &Path, root: &str) -> WsResult<Vec<String>> {
|
||||
let mut roots = load_workspace_history(path);
|
||||
roots.retain(|previous| {
|
||||
if cfg!(windows) { !previous.eq_ignore_ascii_case(root) } else { previous != root }
|
||||
});
|
||||
let payload = serde_json::json!({ "root": roots.first(), "recent": roots });
|
||||
fs::write(path, serde_json::to_string_pretty(&payload).unwrap())?;
|
||||
Ok(roots)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn workspace_forget(app: AppHandle, path: String) -> WsResult<Vec<String>> {
|
||||
let _guard = HISTORY_LOCK.lock().map_err(|_| err("workspace history lock"))?;
|
||||
forget_workspace(&last_workspace_file(&app)?, &path)
|
||||
}
|
||||
|
||||
fn create_workspace_folder(parent: &str, name: &str) -> WsResult<PathBuf> {
|
||||
let name = name.trim();
|
||||
let stem = name.split('.').next().unwrap_or("").to_ascii_uppercase();
|
||||
@@ -575,6 +591,45 @@ fn read_optional_text(path: &Path, missing_ok: bool) -> WsResult<Option<String>>
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct TextRead {
|
||||
path: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
source: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
fn read_many_text(root: &Path, paths: Vec<String>) -> WsResult<Vec<TextRead>> {
|
||||
if paths.len() > 64 { return Err(err("Read at most 64 files per request.")); }
|
||||
let mut remaining = 16 * 1024 * 1024usize;
|
||||
Ok(paths.into_iter().map(|path| {
|
||||
let result = (|| -> WsResult<String> {
|
||||
let resolved = resolve_existing(root, &path)?;
|
||||
let file = fs::File::open(resolved)?;
|
||||
let metadata = file.metadata()?;
|
||||
if !metadata.is_file() { return Err(err("The path must be a file.")); }
|
||||
if metadata.len() > remaining as u64 { return Err(err("The batch exceeds the 16 MiB limit.")); }
|
||||
let mut bytes = Vec::new();
|
||||
file.take(remaining as u64 + 1).read_to_end(&mut bytes)?;
|
||||
if bytes.len() > remaining { return Err(err("The batch exceeds the 16 MiB limit.")); }
|
||||
remaining -= bytes.len();
|
||||
String::from_utf8(bytes).map_err(|_| err("The file is not valid UTF-8."))
|
||||
})();
|
||||
match result {
|
||||
Ok(source) => TextRead { path, source: Some(source), error: None },
|
||||
Err(error) => TextRead { path, source: None, error: Some(error.to_string()) },
|
||||
}
|
||||
}).collect())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn workspace_read_many(window: tauri::Window, state: State<'_, WorkspaceState>, paths: Vec<String>) -> WsResult<Vec<TextRead>> {
|
||||
let root = current_root(&state, window.label())?;
|
||||
tauri::async_runtime::spawn_blocking(move || read_many_text(&root, paths))
|
||||
.await.map_err(|error| err(error.to_string()))?
|
||||
}
|
||||
|
||||
const MAX_BINARY_BYTES: u64 = 64 * 1024 * 1024;
|
||||
|
||||
fn read_workspace_bytes(root: &Path, rel: &str) -> WsResult<Vec<u8>> {
|
||||
@@ -1268,6 +1323,33 @@ mod tests {
|
||||
fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forgetting_workspaces_persists_without_restoring_the_last_root() {
|
||||
let path = std::env::temp_dir().join(format!("rs-forget-history-{}.json", std::process::id()));
|
||||
fs::write(&path, r#"{"root":"D:/first","recent":["D:/first","D:/second"]}"#).unwrap();
|
||||
assert_eq!(forget_workspace(&path, "D:/first").unwrap(), vec!["D:/second"]);
|
||||
assert_eq!(load_workspace_history(&path), vec!["D:/second"]);
|
||||
assert!(forget_workspace(&path, "D:/second").unwrap().is_empty());
|
||||
assert!(load_workspace_history(&path).is_empty());
|
||||
fs::remove_file(path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batch_reads_keep_errors_per_file_and_enforce_workspace_limits() {
|
||||
let root = std::env::temp_dir().join(format!("rs-read-many-{}", std::process::id()));
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
let root = dunce::canonicalize(root).unwrap();
|
||||
fs::write(root.join("valid.md"), "card text").unwrap();
|
||||
fs::write(root.join("binary"), [0xff]).unwrap();
|
||||
let items = read_many_text(&root, vec!["valid.md".into(), "missing".into(), "../outside".into(), ".".into(), "binary".into()]).unwrap();
|
||||
assert_eq!(items[0].source.as_deref(), Some("card text"));
|
||||
assert!(items[1..].iter().all(|item| item.source.is_none() && item.error.is_some()));
|
||||
assert!(read_many_text(&root, vec!["valid.md".into(); 65]).is_err());
|
||||
fs::write(root.join("large"), vec![b'x'; 16 * 1024 * 1024 + 1]).unwrap();
|
||||
assert!(read_many_text(&root, vec!["large".into()]).unwrap()[0].error.is_some());
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_workspace_creates_only_a_new_child_folder() {
|
||||
let parent = std::env::temp_dir().join(format!("rs-new-workspace-{}", std::process::id()));
|
||||
|
||||
Reference in New Issue
Block a user