From 682286402797167027c8433fbbad83de63d5b660 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 8 Feb 2025 12:07:59 +0200 Subject: [PATCH] cli: Turn on pretty mode by default Fixes #929 --- cli/app.rs | 28 +++++++++++++++++----------- cli/main.rs | 4 +++- testing/tester.tcl | 5 +++-- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cli/app.rs b/cli/app.rs index b4d029648..4b4585e07 100644 --- a/cli/app.rs +++ b/cli/app.rs @@ -25,7 +25,7 @@ pub struct Opts { pub database: Option, #[clap(index = 2, help = "Optional SQL command to execute")] pub sql: Option, - #[clap(short = 'm', long, default_value_t = OutputMode::Raw)] + #[clap(short = 'm', long, default_value_t = OutputMode::List)] pub output_mode: OutputMode, #[clap(short, long, default_value = "")] pub output: String, @@ -88,7 +88,7 @@ impl Default for Io { #[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)] pub enum OutputMode { - Raw, + List, Pretty, } @@ -121,7 +121,7 @@ pub enum Command { Cwd, /// Display information about settings ShowInfo, - /// Set the value of NULL to be printedin 'raw' mode + /// Set the value of NULL to be printedin 'list' mode NullValue, /// Toggle 'echo' mode to repeat commands before execution Echo, @@ -160,7 +160,7 @@ impl Command { Self::Help => ".help", Self::Schema => ".schema ??", Self::Opcodes => ".opcodes", - Self::OutputMode => ".mode raw|pretty", + Self::OutputMode => ".mode list|pretty", Self::SetOutput => ".output ?file?", Self::Cwd => ".cd ", Self::ShowInfo => ".show", @@ -220,11 +220,17 @@ pub struct Settings { impl From<&Opts> for Settings { fn from(opts: &Opts) -> Self { + let is_stdout = opts.output.is_empty(); + let output_mode = if opts.output_mode == OutputMode::List && is_stdout { + OutputMode::Pretty + } else { + opts.output_mode + }; Self { null_value: String::new(), - output_mode: opts.output_mode, + output_mode, echo: false, - is_stdout: opts.output.is_empty(), + is_stdout, output_filename: opts.output.clone(), db_file: opts .database @@ -257,7 +263,7 @@ impl std::fmt::Display for Settings { } impl Limbo { - pub fn new() -> anyhow::Result { + pub fn new(interactive: bool) -> anyhow::Result { let opts = Opts::parse(); let db_file = opts .database @@ -293,7 +299,7 @@ impl Limbo { if opts.sql.is_some() { app.handle_first_input(opts.sql.as_ref().unwrap()); } - if !opts.quiet { + if !opts.quiet && interactive { app.write_fmt(format_args!("Limbo v{}", env!("CARGO_PKG_VERSION")))?; app.writeln("Enter \".help\" for usage hints.")?; app.display_in_memory()?; @@ -391,7 +397,7 @@ impl Limbo { Ok(file) => { self.writer = Box::new(file); self.opts.is_stdout = false; - self.opts.output_mode = OutputMode::Raw; + self.opts.output_mode = OutputMode::List; self.opts.output_filename = path.to_string(); Ok(()) } @@ -618,7 +624,7 @@ impl Limbo { ) -> anyhow::Result<()> { match output { Ok(Some(ref mut rows)) => match self.opts.output_mode { - OutputMode::Raw => loop { + OutputMode::List => loop { if self.interrupt_count.load(Ordering::SeqCst) > 0 { println!("Query interrupted."); return Ok(()); @@ -916,7 +922,7 @@ Special Commands: .quit Stop interpreting input stream and exit. .show Display current settings. .open Open and connect to a database file. -.output Change the output mode. Available modes are 'raw' and 'pretty'. +.mode Change the output mode. Available modes are 'list' and 'pretty'. .schema Show the schema of the specified table. .tables List names of tables matching LIKE pattern TABLE .opcodes Display all the opcodes defined by the virtual machine diff --git a/cli/main.rs b/cli/main.rs index df4aa9787..bb185243e 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -4,11 +4,13 @@ mod import; mod opcodes_dictionary; use rustyline::{error::ReadlineError, DefaultEditor}; +use std::io::IsTerminal; use std::sync::atomic::Ordering; fn main() -> anyhow::Result<()> { env_logger::init(); - let mut app = app::Limbo::new()?; + let interactive = std::io::stdin().is_terminal(); + let mut app = app::Limbo::new(interactive)?; let mut rl = DefaultEditor::new()?; let home = dirs::home_dir().expect("Could not determine home directory"); let history_file = home.join(".limbo_history"); diff --git a/testing/tester.tcl b/testing/tester.tcl index 735c91aae..738031966 100644 --- a/testing/tester.tcl +++ b/testing/tester.tcl @@ -3,8 +3,9 @@ set test_dbs [list "testing/testing.db" "testing/testing_norowidalias.db"] set test_small_dbs [list "testing/testing_small.db" ] proc evaluate_sql {sqlite_exec db_name sql} { - set command [list $sqlite_exec $db_name $sql] - set output [exec {*}$command] + set command [list $sqlite_exec $db_name] + set sql_with_mode ".mode list\n$sql" + set output [exec {*}$command << $sql_with_mode] return $output }