logging is in a separate method now

main
Jordan Orelli 1 year ago
parent 522668be28
commit 4a0db72d4e

@ -19,21 +19,12 @@ use anyhow::Result;
fn main() -> Result<()> { fn main() -> Result<()> {
let mut shell = Shell::new()?; let mut shell = Shell::new()?;
let log_path = shell.expand_path("~/clyde.log"); shell.enable_logging("~/clyde.log");
match Log::file(log_path) {
Ok(f) => {
let target = Box::leak(Box::new(f));
_ = set_logger(target).map(|()| set_max_level(LevelFilter::Debug));
}
Err(e) => {
println!("did not open log file: {}", e);
}
}
let prompt = Prompt::new(); let prompt = Prompt::new();
prompt.print(&mut shell.output)?; prompt.print(&mut shell.output)?;
info!(enter"); info!("» shell session start --------");
loop { loop {
match shell.input.next()? { match shell.input.next()? {
input::Event::Key(event) => { input::Event::Key(event) => {
@ -189,17 +180,3 @@ fn main() -> Result<()> {
} }
} }
} }
/*
> ls
foo
bar
whatever
> ls
*/

@ -7,6 +7,15 @@ pub enum Error {
UnexpectedToken, UnexpectedToken,
} }
pub struct Tok {
start_line: usize,
start_col: usize,
end_line: usize,
end_col: usize,
text: String,
kind: Token,
}
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Token { pub enum Token {
@ -17,7 +26,7 @@ pub enum Token {
#[allow(dead_code)] #[allow(dead_code)]
fn lex<S: AsRef<str>>(text: S) -> Result<Vec<Token>, Error> { fn lex<S: AsRef<str>>(text: S) -> Result<Vec<Token>, Error> {
Lexer::new(text.as_ref()).lex() Ok(Lexer::new(text.as_ref()).collect())
} }
struct Lexer<'text> { struct Lexer<'text> {
@ -35,27 +44,6 @@ impl<'text> Lexer<'text> {
} }
} }
fn lex(&mut self) -> Result<Vec<Token>, Error> {
let mut tokens = vec![];
while self.peek().is_some() {
self.skip_whitespace();
match self.peek() {
None => return Ok(tokens),
Some('|') => {
tokens.push(Token::Pipe);
self.skip();
}
Some('*') => {
tokens.push(self.glob());
}
Some(_) => {
tokens.push(self.ident());
}
}
}
Ok(tokens)
}
fn skip_whitespace(&mut self) { fn skip_whitespace(&mut self) {
loop { loop {
match self.peek() { match self.peek() {
@ -112,7 +100,7 @@ impl<'text> Lexer<'text> {
Some('*') => { Some('*') => {
self.keep(); self.keep();
self.glob(); self.glob();
}, }
Some(c) => { Some(c) => {
if c.is_whitespace() { if c.is_whitespace() {
break; break;
@ -146,6 +134,24 @@ impl<'text> Lexer<'text> {
} }
} }
impl <'text> Iterator for Lexer<'text> {
type Item = Token;
fn next(&mut self) -> Option<Token> {
self.skip_whitespace();
self.kept.clear();
match self.peek() {
Some('|') => {
self.skip();
Some(Token::Pipe)
}
Some('*') => Some(self.glob()),
Some(_) => Some(self.ident()),
None => None,
}
}
}
#[derive(PartialEq, Clone)] #[derive(PartialEq, Clone)]
pub enum Element { pub enum Element {
Empty, Empty,
@ -350,6 +356,10 @@ mod tests {
Token::Ident(s.into()) Token::Ident(s.into())
} }
fn glob<S: Into<String>>(s: S) -> Token {
Token::Glob(s.into())
}
macro_rules! lex { macro_rules! lex {
( (
$($name:ident: $line:literal $($token:expr)* ;)+ $($name:ident: $line:literal $($token:expr)* ;)+
@ -407,6 +417,26 @@ mod tests {
pipeline_2: pipeline_2:
"one |two" "one |two"
ident("one") Pipe ident("two"); ident("one") Pipe ident("two");
simple_glob:
"*"
glob("*");
ext_glob:
"*.rs"
glob("*.rs");
mixed:
"ls *.rs"
ident("ls") glob("*.rs");
globby_pipeline:
"ls *.rs | wc -l"
ident("ls") glob("*.rs") Pipe ident("wc") ident("-l");
mid_glob:
"a*b"
glob("a*b");
} }
#[test] #[test]

@ -69,6 +69,22 @@ impl Shell {
Ok(()) Ok(())
} }
pub fn enable_logging<P>(&self, path: P)
where
P: AsRef<Path>,
{
let log_path = self.expand_path(path);
match Log::file(log_path) {
Ok(f) => {
let target = Box::leak(Box::new(f));
_ = set_logger(target).map(|()| set_max_level(LevelFilter::Debug));
}
Err(e) => {
println!("did not open log file: {}", e);
}
}
}
pub fn expand_path<P: AsRef<Path>>(&self, p: P) -> PathBuf { pub fn expand_path<P: AsRef<Path>>(&self, p: P) -> PathBuf {
let p = p.as_ref(); let p = p.as_ref();

Loading…
Cancel
Save