-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
532 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
[package] | ||
name = "shrust" | ||
version = "0.0.4" | ||
description = "A library for creating interactive command line shells in Rust" | ||
authors = ["Pierre-Henri Symoneaux"] | ||
repository = "https://github.com/phsym/shrust" | ||
homepage = "https://github.com/phsym/shrust" | ||
documentation = "http://phsym.github.io/shrust" | ||
readme = "README.md" | ||
license = "MIT" | ||
keywords = ["shell", "console", "interactive", "terminal", "command"] | ||
|
||
[dependencies] | ||
prettytable-rs = "^0.6" | ||
[package] | ||
name = "shrust" | ||
version = "0.0.4" | ||
description = "A library for creating interactive command line shells in Rust" | ||
authors = ["Pierre-Henri Symoneaux"] | ||
repository = "https://github.com/phsym/shrust" | ||
homepage = "https://github.com/phsym/shrust" | ||
documentation = "http://phsym.github.io/shrust" | ||
readme = "README.md" | ||
license = "MIT" | ||
keywords = ["shell", "console", "interactive", "terminal", "command"] | ||
|
||
[dependencies] | ||
prettytable-rs = "^0.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +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()); | ||
} | ||
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
extern crate shrust; | ||
use shrust::{Shell, ShellIO}; | ||
use std::io::prelude::*; | ||
|
||
fn main() { | ||
let mut shell = Shell::new(()); | ||
shell.set_default(|io, _, cmd| { | ||
try!(writeln!(io, "Hello from default handler !!! Received: {}", cmd)); | ||
Ok(()) | ||
}); | ||
shell.run_loop(&mut ShellIO::default()); | ||
} | ||
extern crate shrust; | ||
use shrust::{Shell, ShellIO}; | ||
use std::io::prelude::*; | ||
|
||
fn main() { | ||
let mut shell = Shell::new(()); | ||
shell.set_default(|io, _, cmd| { | ||
try!(writeln!(io, "Hello from default handler !!! Received: {}", cmd)); | ||
Ok(()) | ||
}); | ||
shell.run_loop(&mut ShellIO::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +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()); | ||
} | ||
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
extern crate shrust; | ||
use shrust::{Shell, ShellIO}; | ||
use std::io::prelude::*; | ||
|
||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
|
||
fn main() { | ||
let map = HashMap::new(); | ||
let mut shell = Shell::new(map); | ||
|
||
shell.new_command("put", "Insert a value", 2, |_, map, args| { | ||
map.insert(try!(usize::from_str(args[0])), args[1].to_string()); | ||
Ok(()) | ||
}); | ||
shell.new_command("get", "Get a value", 1, |mut io, map, args| { | ||
match map.get(&try!(usize::from_str(args[0]))) { | ||
Some(val) => writeln!(io, "{}", val).unwrap(), | ||
None => writeln!(io, "Not found").unwrap() | ||
}; | ||
Ok(()) | ||
}); | ||
shell.new_command("remove", "Remove a value", 1, |_, map, args| { | ||
map.remove(&try!(usize::from_str(args[0]))); | ||
Ok(()) | ||
}); | ||
shell.new_command("list", "List all values", 0, |mut io, map, _| { | ||
for (k, v) in map { | ||
writeln!(io, "{} = {}", k, v).unwrap(); | ||
} | ||
Ok(()) | ||
}); | ||
shell.new_command_noargs("clear", "Clear all values", |_, map| { | ||
map.clear(); | ||
Ok(()) | ||
}); | ||
|
||
shell.run_loop(&mut ShellIO::default()); | ||
} | ||
extern crate shrust; | ||
use shrust::{Shell, ShellIO}; | ||
use std::io::prelude::*; | ||
|
||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
|
||
fn main() { | ||
let map = HashMap::new(); | ||
let mut shell = Shell::new(map); | ||
|
||
shell.new_command("put", "Insert a value", 2, |_, map, args| { | ||
map.insert(try!(usize::from_str(args[0])), args[1].to_string()); | ||
Ok(()) | ||
}); | ||
shell.new_command("get", "Get a value", 1, |mut io, map, args| { | ||
match map.get(&try!(usize::from_str(args[0]))) { | ||
Some(val) => writeln!(io, "{}", val).unwrap(), | ||
None => writeln!(io, "Not found").unwrap() | ||
}; | ||
Ok(()) | ||
}); | ||
shell.new_command("remove", "Remove a value", 1, |_, map, args| { | ||
map.remove(&try!(usize::from_str(args[0]))); | ||
Ok(()) | ||
}); | ||
shell.new_command("list", "List all values", 0, |mut io, map, _| { | ||
for (k, v) in map { | ||
writeln!(io, "{} = {}", k, v).unwrap(); | ||
} | ||
Ok(()) | ||
}); | ||
shell.new_command_noargs("clear", "Clear all values", |_, map| { | ||
map.clear(); | ||
Ok(()) | ||
}); | ||
|
||
shell.run_loop(&mut ShellIO::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
extern crate shrust; | ||
use shrust::{Shell, ShellIO}; | ||
use std::io::prelude::*; | ||
|
||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
|
||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
|
||
use std::net::TcpListener; | ||
|
||
fn main() { | ||
let map = Arc::new(Mutex::new(HashMap::new())); | ||
|
||
let mut shell = Shell::new(map); | ||
|
||
shell.new_command("put", "Insert a value", 2, |_, map, args| { | ||
map.lock().unwrap().insert(try!(usize::from_str(args[0])), args[1].to_string()); | ||
Ok(()) | ||
}); | ||
shell.new_command("get", "Get a value", 1, |mut io, map, args| { | ||
match map.lock().unwrap().get(&try!(usize::from_str(args[0]))) { | ||
Some(val) => writeln!(io, "{}", val).unwrap(), | ||
None => writeln!(io, "Not found").unwrap() | ||
}; | ||
Ok(()) | ||
}); | ||
shell.new_command("remove", "Remove a value", 1, |_, map, args| { | ||
map.lock().unwrap().remove(&try!(usize::from_str(args[0]))); | ||
Ok(()) | ||
}); | ||
shell.new_command("list", "List all values", 0, |mut io, map, _| { | ||
for (k, v) in &*map.lock().unwrap() { | ||
writeln!(io, "{} = {}", k, v).unwrap(); | ||
} | ||
Ok(()) | ||
}); | ||
shell.new_command_noargs("clear", "Clear all values", |_, map| { | ||
map.lock().unwrap().clear(); | ||
Ok(()) | ||
}); | ||
|
||
let serv = TcpListener::bind("0.0.0.0:1234").expect("Cannot open socket"); | ||
for sock in serv.incoming() { | ||
let sock = sock.unwrap(); | ||
let mut shell = shell.clone(); | ||
let mut io = ShellIO::new_io(sock); | ||
thread::spawn(move || shell.run_loop(&mut io)); | ||
} | ||
} | ||
extern crate shrust; | ||
use shrust::{Shell, ShellIO}; | ||
use std::io::prelude::*; | ||
|
||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
|
||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
|
||
use std::net::TcpListener; | ||
|
||
fn main() { | ||
let map = Arc::new(Mutex::new(HashMap::new())); | ||
|
||
let mut shell = Shell::new(map); | ||
|
||
shell.new_command("put", "Insert a value", 2, |_, map, args| { | ||
map.lock().unwrap().insert(try!(usize::from_str(args[0])), args[1].to_string()); | ||
Ok(()) | ||
}); | ||
shell.new_command("get", "Get a value", 1, |mut io, map, args| { | ||
match map.lock().unwrap().get(&try!(usize::from_str(args[0]))) { | ||
Some(val) => writeln!(io, "{}", val).unwrap(), | ||
None => writeln!(io, "Not found").unwrap() | ||
}; | ||
Ok(()) | ||
}); | ||
shell.new_command("remove", "Remove a value", 1, |_, map, args| { | ||
map.lock().unwrap().remove(&try!(usize::from_str(args[0]))); | ||
Ok(()) | ||
}); | ||
shell.new_command("list", "List all values", 0, |mut io, map, _| { | ||
for (k, v) in &*map.lock().unwrap() { | ||
writeln!(io, "{} = {}", k, v).unwrap(); | ||
} | ||
Ok(()) | ||
}); | ||
shell.new_command_noargs("clear", "Clear all values", |_, map| { | ||
map.lock().unwrap().clear(); | ||
Ok(()) | ||
}); | ||
|
||
let serv = TcpListener::bind("0.0.0.0:1234").expect("Cannot open socket"); | ||
for sock in serv.incoming() { | ||
let sock = sock.unwrap(); | ||
let mut shell = shell.clone(); | ||
let mut io = ShellIO::new_io(sock); | ||
thread::spawn(move || shell.run_loop(&mut io)); | ||
} | ||
} |
Oops, something went wrong.