Crate cmdx

Crate cmdx 

Source
Expand description

§cmdx - Cross-Platform Command and Path Translator Library

A high-performance library for translating shell commands and file paths between different operating systems. Designed for integration into terminal emulators and cross-platform tools.

§Features

  • Command Translation: Translate shell commands with flag support
  • Path Translation: Bidirectional file path translation (Windows ↔ Unix)
  • OS Detection: Runtime detection of the current operating system
  • High Performance: Static lookup tables with lazy initialization

§Command Translation Example

use cmdx::{translate_command, Os};

// Translate a Windows command to Linux
let result = translate_command("dir /w", Os::Windows, Os::Linux);
assert!(result.is_ok());
assert!(result.unwrap().command.contains("ls"));

// Translate a Linux command to Windows
let result = translate_command("ls -la", Os::Linux, Os::Windows);
assert!(result.is_ok());
assert!(result.unwrap().command.contains("dir"));

§Path Translation Example

use cmdx::{translate_path, Os};

// Windows to Linux path
let result = translate_path("C:\\Users\\john\\file.txt", Os::Windows, Os::Linux);
assert!(result.is_ok());
assert_eq!(result.unwrap().path, "/mnt/c/Users/john/file.txt");

// Linux to Windows path
let result = translate_path("/mnt/c/Users/john", Os::Linux, Os::Windows);
assert!(result.is_ok());
assert_eq!(result.unwrap().path, "C:\\Users\\john");

§Terminal Emulator Integration

use cmdx::{translate_command, translate_path, detect_os, Os};

// Detect the current OS at runtime
let current_os = detect_os();

// Translate user input for a different target OS
fn process_input(input: &str, target_os: Os) -> String {
    let current = cmdx::detect_os();
     
    // Try command translation first
    if let Ok(result) = cmdx::translate_command(input, current, target_os) {
        return result.command;
    }
     
    // Fall back to path translation if it looks like a path
    if let Ok(result) = cmdx::translate_path(input, current, target_os) {
        return result.path;
    }
     
    input.to_string()
}

Re-exports§

pub use translator::command_map::CommandMapping;
pub use translator::command_map::FlagMapping;
pub use translator::command_map::is_native_command;
pub use translator::command_map::is_target_command_for_os;
pub use translator::engine::translate_command;
pub use translator::engine::translate_command_str;
pub use translator::engine::translate_batch;
pub use translator::engine::translate_compound_command;
pub use translator::engine::translate_full;
pub use translator::engine::translate_script_extension;
pub use translator::engine::translate_shebang;
pub use translator::engine::TranslationResult;
pub use translator::engine::TranslationError;
pub use translator::path::translate_path;
pub use translator::path::translate_path_str;
pub use translator::path::translate_path_auto;
pub use translator::path::translate_paths;
pub use translator::path::PathTranslation;
pub use translator::path::PathError;
pub use translator::path::is_windows_path;
pub use translator::path::is_unix_path;
pub use translator::env::translate_env_vars;
pub use translator::env::translate_with_env;
pub use translator::os::Os;
pub use translator::os::detect_os;

Modules§

translator
Translator module - contains all translation logic