|
| 1 | +use { |
| 2 | + console::Style, |
| 3 | + dialoguer::{theme::ColorfulTheme, Confirm, Editor, Input, Select}, |
| 4 | + indicatif::{ProgressBar, ProgressStyle}, |
| 5 | + lazy_static::lazy_static, |
| 6 | + multisql::{CSVStorage, Glue, SledStorage, Storage}, |
| 7 | +}; |
| 8 | + |
| 9 | +lazy_static! { |
| 10 | + pub(crate) static ref PROGRESS_STYLE: ProgressStyle = ProgressStyle::default_spinner() |
| 11 | + .template("{spinner:.magenta} {elapsed:3.red} {msg:.green}") |
| 12 | + .tick_chars("|/—\\*"); |
| 13 | +} |
| 14 | + |
| 15 | +fn main() { |
| 16 | + let mut glue = Glue::new_multi(vec![]); |
| 17 | + while prompt(&mut glue) {} |
| 18 | +} |
| 19 | + |
| 20 | +const PROMPT_ACTION: [&str; 2] = ["Connect", "Query"]; |
| 21 | +const PROMPT_KIND: [&str; 2] = ["Sled", "CSV"]; |
| 22 | +const QUERY_KIND: [&str; 3] = ["Small", "Big", "File"]; |
| 23 | + |
| 24 | +fn prompt(glue: &mut Glue) -> bool { |
| 25 | + let mut input_theme = ColorfulTheme::default(); |
| 26 | + input_theme.active_item_prefix = Style::new().green().apply_to(String::from("•-")); |
| 27 | + |
| 28 | + match Select::with_theme(&input_theme) |
| 29 | + .items(&PROMPT_ACTION) |
| 30 | + .default(0) |
| 31 | + .interact() |
| 32 | + .unwrap() |
| 33 | + { |
| 34 | + 0 => { |
| 35 | + let name = Input::with_theme(&input_theme) |
| 36 | + .with_prompt("Name") |
| 37 | + .interact() |
| 38 | + .unwrap(); |
| 39 | + let new_storage = match Select::with_theme(&input_theme) |
| 40 | + .items(&PROMPT_KIND) |
| 41 | + .default(0) |
| 42 | + .interact() |
| 43 | + .unwrap() |
| 44 | + { |
| 45 | + 0 => { |
| 46 | + let path: String = Input::with_theme(&input_theme) |
| 47 | + .with_prompt("Path") |
| 48 | + .interact() |
| 49 | + .unwrap(); |
| 50 | + Storage::new_sled(SledStorage::new(&path).unwrap()) |
| 51 | + } |
| 52 | + 1 => { |
| 53 | + let path: String = Input::with_theme(&input_theme) |
| 54 | + .with_prompt("Path") |
| 55 | + .interact() |
| 56 | + .unwrap(); |
| 57 | + Storage::new_csv(CSVStorage::new(&path).unwrap()) |
| 58 | + } |
| 59 | + _ => unreachable!(), |
| 60 | + }; |
| 61 | + glue.extend(vec![Glue::new(name, new_storage)]); |
| 62 | + } |
| 63 | + 1 => { |
| 64 | + let query = match Select::with_theme(&input_theme) |
| 65 | + .items(&QUERY_KIND) |
| 66 | + .default(0) |
| 67 | + .interact() |
| 68 | + .unwrap() |
| 69 | + { |
| 70 | + 0 => Input::with_theme(&input_theme) |
| 71 | + .with_prompt("Query") |
| 72 | + .interact() |
| 73 | + .unwrap(), |
| 74 | + 1 => { |
| 75 | + let text = Editor::new() |
| 76 | + .extension("sql") |
| 77 | + .require_save(false) |
| 78 | + .edit("") |
| 79 | + .unwrap() |
| 80 | + .unwrap(); |
| 81 | + println!("{}", text); |
| 82 | + Confirm::with_theme(&input_theme) |
| 83 | + .with_prompt("Run") |
| 84 | + .default(true) |
| 85 | + .interact() |
| 86 | + .unwrap(); |
| 87 | + text |
| 88 | + } |
| 89 | + 2 => unimplemented!(), |
| 90 | + _ => unreachable!(), |
| 91 | + }; |
| 92 | + |
| 93 | + let progress = ProgressBar::new_spinner() |
| 94 | + .with_message(format!("Running Query")) |
| 95 | + .with_style(PROGRESS_STYLE.clone()); |
| 96 | + progress.enable_steady_tick(200); |
| 97 | + |
| 98 | + glue.execute(&query).unwrap(); |
| 99 | + |
| 100 | + progress.finish(); |
| 101 | + } |
| 102 | + _ => unreachable!(), |
| 103 | + } |
| 104 | + |
| 105 | + true |
| 106 | +} |
0 commit comments