From 8d34ab0fcff6d1d01de5e8267af6eb8d8b2ba4e9 Mon Sep 17 00:00:00 2001 From: Pierre-Henri Symoneaux Date: Sun, 19 Jun 2016 14:11:04 +0200 Subject: [PATCH] Added some examples --- README.md | 89 +++++++++++++++++++++++++++++++++++++ examples/data.rs | 21 +++++++++ examples/dummy.rs | 13 ++++++ examples/{cli.rs => map.rs} | 0 4 files changed, 123 insertions(+) create mode 100644 examples/data.rs create mode 100644 examples/dummy.rs rename examples/{cli.rs => map.rs} (100%) diff --git a/README.md b/README.md index 71f980727e..40a21991b6 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,92 @@ Rust library to create interactive command line shells This is currently a work in progress, and the API should be consider unstable. I'll start documenting and releasing to **crates.io** once a first level of stability has been reached + +# How to use + +## Including + +More often, you will include the library as a dependency to your project. In order to do this, add the following lines to your **Cargo.toml** file : + +```toml +[dependencies] +shrust = "0.0.1" +``` + +## Basic usage + +Let's have a look at example [dummy.rs](./examples/dummy.rs) : +```rust +extern crate shrust; +use shrust::{Shell, ShellIO}; +use std::io::prelude::*; + +fn main() { + let mut shell = Shell::new(()); + shell.new_command_noargs("hello", "Say 'hello' to the world", |io, _| { + try!(writeln!(io, "Hello World !!!")); + Ok(()) + }); + + shell.run_loop(&mut ShellIO::default()); +} +``` + +The output of this program would be +``` +λ cargo run --example dummy + Running `target\debug\examples\dummy.exe` +>help + hello : Say 'hello' to the world + help : Print this help + history : Print commands history or run a command from it + quit : Quit +>hello +Hello World !!! +>quit +``` + +## Attaching data + +You can attach data to the shell for usage by commands as seen in [data.rs](./examples/data.rs): +```rust +let v = Vec::new(); +let mut shell = Shell::new(v); +shell.new_command("push", "Add string to the list", 1, |io, v, s| { + try!(writeln!(io, "Pushing {}", s[0])); + v.push(s[0].to_string()); + Ok(()) +}); +shell.new_command_noargs("list", "List strings", |io, v| { + for s in v { + try!(writeln!(io, "{}", s)); + } + Ok(()) +}); + +shell.run_loop(&mut ShellIO::default()); +``` +Output: +``` +λ cargo run --example dummy + Running `target\debug\examples\dummy.exe` +>help + help : Print this help + history : Print commands history or run a command from it + list : List strings + push : Add string to the list + quit : Quit +>push foo +Pushing foo +>push bar +Pushing bar +>list +foo +bar +>quit +``` + +## Multithreading +TBD... + +Additional examples are provided in documentation and in [examples](./examples/) directory diff --git a/examples/data.rs b/examples/data.rs new file mode 100644 index 0000000000..07d8690845 --- /dev/null +++ b/examples/data.rs @@ -0,0 +1,21 @@ +extern crate shrust; +use shrust::{Shell, ShellIO}; +use std::io::prelude::*; + +fn main() { + let v = Vec::new(); + let mut shell = Shell::new(v); + shell.new_command("push", "Add string to the list", 1, |io, v, s| { + try!(writeln!(io, "Pushing {}", s[0])); + v.push(s[0].to_string()); + Ok(()) + }); + shell.new_command_noargs("list", "List strings", |io, v| { + for s in v { + try!(writeln!(io, "{}", s)); + } + Ok(()) + }); + + shell.run_loop(&mut ShellIO::default()); +} diff --git a/examples/dummy.rs b/examples/dummy.rs new file mode 100644 index 0000000000..655efc8b8e --- /dev/null +++ b/examples/dummy.rs @@ -0,0 +1,13 @@ +extern crate shrust; +use shrust::{Shell, ShellIO}; +use std::io::prelude::*; + +fn main() { + let mut shell = Shell::new(()); + shell.new_command_noargs("hello", "Say 'hello' to the world", |io, _| { + try!(writeln!(io, "Hello World !!!")); + Ok(()) + }); + + shell.run_loop(&mut ShellIO::default()); +} diff --git a/examples/cli.rs b/examples/map.rs similarity index 100% rename from examples/cli.rs rename to examples/map.rs