(con: Connection)
| 39 | "; |
| 40 | |
| 41 | pub async fn exec(con: Connection) -> Result<(), anyhow::Error> { |
| 42 | let database = con.database.clone(); |
| 43 | let mut rl = Editor::<ReplHelper, DefaultHistory>::new().unwrap(); |
| 44 | let history = home_dir().unwrap_or_else(temp_dir).join(".stdb.history.txt"); |
| 45 | if rl.load_history(&history).is_err() { |
| 46 | eprintln!("No previous history."); |
| 47 | } |
| 48 | rl.set_helper(Some(ReplHelper::new().unwrap())); |
| 49 | |
| 50 | println!( |
| 51 | "\ |
| 52 | ┌──────────────────────────────────────────────────────────┐ |
| 53 | │ .exit: Exit the REPL │ |
| 54 | │ .clear: Clear the Screen │ |
| 55 | │ │ |
| 56 | │ Give us feedback in our Discord server: │ |
| 57 | │ https://discord.gg/w2DVqNZXdN │ |
| 58 | └──────────────────────────────────────────────────────────┘", |
| 59 | ); |
| 60 | |
| 61 | let api = ClientApi::new(con); |
| 62 | |
| 63 | loop { |
| 64 | let readline = rl.readline(&format!("🪐{}>", &database).green()); |
| 65 | match readline { |
| 66 | Ok(line) => match line.as_str() { |
| 67 | ".exit" => break, |
| 68 | ".clear" => { |
| 69 | rl.clear_screen().ok(); |
| 70 | } |
| 71 | sql => { |
| 72 | rl.add_history_entry(sql).ok(); |
| 73 | |
| 74 | if let Err(err) = run_sql(api.sql(), sql, true).await { |
| 75 | eprintln!("{}", err.to_string().red()) |
| 76 | } |
| 77 | } |
| 78 | }, |
| 79 | Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => { |
| 80 | println!("\n{}", "Aborted!".red()); |
| 81 | break; |
| 82 | } |
| 83 | x => { |
| 84 | eprintln!("\nUnexpected: {x:?}"); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | rl.save_history(&history).ok(); |
| 91 | |
| 92 | Ok(()) |
| 93 | } |
| 94 | |
| 95 | pub(crate) struct ReplHelper { |
| 96 | syntaxes: SyntaxSet, |
nothing calls this directly
no test coverage detected
searching dependent graphs…