nushell/src/main.rs

134 lines
3.9 KiB
Rust
Raw Normal View History

2019-05-10 18:59:12 +02:00
#![feature(crate_visibility_modifier)]
#![feature(in_band_lifetimes)]
#![allow(unused)]
mod commands;
mod env;
mod errors;
mod format;
mod object;
2019-05-11 06:45:57 +02:00
mod parser;
2019-05-10 18:59:12 +02:00
2019-05-11 10:08:21 +02:00
crate use crate::commands::command::{Command, CommandAction, CommandBlueprint, CommandSuccess};
2019-05-10 18:59:12 +02:00
crate use crate::env::{Environment, Host};
2019-05-11 06:45:57 +02:00
crate use crate::errors::ShellError;
2019-05-11 09:00:33 +02:00
crate use crate::format::RenderView;
2019-05-10 18:59:12 +02:00
use crate::object::base::{ToEntriesView, ToGenericView};
2019-05-11 09:00:33 +02:00
use ansi_term::Color;
use conch_parser::lexer::Lexer;
use conch_parser::parse::DefaultParser;
2019-05-10 18:59:12 +02:00
use rustyline::error::ReadlineError;
use rustyline::Editor;
2019-05-11 10:08:21 +02:00
use std::cell::RefCell;
2019-05-10 18:59:12 +02:00
use std::collections::BTreeMap;
use std::error::Error;
2019-05-11 10:08:21 +02:00
use std::rc::Rc;
2019-05-11 09:00:33 +02:00
use subprocess::Exec;
2019-05-10 18:59:12 +02:00
use sysinfo::{self, SystemExt};
#[derive(Debug)]
pub enum MaybeOwned<'a, T> {
Owned(T),
Borrowed(&'a T),
}
impl<T> MaybeOwned<'a, T> {
crate fn borrow(&self) -> &T {
match self {
MaybeOwned::Owned(v) => v,
MaybeOwned::Borrowed(v) => v,
}
}
}
fn main() -> Result<(), Box<Error>> {
let mut rl = Editor::<()>::new();
if rl.load_history("history.txt").is_err() {
println!("No previous history.");
}
let mut host = crate::env::host::BasicHost;
let mut env = crate::Environment::basic()?;
2019-05-11 10:08:21 +02:00
let mut commands = BTreeMap::<String, Box<dyn crate::CommandBlueprint>>::new();
2019-05-10 18:59:12 +02:00
2019-05-11 10:08:21 +02:00
let mut system = Rc::new(RefCell::new(sysinfo::System::new()));
let mut ps = crate::commands::ps::PsBlueprint::new(system);
let mut ls = crate::commands::ls::LsBlueprint;
let mut cd = crate::commands::cd::CdBlueprint;
2019-05-10 18:59:12 +02:00
commands.insert("ps".to_string(), Box::new(ps));
commands.insert("ls".to_string(), Box::new(ls));
2019-05-11 09:00:33 +02:00
commands.insert("cd".to_string(), Box::new(cd));
2019-05-10 18:59:12 +02:00
loop {
2019-05-11 09:00:33 +02:00
let readline = rl.readline(&format!(
"{}> ",
Color::Green.paint(env.cwd().display().to_string())
));
2019-05-11 06:45:57 +02:00
2019-05-10 18:59:12 +02:00
match readline {
Ok(line) => {
2019-05-11 09:00:33 +02:00
let result = crate::parser::shell_parser(&line)
.map_err(|e| ShellError::new(format!("{:?}", e)))?;
2019-05-11 06:45:57 +02:00
let parsed = result.1;
2019-05-10 18:59:12 +02:00
rl.add_history_entry(line.as_ref());
2019-05-11 06:45:57 +02:00
if parsed.len() > 1 {
println!("Piping is not yet implemented");
}
let command = &parsed[0][0].name();
2019-05-11 09:00:33 +02:00
let args = parsed[0][1..]
.iter()
.map(|i| i.name().to_string())
.collect();
2019-05-11 06:45:57 +02:00
match commands.get_mut(*command) {
2019-05-10 18:59:12 +02:00
Some(command) => {
2019-05-11 10:08:21 +02:00
let mut instance = command.create(args, &mut host, &mut env);
let result = instance.run()?;
for action in result.action {
match action {
crate::CommandAction::ChangeCwd(cwd) => env.cwd = cwd,
}
}
let view = result.value.to_generic_view();
2019-05-10 18:59:12 +02:00
let rendered = view.render_view(&mut host);
for line in rendered {
match line.as_ref() {
"\n" => println!(""),
line => println!("{}", line),
}
}
}
2019-05-11 09:00:33 +02:00
other => {
Exec::shell(line).cwd(env.cwd()).join().unwrap();
}
2019-05-10 18:59:12 +02:00
}
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
break;
}
Err(ReadlineError::Eof) => {
println!("CTRL-D");
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
rl.save_history("history.txt").unwrap();
Ok(())
}