Struct IShell

Source
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

Source

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");
Source

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);
Source

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");
Source

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");

Trait Implementations§

Source§

impl Default for IShell

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for IShell

§

impl RefUnwindSafe for IShell

§

impl Send for IShell

§

impl Sync for IShell

§

impl Unpin for IShell

§

impl UnwindSafe for IShell

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.