You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

69 lines
1.8 KiB

use crate::builtins::BuiltinConfig;
use crate::prompt::Prompt;
use colored::Colorize;
use execute::interpret;
use rustyline::completion::FilenameCompleter;
use rustyline::config::Configurer;
use rustyline::error::ReadlineError;
use rustyline::hint::HistoryHinter;
use rustyline::{CompletionType, Config, Editor, Result};
use rustyline_derive::{Completer, Helper, Highlighter, Hinter, Validator};
use whoami::username;
mod builtins;
mod error;
mod execute;
mod parse;
mod preprocess;
mod prompt;
#[derive(Completer, Helper, Highlighter, Hinter, Validator)]
struct EditorHelper {
#[rustyline(Completer)]
completer: FilenameCompleter,
#[rustyline(Hinter)]
hinter: HistoryHinter,
}
fn main() -> Result<()> {
let (ctrlc_send, ctrlc_recv) = crossbeam_channel::unbounded::<()>();
let _ = ctrlc::set_handler(move || {
let _ = ctrlc_send.send(());
});
let config = Config::builder()
.completion_type(CompletionType::List)
.build();
let helper = EditorHelper {
completer: FilenameCompleter::new(),
hinter: HistoryHinter {},
};
let mut rl = Editor::with_config(config)?;
rl.set_helper(Some(helper));
rl.set_auto_add_history(true);
let mut prompt = Prompt::new();
let mut config = BuiltinConfig::new();
println!("\nWelcome, {}.", username().bright_green().bold());
loop {
let readline = rl.readline(&prompt.get_prompt());
match readline {
Ok(line) => interpret(line, &mut config, ctrlc_recv.clone()),
Err(ReadlineError::Interrupted) => continue,
Err(ReadlineError::Eof) => break,
Err(err) => {
eprintln!("Error: {:?}", err);
break;
}
}
prompt.style = config.prompt_style.clone()
}
Ok(())
}