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

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