Skip to content

Commit

Permalink
Improved hisotry and implemented Deref(Mut) for Shell
Browse files Browse the repository at this point in the history
  • Loading branch information
pierresy committed Mar 15, 2016
1 parent 655965e commit c0131eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
8 changes: 4 additions & 4 deletions examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ fn main() {
let mut reg = Shell::new(map);

reg.new_command("put", "Insert a value", 2, |shell, args| {
shell.data().insert(try!(usize::from_str(args[0])), args[1].to_string());
shell.insert(try!(usize::from_str(args[0])), args[1].to_string());
Ok(())
});
reg.new_command("get", "Get a value", 1, |shell, args| {
match shell.data().get(&try!(usize::from_str(args[0]))) {
match shell.get(&try!(usize::from_str(args[0]))) {
Some(val) => println!("{}", val),
None => println!("Not found")
};
Ok(())
});
reg.new_command("remove", "Remove a value", 1, |shell, args| {
shell.data().remove(&try!(usize::from_str(args[0])));
shell.remove(&try!(usize::from_str(args[0])));
Ok(())
});
reg.new_command("list", "List all values", 0, |shell, _| {
Expand All @@ -30,7 +30,7 @@ fn main() {
Ok(())
});
reg.new_command_noargs("clear", "Clear all values", |shell| {
shell.data().clear();
shell.clear();
Ok(())
});

Expand Down
91 changes: 56 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::string::ToString;
use std::rc::Rc;
use std::error::Error;
use std::fmt;
use std::ops::{Deref, DerefMut};

use std::collections::BTreeMap;

Expand Down Expand Up @@ -90,8 +91,7 @@ pub struct Shell<T> {
commands: BTreeMap<String, Rc<Command<T>>>,
data: T,
prompt: String,
history: Vec<String>,
history_size: usize
history: History
}

impl <T> Shell<T> {
Expand All @@ -100,8 +100,7 @@ impl <T> Shell<T> {
commands: BTreeMap::new(),
data: data,
prompt: String::from(">"),
history: Vec::new(),
history_size: 10
history: History::new(10),
};
sh.register_command(builtins::help_cmd());
sh.register_command(builtins::quit_cmd());
Expand All @@ -117,13 +116,6 @@ impl <T> Shell<T> {
self.prompt = prompt;
}

pub fn set_history_size(&mut self, size: usize) {
self.history_size = size;
while self.history.len() > size {
self.history.remove(0);
}
}

pub fn register_command(&mut self, cmd: Command<T>) {
self.commands.insert(cmd.name.clone(), Rc::new(cmd));
}
Expand All @@ -150,27 +142,8 @@ impl <T> Shell<T> {
return Ok(());
}

pub fn print_history(&self) -> ExecResult {
let mut cnt = 0;
for s in &self.history {
println!("{}: {}", cnt, s);
cnt += 1;
}
return Ok(());
}

pub fn run_history(&mut self, i: usize) -> ExecResult {
return match self.history.get(i).map(|s| s.clone()) {
Some(ref cmd) => self.run(cmd),
None => Err(InvalidHistory(i))
};
}

fn push_history(&mut self, line: String) {
self.history.push(line);
if self.history.len() > 10 {
self.history.remove(0);
}
pub fn get_history(&mut self) -> &mut History {
return &mut self.history;
}

pub fn run(&mut self, line: &str) -> ExecResult {
Expand Down Expand Up @@ -201,13 +174,59 @@ impl <T> Shell<T> {
e @ _ => println!("Error : {}", e)
};
} else {
self.push_history(line);
self.get_history().push(line);
}
self.print_prompt();
}
}
}

impl <T> Deref for Shell<T> {
type Target = T;
fn deref(&self) -> &T {
return &self.data;
}
}

impl <T> DerefMut for Shell<T> {
fn deref_mut(&mut self) -> &mut T {
return &mut self.data;
}
}

pub struct History {
history: Vec<String>,
capacity: usize
}

impl History {
pub fn new(capacity: usize) -> History {
return History {
history: Vec::with_capacity(capacity),
capacity: capacity
};
}

pub fn push(&mut self, cmd: String) {
if self.history.len() >= self.capacity {
self.history.remove(0);
}
self.history.push(cmd);
}

pub fn print(&self) {
let mut cnt = 0;
for s in &self.history {
println!("{}: {}", cnt, s);
cnt += 1;
}
}

pub fn get(&self, i: usize) -> Option<String> {
return self.history.get(i).map(|s| s.clone());
}
}

mod builtins {
use std::str::FromStr;
use super::Command;
Expand All @@ -225,9 +244,11 @@ mod builtins {
return Command::new("history".to_string(), "Print commands history or run a command from it".to_string(), 0, Box::new(|shell, args| {
if args.len() > 0 {
let i = try!(usize::from_str(args[0]));
return shell.run_history(i);
let cmd = try!(shell.get_history().get(i).ok_or(ExecError::InvalidHistory(i)));
return shell.run(&cmd);
} else {
return shell.print_history();
shell.get_history().print();
return Ok(());
}
}));
}
Expand Down

0 comments on commit c0131eb

Please sign in to comment.