diff --git a/cli/main.rs b/cli/main.rs index 4f3fa3b26..700e0a098 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use clap::Parser; +use clap::{Parser, ValueEnum}; use cli_table::{Cell, Table}; use lig_core::{Database, DatabaseRef, Value}; use rustyline::{error::ReadlineError, DefaultEditor}; @@ -10,10 +10,27 @@ use std::io::{Read, Seek}; use std::path::PathBuf; use std::sync::Arc; +#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)] +enum OutputMode { + Raw, + Pretty, +} + +impl std::fmt::Display for OutputMode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.to_possible_value() + .expect("no values are skipped") + .get_name() + .fmt(f) + } +} + #[derive(Parser)] #[command(author, version, about, long_about = None)] struct Opts { database: PathBuf, + #[clap(short, long, default_value_t = OutputMode::Pretty)] + output_mode: OutputMode, } fn main() -> anyhow::Result<()> { @@ -34,25 +51,42 @@ fn main() -> anyhow::Result<()> { Ok(line) => { rl.add_history_entry(line.to_owned())?; match conn.query(line) { - Ok(Some(ref mut rows)) => { - let mut table_rows: Vec> = vec![]; - while let Some(row) = rows.next()? { - table_rows.push( - row.values - .iter() - .map(|value| match value { - Value::Null => "NULL".cell(), - Value::Integer(i) => i.to_string().cell(), - Value::Float(f) => f.to_string().cell(), - Value::Text(s) => s.cell(), - Value::Blob(b) => format!("{:?}", b).cell(), - }) - .collect(), - ); + Ok(Some(ref mut rows)) => match opts.output_mode { + OutputMode::Raw => { + while let Some(row) = rows.next()? { + print!("|"); + for val in row.values.iter() { + match val { + Value::Null => print!("NULL|"), + Value::Integer(i) => print!("{}|", i), + Value::Float(f) => print!("{}|", f), + Value::Text(s) => print!("{}|", s), + Value::Blob(b) => print!("{:?}|", b), + } + } + println!(); + } } - let table = table_rows.table(); - cli_table::print_stdout(table).unwrap(); - } + OutputMode::Pretty => { + let mut table_rows: Vec> = vec![]; + while let Some(row) = rows.next()? { + table_rows.push( + row.values + .iter() + .map(|value| match value { + Value::Null => "NULL".cell(), + Value::Integer(i) => i.to_string().cell(), + Value::Float(f) => f.to_string().cell(), + Value::Text(s) => s.cell(), + Value::Blob(b) => format!("{:?}", b).cell(), + }) + .collect(), + ); + } + let table = table_rows.table(); + cli_table::print_stdout(table).unwrap(); + } + }, Ok(None) => {} Err(err) => { eprintln!("{}", err);