pub struct IShell { /* private fields */ }Expand description
A shell interface with memory
§Examples
use ishell::IShell;
// Opening an IShell at the same directory as the shell running this program
let shell = IShell::new();
// Create a directory, travel into it and create another directory
shell.run_command("mkdir test");
shell.run_command("cd test");
shell.run_command("mkdir test2");
// Are we still in the `test` directory?..
let result = shell.run_command("ls");
let stdout_res = String::from_utf8(result.stdout).expect("Stdout contained invalid UTF-8!");
assert_eq!(stdout_res.trim(), "test2"); // Indeed we are
// Let's clean after ourselves
shell.run_command("cd ..");
shell.run_command("rm -r test");Implementations§
Source§impl IShell
impl IShell
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new IShell with internal shell’s
directory set to the value of std::env::current_dir().
§Panics
This function will panic due to std::env::current_dir() if any of the following is true:
- Current directory (from where your program is ran) does not exist
- There are insufficient permissions to access the current directory (from where your program is ran)
- Directory (from where your program is ran) contains invalid UTF-8
§Examples
use ishell::IShell;
// Opening an IShell at the same directory as the shell running this program
let shell = IShell::new();
// Opening an IShell at the `/home/user/Desktop/` directory
let desktop_shell = IShell::from_path("/home/user/Desktop/");
// Opening an IShell at the directory "test123" or "./test123",
// relative to the shell running this program
let relative_test123_shell = IShell::from_path("test123");Sourcepub fn from_path(initial_dir: impl AsRef<Path>) -> Result<Self, ShellInitError>
pub fn from_path(initial_dir: impl AsRef<Path>) -> Result<Self, ShellInitError>
Constructs a new IShell with internal shell’s directory set to the value of
<current_dir> / initial_dir
if it exists. Otherwise, initial_dir is treated as a full path
§Examples
use ishell::IShell;
// Opens a shell in relative (to the `std::env::current_dir()`)
// directory `target` if it exists. If it does not,
// tries to treat `target` as a full path
let shell = IShell::from_path("target").unwrap();
// Opens a shell at resolved path from current user running it
let shell = IShell::from_path("~").unwrap();
let result = shell.run_command("ls -l");
let result = String::from_utf8(result.stdout).expect("Stdout contained invalid UTF-8!");
println!("{}", result);Sourcepub fn run_command(&self, command: &str) -> ShellOutput
pub fn run_command(&self, command: &str) -> ShellOutput
Runs a command through IShell within its current_dir.
Any cd command will not be actually ran. Instead, inner directory of IShell (current_dir) will change
accordingly. If cd is aliased to something else, (i.e. changedir), and you use this alias instead of cd,
then IShell wont understand that you wanted it to change directory.
§Examples
use ishell::{ShellOutput, IShell};
let shell = IShell::from_path("~").unwrap();
let result: ShellOutput = shell.run_command("ls");Sourcepub fn forget_current_directory(&self)
pub fn forget_current_directory(&self)
Forget current directory and go back to the directory initially specified.
§Examples
use ishell::IShell;
// Opening an IShell at the same directory as the shell running this program
let shell = IShell::new();
shell.run_command("mkdir test123");
shell.run_command("cd test123");
// We just created this dir. It should be empty.
let result = shell.run_command("ls");
let result = String::from_utf8(result.stdout)
.expect("Stdout contained invalid UTF-8!");
assert_eq!(result, "");
// This will move us to the initial directory in which
// the IShell was created (the same directory as the shell running this program)
shell.forget_current_directory();
let result = shell.run_command("ls test123");
assert!(result.is_success());
shell.run_command("rm -r test123");