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.

65 lines
1.7 KiB

  1. use crate::builtins::BuiltinConfig;
  2. use crate::prompt::Prompt;
  3. use colored::Colorize;
  4. use execute::interpret;
  5. use rustyline::completion::FilenameCompleter;
  6. use rustyline::config::Configurer;
  7. use rustyline::error::ReadlineError;
  8. use rustyline::{CompletionType, Config, Editor, Result};
  9. use rustyline_derive::{Completer, Helper, Highlighter, Hinter, Validator};
  10. use whoami::username;
  11. mod builtins;
  12. mod error;
  13. mod execute;
  14. mod parse;
  15. mod preprocess;
  16. mod prompt;
  17. #[derive(Completer, Helper, Highlighter, Hinter, Validator)]
  18. struct EditorHelper {
  19. #[rustyline(Completer)]
  20. completer: FilenameCompleter,
  21. }
  22. fn main() -> Result<()> {
  23. let (ctrlc_send, ctrlc_recv) = crossbeam_channel::unbounded::<()>();
  24. let _ = ctrlc::set_handler(move || {
  25. let _ = ctrlc_send.send(());
  26. });
  27. let config = Config::builder()
  28. .completion_type(CompletionType::List)
  29. .build();
  30. let helper = EditorHelper {
  31. completer: FilenameCompleter::new(),
  32. };
  33. let mut rl = Editor::with_config(config)?;
  34. rl.set_helper(Some(helper));
  35. rl.set_auto_add_history(true);
  36. let mut prompt = Prompt::new();
  37. let mut config = BuiltinConfig::new();
  38. println!("\nWelcome, {}.", username().bright_green().bold());
  39. loop {
  40. let readline = rl.readline(&prompt.get_prompt());
  41. match readline {
  42. Ok(line) => interpret(line, &mut config, ctrlc_recv.clone()),
  43. Err(ReadlineError::Interrupted) => continue,
  44. Err(ReadlineError::Eof) => break,
  45. Err(err) => {
  46. eprintln!("Error: {:?}", err);
  47. break;
  48. }
  49. }
  50. prompt.style = config.prompt_style.clone()
  51. }
  52. Ok(())
  53. }