Skip to content

Commit

Permalink
cli: Turn on pretty mode by default
Browse files Browse the repository at this point in the history
Fixes #929
  • Loading branch information
penberg committed Feb 9, 2025
1 parent a1a9218 commit 60250d0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
28 changes: 17 additions & 11 deletions cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Opts {
pub database: Option<PathBuf>,
#[clap(index = 2, help = "Optional SQL command to execute")]
pub sql: Option<String>,
#[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,
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Default for Io {

#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
pub enum OutputMode {
Raw,
List,
Pretty,
}

Expand Down Expand Up @@ -122,7 +122,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,
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Command {
Self::Help => ".help",
Self::Schema => ".schema ?<table>?",
Self::Opcodes => ".opcodes",
Self::OutputMode => ".mode raw|pretty",
Self::OutputMode => ".mode list|pretty",
Self::SetOutput => ".output ?file?",
Self::Cwd => ".cd <directory>",
Self::ShowInfo => ".show",
Expand Down Expand Up @@ -221,11 +221,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
Expand Down Expand Up @@ -258,7 +264,7 @@ impl std::fmt::Display for Settings {
}

impl Limbo {
pub fn new() -> anyhow::Result<Self> {
pub fn new(interactive: bool) -> anyhow::Result<Self> {
let opts = Opts::parse();
let db_file = opts
.database
Expand Down Expand Up @@ -294,7 +300,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()?;
Expand Down Expand Up @@ -392,7 +398,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(())
}
Expand Down Expand Up @@ -619,7 +625,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(());
Expand Down Expand Up @@ -941,7 +947,7 @@ Special Commands:
.quit Stop interpreting input stream and exit.
.show Display current settings.
.open <database_file> Open and connect to a database file.
.output <mode> Change the output mode. Available modes are 'raw' and 'pretty'.
.mode <mode> Change the output mode. Available modes are 'list' and 'pretty'.
.schema <table_name> Show the schema of the specified table.
.tables <pattern> List names of tables matching LIKE pattern TABLE
.opcodes Display all the opcodes defined by the virtual machine
Expand Down
4 changes: 3 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions testing/tester.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 8 additions & 2 deletions tests/integration/pragma/test_pragma_stmts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ mod tests {
use rexpect::session::{spawn_command, PtySession};
use std::process;

const TIMEOUT_MS: u64 = 5000;

#[test]
fn test_pragma_journal_mode_wal() -> Result<(), Error> {
let mut child = spawn_command(run_cli(), Some(1000))?;
let mut child = spawn_command(run_cli(), Some(TIMEOUT_MS))?;
child.exp_regex("limbo>")?; // skip everything until limbo cursor appear
child.exp_regex(".?")?;
child.send_line(".mode list")?;
child.exp_regex(".?")?;
child.send_line("pragma journal_mode;")?;
child.exp_string("wal")?;
quit(&mut child)
}

#[test]
fn test_pragma_wal_checkpoint() -> Result<(), Error> {
let mut child = spawn_command(run_cli(), Some(1000))?;
let mut child = spawn_command(run_cli(), Some(TIMEOUT_MS))?;
child.exp_regex("limbo>")?; // skip everything until limbo cursor appear
child.exp_regex(".?")?;
child.send_line(".mode list")?;
child.exp_regex(".?")?;
child.send_line("pragma wal_checkpoint;")?;
child.exp_string("0|0|0")?;
quit(&mut child)
Expand Down

0 comments on commit 60250d0

Please sign in to comment.