From 804084015992aefdcd35710f4d5874cdbce01783 Mon Sep 17 00:00:00 2001 From: dawn <90008@gaze.systems> Date: Sat, 20 Dec 2025 05:45:23 +0300 Subject: [PATCH] format --- src/cmd/glob.rs | 68 ++++++++++++++++++++++-------------------- src/cmd/ls.rs | 22 +++++--------- src/cmd/mv.rs | 35 ++++++++++++++-------- src/cmd/open.rs | 29 ++++++++++-------- src/cmd/rm.rs | 10 ++----- src/cmd/source_file.rs | 18 +++++------ src/lib.rs | 4 +-- 7 files changed, 93 insertions(+), 93 deletions(-) diff --git a/src/cmd/glob.rs b/src/cmd/glob.rs index c7f4c9d..ef5247f 100644 --- a/src/cmd/glob.rs +++ b/src/cmd/glob.rs @@ -87,10 +87,10 @@ pub fn glob_match( // Normalize pattern: remove leading / for relative matching let normalized_pattern = pattern_str.trim_start_matches('/'); let is_recursive = normalized_pattern.contains("**"); - + // Collect matching paths let mut matches = Vec::new(); - + fn walk_directory( current_path: Arc, current_relative_path: String, @@ -111,7 +111,26 @@ pub fn glob_match( if let Ok(entries) = current_path.read_dir() { for entry in entries { let filename = entry.filename(); - let entry_path = current_path.join(&filename) + let entry_path = + current_path + .join(&filename) + .map_err(|e| ShellError::GenericError { + error: "path error".into(), + msg: e.to_string(), + span: None, + help: None, + inner: vec![], + })?; + + // Build relative path from base + let new_relative = if current_relative_path.is_empty() { + filename.clone() + } else { + format!("{}/{}", current_relative_path, filename) + }; + + let metadata = entry_path + .metadata() .map_err(|e| ShellError::GenericError { error: "path error".into(), msg: e.to_string(), @@ -119,22 +138,7 @@ pub fn glob_match( help: None, inner: vec![], })?; - - // Build relative path from base - let new_relative = if current_relative_path.is_empty() { - filename.clone() - } else { - format!("{}/{}", current_relative_path, filename) - }; - - let metadata = entry_path.metadata().map_err(|e| ShellError::GenericError { - error: "path error".into(), - msg: e.to_string(), - span: None, - help: None, - inner: vec![], - })?; - + // Check if this path matches the pattern // For patterns without path separators, match just the filename // For patterns with path separators, match the full relative path @@ -143,7 +147,7 @@ pub fn glob_match( } else { &filename }; - + if pattern.matches(path_to_match) { let should_include = match metadata.file_type { VfsFileType::Directory => !no_dirs, @@ -153,14 +157,15 @@ pub fn glob_match( matches.push(new_relative.clone()); } } - + // Recursively walk into subdirectories if metadata.file_type == VfsFileType::Directory { // Continue if: recursive pattern, or we haven't reached max depth, or pattern has more components - let should_recurse = is_recursive + let should_recurse = is_recursive || current_depth < max_depth - || (normalized_pattern.contains('/') && current_depth < normalized_pattern.split('/').count()); - + || (normalized_pattern.contains('/') + && current_depth < normalized_pattern.split('/').count()); + if should_recurse { walk_directory( Arc::new(entry_path), @@ -276,11 +281,7 @@ impl Command for Glob { // Determine if pattern is absolute (starts with /) let is_absolute = pattern_str.starts_with('/'); - let base_path = if is_absolute { - get_vfs() - } else { - get_pwd() - }; + let base_path = if is_absolute { get_vfs() } else { get_pwd() }; // Use the glob_match function let options = GlobOptions { @@ -288,17 +289,18 @@ impl Command for Glob { no_dirs, no_files, }; - + let matches = glob_match(&pattern_str, base_path, options)?; // Convert matches to Value stream let signals = engine_state.signals().clone(); - let values = matches.into_iter().map(move |path| Value::string(path, span)); - + let values = matches + .into_iter() + .map(move |path| Value::string(path, span)); + Ok(PipelineData::list_stream( ListStream::new(values, span, signals.clone()), None, )) } } - diff --git a/src/cmd/ls.rs b/src/cmd/ls.rs index f37a710..c776c95 100644 --- a/src/cmd/ls.rs +++ b/src/cmd/ls.rs @@ -1,17 +1,17 @@ use std::time::{SystemTime, UNIX_EPOCH}; use crate::{ - cmd::glob::{expand_path, GlobOptions}, + cmd::glob::{GlobOptions, expand_path}, error::to_shell_err, globals::{get_pwd, get_vfs}, }; -use std::sync::Arc; use jacquard::chrono; use nu_engine::CallExt; use nu_protocol::{ Category, ListStream, PipelineData, Record, ShellError, Signature, SyntaxShape, Type, Value, engine::{Command, EngineState, Stack}, }; +use std::sync::Arc; #[derive(Clone)] pub struct Ls; @@ -78,11 +78,7 @@ impl Command for Ls { }; let is_absolute = path_str.starts_with('/'); - let base_path: Arc = if is_absolute { - get_vfs() - } else { - pwd.clone() - }; + let base_path: Arc = if is_absolute { get_vfs() } else { pwd.clone() }; let options = GlobOptions { max_depth: None, @@ -145,13 +141,11 @@ impl Command for Ls { Ok(Some(Value::record(record, span))) }; - let entries = matches - .into_iter() - .flat_map(move |rel_path| { - make_record(&rel_path) - .transpose() - .map(|res| res.unwrap_or_else(|err| Value::error(err, span))) - }); + let entries = matches.into_iter().flat_map(move |rel_path| { + make_record(&rel_path) + .transpose() + .map(|res| res.unwrap_or_else(|err| Value::error(err, span))) + }); let signals = engine_state.signals().clone(); Ok(PipelineData::list_stream( diff --git a/src/cmd/mv.rs b/src/cmd/mv.rs index e89d64f..3fbade0 100644 --- a/src/cmd/mv.rs +++ b/src/cmd/mv.rs @@ -1,16 +1,16 @@ use std::io::{Read, Write}; use crate::{ - cmd::glob::{expand_path, GlobOptions}, + cmd::glob::{GlobOptions, expand_path}, error::to_shell_err, globals::{get_pwd, get_vfs}, }; -use std::sync::Arc; use nu_engine::CallExt; use nu_protocol::{ Category, PipelineData, ShellError, Signature, SyntaxShape, Type, Value, engine::{Command, EngineState, Stack}, }; +use std::sync::Arc; use vfs::{VfsError, VfsFileType}; #[derive(Clone)] @@ -77,11 +77,7 @@ impl Command for Mv { // Expand source path (glob or single) into list of paths let is_absolute = source_str.starts_with('/'); - let base_path: Arc = if is_absolute { - get_vfs() - } else { - get_pwd() - }; + let base_path: Arc = if is_absolute { get_vfs() } else { get_pwd() }; let options = GlobOptions { max_depth: None, @@ -90,7 +86,11 @@ impl Command for Mv { }; let matches = expand_path(&source_str, base_path.clone(), options)?; - let is_glob = matches.len() > 1 || source_str.contains('*') || source_str.contains('?') || source_str.contains('[') || source_str.contains("**"); + let is_glob = matches.len() > 1 + || source_str.contains('*') + || source_str.contains('?') + || source_str.contains('[') + || source_str.contains("**"); // Resolve destination let dest = get_pwd() @@ -99,7 +99,9 @@ impl Command for Mv { // For glob patterns, destination must be a directory if is_glob { - let dest_meta = dest.metadata().map_err(to_shell_err(call.arguments_span()))?; + let dest_meta = dest + .metadata() + .map_err(to_shell_err(call.arguments_span()))?; if dest_meta.file_type != VfsFileType::Directory { return Err(ShellError::GenericError { error: "destination must be a directory".to_string(), @@ -113,14 +115,19 @@ impl Command for Mv { // Move each matching file/directory for rel_path in matches { - let source = base_path.join(&rel_path).map_err(to_shell_err(call.arguments_span()))?; - let source_meta = source.metadata().map_err(to_shell_err(call.arguments_span()))?; + let source = base_path + .join(&rel_path) + .map_err(to_shell_err(call.arguments_span()))?; + let source_meta = source + .metadata() + .map_err(to_shell_err(call.arguments_span()))?; // Determine destination path let dest_entry = if is_glob { // For glob patterns, use filename in destination directory let filename = rel_path.split('/').last().unwrap_or(&rel_path); - dest.join(filename).map_err(to_shell_err(call.arguments_span()))? + dest.join(filename) + .map_err(to_shell_err(call.arguments_span()))? } else { // For single path, use destination as-is dest.clone() @@ -128,7 +135,9 @@ impl Command for Mv { match source_meta.file_type { VfsFileType::File => move_file(&source, &dest_entry, call.arguments_span())?, - VfsFileType::Directory => move_directory(&source, &dest_entry, call.arguments_span())?, + VfsFileType::Directory => { + move_directory(&source, &dest_entry, call.arguments_span())? + } } } diff --git a/src/cmd/open.rs b/src/cmd/open.rs index 0b0f5c2..840443f 100644 --- a/src/cmd/open.rs +++ b/src/cmd/open.rs @@ -1,16 +1,17 @@ use std::ops::Not; use crate::{ - cmd::glob::{expand_path, GlobOptions}, + cmd::glob::{GlobOptions, expand_path}, globals::{get_pwd, get_vfs}, }; -use std::sync::Arc; use nu_command::{FromCsv, FromJson, FromOds, FromToml, FromTsv, FromXlsx, FromXml, FromYaml}; use nu_engine::CallExt; use nu_protocol::{ - ByteStream, Category, ListStream, PipelineData, ShellError, Signature, SyntaxShape, Type, Value, + ByteStream, Category, ListStream, PipelineData, ShellError, Signature, SyntaxShape, Type, + Value, engine::{Command, EngineState, Stack}, }; +use std::sync::Arc; #[derive(Clone)] pub struct Open; @@ -65,11 +66,7 @@ impl Command for Open { // Expand path (glob or single) into list of paths let is_absolute = path_str.starts_with('/'); - let base_path: Arc = if is_absolute { - get_vfs() - } else { - get_pwd() - }; + let base_path: Arc = if is_absolute { get_vfs() } else { get_pwd() }; let options = GlobOptions { max_depth: None, @@ -127,14 +124,15 @@ impl Command for Open { match cmd.run(engine_state, stack, call, data) { Ok(pipeline_data) => { // Convert pipeline data to value - pipeline_data.into_value(span).unwrap_or_else(|e| { - Value::error(e, span) - }) + pipeline_data + .into_value(span) + .unwrap_or_else(|e| Value::error(e, span)) } Err(e) => Value::error(e, span), } } else { - data.into_value(span).unwrap_or_else(|e| Value::error(e, span)) + data.into_value(span) + .unwrap_or_else(|e| Value::error(e, span)) }; results.push(value); } @@ -154,7 +152,12 @@ impl Command for Open { } // If single file, return the single result directly (for backward compatibility) - if results.len() == 1 && !path_str.contains('*') && !path_str.contains('?') && !path_str.contains('[') && !path_str.contains("**") { + if results.len() == 1 + && !path_str.contains('*') + && !path_str.contains('?') + && !path_str.contains('[') + && !path_str.contains("**") + { match results.into_iter().next().unwrap() { Value::Error { error, .. } => Err(*error), val => Ok(PipelineData::Value(val, None)), diff --git a/src/cmd/rm.rs b/src/cmd/rm.rs index d2c2ea6..2099a03 100644 --- a/src/cmd/rm.rs +++ b/src/cmd/rm.rs @@ -1,14 +1,14 @@ use crate::{ - cmd::glob::{expand_path, GlobOptions}, + cmd::glob::{GlobOptions, expand_path}, error::to_shell_err, globals::{get_pwd, get_vfs}, }; -use std::sync::Arc; use nu_engine::CallExt; use nu_protocol::{ Category, PipelineData, ShellError, Signature, SyntaxShape, Type, Value, engine::{Command, EngineState, Stack}, }; +use std::sync::Arc; use vfs::VfsFileType; #[derive(Clone)] @@ -75,11 +75,7 @@ impl Command for Rm { // Expand path (glob or single) into list of paths let is_absolute = path_str.starts_with('/'); - let base_path: Arc = if is_absolute { - get_vfs() - } else { - get_pwd() - }; + let base_path: Arc = if is_absolute { get_vfs() } else { get_pwd() }; let options = GlobOptions { max_depth: None, diff --git a/src/cmd/source_file.rs b/src/cmd/source_file.rs index 348d9b7..457276a 100644 --- a/src/cmd/source_file.rs +++ b/src/cmd/source_file.rs @@ -3,13 +3,13 @@ use crate::{ error::{CommandError, to_shell_err}, globals::{get_pwd, get_vfs, print_to_console, set_pwd}, }; -use std::sync::Arc; use nu_engine::{CallExt, get_eval_block_with_early_return}; use nu_parser::parse; use nu_protocol::{ Category, PipelineData, ShellError, Signature, SyntaxShape, Type, Value, engine::{Command, EngineState, Stack, StateWorkingSet}, }; +use std::sync::Arc; #[derive(Clone)] pub struct SourceFile; @@ -60,23 +60,19 @@ impl Command for SourceFile { let pwd = get_pwd(); let is_absolute = path_str.starts_with('/'); - let base_path: Arc = if is_absolute { - get_vfs() - } else { - pwd.clone() - }; + let base_path: Arc = if is_absolute { get_vfs() } else { pwd.clone() }; // Check if it's a glob pattern (contains *, ?, [, or **) - let is_glob = path_str.contains('*') - || path_str.contains('?') - || path_str.contains('[') + let is_glob = path_str.contains('*') + || path_str.contains('?') + || path_str.contains('[') || path_str.contains("**"); let paths_to_source = if is_glob { // Expand glob pattern let options = crate::cmd::glob::GlobOptions { max_depth: None, - no_dirs: true, // Only source files, not directories + no_dirs: true, // Only source files, not directories no_files: false, }; glob_match(&path_str, base_path.clone(), options)? @@ -88,7 +84,7 @@ impl Command for SourceFile { // Source each matching file for rel_path in paths_to_source { let full_path = base_path.join(&rel_path).map_err(to_shell_err(span))?; - + let metadata = full_path.metadata().map_err(to_shell_err(span))?; if metadata.file_type != vfs::VfsFileType::File { continue; diff --git a/src/lib.rs b/src/lib.rs index 1d1d252..e7218d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,8 +28,8 @@ pub mod memory_fs; use crate::{ cmd::{ - Cd, Eval, Fetch, Glob, Job, JobKill, JobList, Ls, Mkdir, Mv, Open, Print, Pwd, Random, Rm, Save, - SourceFile, Sys, + Cd, Eval, Fetch, Glob, Job, JobKill, JobList, Ls, Mkdir, Mv, Open, Print, Pwd, Random, Rm, + Save, SourceFile, Sys, }, default_context::add_shell_command_context, globals::{InterruptBool, get_pwd, get_vfs, print_to_console, set_interrupt}, -- 2.51.2